服务器SHTTPD支持错误值绝大多数的错误响应,其错误代码定义如下:
enum{
ERROR301=301, ERROR302=302, ERROR303, ERROR304, ERROR305, ERROR307=
307,
ERROR400=400, ERROR401, ERROR402, ERROR403, ERROR404, ERROR405,
ERROR406,
ERROR407, ERROR408, ERROR409, ERROR410, ERROR411, ERROR412, ERROR413,
ERROR414, ERROR415, ERROR416, ERROR417,
ERROR500=500, ERROR501, ERROR502, ERROR503, ERROR504, ERROR505
};
将错误代码的错误信息和含义及错误代码值构建一个全局的错误信息结构数组,代码如下:
struct error_mine{
int error_code; /*错误代码*/
char *content; /*错误信息*/
char *msg; /*含义*/
}_error_http={
{ERROR301, "Error: 301", "永久移动" },
{ERROR302, "Error: 302", "创建" },
{ERROR303, "Error: 303", "观察别的部分" },
{ERROR304, "Error: 304", "只读" },
{ERROR305, "Error: 305", "用户代理" },
{ERROR307, "Error: 307", "临时重发" },
{ERROR400, "Error: 400", "坏请求" },
{ERROR401, "Error: 401", "未授权的" },
{ERROR402, "Error: 402", "必要的支付" },
{ERROR403, "Error: 403", "禁用" },
{ERROR404, "Error: 404", "没找到" },
{ERROR405, "Error: 405", "不允许的方式" },
{ERROR406, "Error: 406", "不接受" },
{ERROR407, "Error: 407", "需要代理验证" },
{ERROR408, "Error: 408", "请求超时" },
{ERROR409, "Error: 409", "冲突" },
{ERROR410, "Error: 410", "停止" },
{ERROR411, "Error: 411", "需要的长度" },
{ERROR412, "Error: 412", "预处理失败" },
{ERROR413, "Error: 413", "请求实体太大" },
{ERROR414, "Error: 414", "请求-URI太大" },
{ERROR415, "Error: 415", "不支持的媒体类型", },
{ERROR416, "Error: 416", "请求的范围不满足" },
{ERROR417, "Error: 417", "期望失败" },
{ERROR500, "Error: 500", "服务器内部错误" },
{ERROR501, "Error: 501", "不能实现" },
{ERROR502, "Error: 502", "坏网关" },
{ERROR503, "Error: 503", "服务不能实现" },
{ERROR504, "Error: 504", "网关超时" },
{ERROR505, "Error: 505", "HTTP版本不支持" },
{0, NULL, NULL }
};
错误类型生成的方法为查找变量_error_http中与每个状态值匹配的项,利用此项来构建头部信息。头部信息的结构与Get方法中的含义一致。
int GenerateErrorMine(struct worker_ctl * wctl)
{
struct error_mine *err = NULL; /*错误类型*/
int i = 0;
for(err = &_error_http[i]; /*轮询查找类型匹配的错误类型*/
err->error_code != wctl->conn.con_res.status;
i++)
;
if(err-> error_code!= 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-> error_code,
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-> error_code; /*错误代码*/
}