Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64220
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 236
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-05 21:34
文章分类

全部博文(21)

文章存档

2015年(21)

我的朋友

分类: LINUX

2015-08-07 23:57:12


点击(此处)折叠或打开

  1. //start from the very beginning,and to create greatness
  2. //@author: Chuangwei Lin
  3. //@E-mail:979951191@qq.com
  4. //@brief: SHTTPD错误处理的实现
  5. #include "lcw_shttpd.h"
  6. //错误代码定义如下
  7. enum
  8. {
  9.     ERROR301, ERROR302, ERROR303, ERROR304, ERROR305, ERROR307,
  10.     ERROR400, ERROR401, ERROR402, ERROR403, ERROR404, ERROR405, ERROR406,
  11.     ERROR407, ERROR408, ERROR409, ERROR410, ERROR411, ERROR412, ERROR413,
  12.     ERROR414, ERROR415, ERROR416, ERROR417,
  13.     ERROR500, ERROR501, ERROR502, ERROR503, ERROR504, ERROR505
  14. };
  15. //全局错误信息结构体
  16. struct error_mine
  17. {
  18.     int error_code;//错误代码
  19.     char *content;//错误信息
  20.     char *msg;//含义
  21.     int status;//状态
  22. };
  23. //全局错误信息数组
  24. struct error_mine _error_http[]=
  25. {
  26.     {ERROR301, "Error: 301", "永久移动",         301},
  27.     {ERROR302, "Error: 302", "创建",             302},
  28.     {ERROR303, "Error: 303", "观察别的部分",     303},
  29.     {ERROR304, "Error: 304", "只读",             304},
  30.     {ERROR305, "Error: 305", "用户代理",         305},
  31.     {ERROR307, "Error: 307", "临时重发",         307},    
  32.     {ERROR400, "Error: 400", "坏请求",             400},
  33.     {ERROR401, "Error: 401", "未授权的",         401},
  34.     {ERROR402, "Error: 402", "必要的支付",         402},
  35.     {ERROR403, "Error: 403", "禁用",             403},
  36.     {ERROR404, "Error: 404", "没找到",             404},
  37.     {ERROR405, "Error: 405", "不允许的方式",     405},
  38.     {ERROR406, "Error: 406", "不接受",             406},
  39.     {ERROR407, "Error: 407", "需要代理验证",     407},
  40.     {ERROR408, "Error: 408", "请求超时",         408},
  41.     {ERROR409, "Error: 409", "冲突",             409},
  42.     {ERROR410, "Error: 410", "停止",             410},
  43.     {ERROR411, "Error: 411", "需要的长度",         411},
  44.     {ERROR412, "Error: 412", "预处理失败",         412},
  45.     {ERROR413, "Error: 413", "请求实体太大",     413},
  46.     {ERROR414, "Error: 414", "请求-URI太大",     414},
  47.     {ERROR415, "Error: 415", "不支持的媒体类型",415},
  48.     {ERROR416, "Error: 416", "请求的范围不满足",416},
  49.     {ERROR417, "Error: 417", "期望失败",         417},
  50.     {ERROR500, "Error: 500", "服务器内部错误", 500},
  51.     {ERROR501, "Error: 501", "不能实现",         501},
  52.     {ERROR502, "Error: 502", "坏网关",             502},
  53.     {ERROR503, "Error: 503", "服务不能实现",     503},
  54.     {ERROR504, "Error: 504", "网关超时",         504},
  55.     {ERROR505, "Error: 505", "HTTP版本不支持", 505}
  56. };


  57. void Error_400(struct worker_ctl* wctl)
  58. {
  59.     ;
  60. }

  61. void Error_403(struct worker_ctl* wctl)
  62. {
  63.     ;
  64. }

  65. void Error_404(struct worker_ctl* wctl)
  66. {
  67.     ;
  68. }

  69. void Error_505(struct worker_ctl* wctl)
  70. {
  71.     ;
  72. }

  73. /******************************************************
  74. 函数名:GenerateErrorMine(struct worker_ctl * wctl)
  75. 参数:
  76. 功能:错误类型生成
  77. *******************************************************/
  78. int GenerateErrorMine(struct worker_ctl * wctl)
  79. {
  80.     struct error_mine *err = NULL;//错误类型
  81.     int i = 0;
  82.     //轮询查找类型匹配的错误类型
  83.     for(err = &_error_http[i];err->status != wctl->conn.con_res.status;i++);//这句感觉怪怪的,如果一直找不到
  84.     
  85.     if(err->status != wctl->conn.con_res.status)
  86.     {
  87.         err = &_error_http[0]; //没有找到的错误类型为第一个
  88.     }
  89.     //构建信息头部
  90.     snprintf(wctl->conn.dres, sizeof(wctl->conn.dres),
  91.         "HTTP/%lu.%lu %d %s\r\n"
  92.         "Content-Type:%s\r\n"
  93.         "Content-Length:%d\r\n"
  94.         "\r\n"
  95.         "%s",
  96.         wctl->conn.con_req.major,
  97.         wctl->conn.con_req.minor,
  98.         err->status,
  99.         err->msg,
  100.         "text/plain",
  101.         strlen(err->content),
  102.         err->content);

  103.     wctl->conn.con_res.cl = strlen(err->content);//内容长度
  104.     wctl->conn.con_res.fd = -1;//无文件可读
  105.     wctl->conn.con_res.status = 400;//错误代码
  106.     
  107.     return 0;
  108. }

阅读(1231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~