-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshttpd_error.c
108 lines (96 loc) · 3.76 KB
/
shttpd_error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "shttpd.h"
enum {
ERROR301, ERROR302, ERROR303, ERROR304, ERROR305, ERROR307,
ERROR400, ERROR401, ERROR402, ERROR403, ERROR404, ERROR405, ERROR406,
ERROR407, ERROR408, ERROR409, ERROR410, ERROR411, ERROR412, ERROR413,
ERROR414, ERROR415, ERROR416, ERROR417,
ERROR500, ERROR501, ERROR502, ERROR503, ERROR504, ERROR505
};
struct error_mine {
int error_code;
char *content;
char *msg;
int status;
};
struct error_mine _error_http[] =
{
{ERROR301, "Error: 301", "永久移动", 301},
{ERROR302, "Error: 302", "创建", 302},
{ERROR303, "Error: 303", "观察别的部分", 303},
{ERROR304, "Error: 304", "只读", 304},
{ERROR305, "Error: 305", "用户代理", 305},
{ERROR307, "Error: 307", "临时重发", 307},
{ERROR400, "Error: 400", "坏请求", 400},
{ERROR401, "Error: 401", "未授权的", 401},
{ERROR402, "Error: 402", "必要的支付", 402},
{ERROR403, "Error: 403", "禁用", 403},
{ERROR404, "Error: 404", "没找到", 404},
{ERROR405, "Error: 405", "不允许的方式", 405},
{ERROR406, "Error: 406", "不接受", 406},
{ERROR407, "Error: 407", "需要代理验证", 407},
{ERROR408, "Error: 408", "请求超时", 408},
{ERROR409, "Error: 409", "冲突", 409},
{ERROR410, "Error: 410", "停止", 410},
{ERROR411, "Error: 411", "需要的长度", 411},
{ERROR412, "Error: 412", "预处理失败", 412},
{ERROR413, "Error: 413", "请求实体太大", 413},
{ERROR414, "Error: 414", "请求-URI太大", 414},
{ERROR415, "Error: 415", "不支持的媒体类型", 415},
{ERROR416, "Error: 416", "请求的范围不满足", 416},
{ERROR417, "Error: 417", "期望失败", 417},
{ERROR500, "Error: 500", "服务器内部错误", 500},
{ERROR501, "Error: 501", "不能实现", 501},
{ERROR502, "Error: 502", "坏网关", 502},
{ERROR503, "Error: 503", "服务不能实现", 503},
{ERROR504, "Error: 504", "网关超时", 504},
{ERROR505, "Error: 505", "HTTP版本不支持", 505}
};
void Error_400(struct worker_ctl *wctl) {
wctl->conn.con_res.status = 400;
GenerateErrorMine(wctl);
}
void Error_403(struct worker_ctl *wctl) {
wctl->conn.con_res.status = 403;
GenerateErrorMine(wctl);
}
void Error_404(struct worker_ctl *wctl) {
wctl->conn.con_res.status = 404;
GenerateErrorMine(wctl);
}
void Error_505(struct worker_ctl *wctl) {
wctl->conn.con_res.status = 505;
GenerateErrorMine(wctl);
}
int GenerateErrorMine(struct worker_ctl *wctl) {
struct error_mine *err = NULL;
int n = sizeof(_error_http);
int i;
for (i=0; i < n; i++) {
err = &_error_http[i];
if (err->status == wctl->conn.con_res.status) {
break;
}
}
if (err->status != wctl->conn.con_res.status) {
err = &_error_http[0];
}
snprintf(
wctl->conn.dres,
sizeof(wctl->conn.dres),
"HTTP/%lu.%lu %d %s\r\n"
"Content-Type:%s\r\n"
"Content-Length:%d\r\n"
"\r\n"
"%s",
wctl->conn.con_req.major,
wctl->conn.con_req.minor,
err->status,
err->msg,
"text/plain",
strlen(err->content),
err->content);
wctl->conn.con_res.cl = strlen(err->content);
wctl->conn.con_res.fd = -1;
wctl->conn.con_res.status = err->status;
return 0;
}