1
0

handler.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef HV_HTTPD_HANDLER_H
  2. #define HV_HTTPD_HANDLER_H
  3. #include "HttpMessage.h"
  4. #include "HttpResponseWriter.h"
  5. #include "htime.h"
  6. #include "EventLoop.h" // import setTimeout, setInterval
  7. class Handler {
  8. public:
  9. // preprocessor => api_handlers => postprocessor
  10. static int preprocessor(HttpRequest* req, HttpResponse* resp) {
  11. // printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  12. // printf("%s\n", req->Dump(true, true).c_str());
  13. // if (req->content_type != APPLICATION_JSON) {
  14. // return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  15. // }
  16. // 解析body字符串到对应结构体(json、form、kv)
  17. req->ParseBody();
  18. // 响应格式默认为application/json
  19. resp->content_type = APPLICATION_JSON;
  20. #if 0
  21. // 前处理中我们可以做一些公共逻辑,如请求统计、请求拦截、API鉴权等,
  22. // 下面是一段简单的Token头校验代码
  23. // authentication sample code
  24. if (strcmp(req->path.c_str(), "/login") != 0) {
  25. string token = req->GetHeader("token");
  26. if (token.empty()) {
  27. response_status(resp, 10011, "Miss token");
  28. // 返回错误码将直接发送响应,不再走后续的处理
  29. return HTTP_STATUS_UNAUTHORIZED;
  30. }
  31. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  32. response_status(resp, 10012, "Token wrong");
  33. return HTTP_STATUS_UNAUTHORIZED;
  34. }
  35. return 0;
  36. }
  37. #endif
  38. // 返回0表示继续走后面的处理流程
  39. return 0;
  40. }
  41. static int postprocessor(HttpRequest* req, HttpResponse* resp) {
  42. // 后处理中我们可以做一些后处理公共逻辑,如响应状态码统计、响应数据加密等
  43. // printf("%s\n", resp->Dump(true, true).c_str());
  44. return 0;
  45. }
  46. static int sleep(HttpRequest* req, HttpResponse* resp) {
  47. resp->Set("start_ms", gettimeofday_ms());
  48. std::string strTime = req->GetParam("t");
  49. if (!strTime.empty()) {
  50. int ms = atoi(strTime.c_str());
  51. if (ms > 0) {
  52. // 该操作会阻塞当前IO线程,仅用做测试接口,实际应用中请勿在回调中做耗时操作
  53. hv_delay(ms);
  54. }
  55. }
  56. resp->Set("end_ms", gettimeofday_ms());
  57. response_status(resp, 0, "OK");
  58. return 200;
  59. }
  60. static void setTimeout(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer) {
  61. writer->response->Set("start_ms", gettimeofday_ms());
  62. std::string strTime = req->GetParam("t");
  63. if (!strTime.empty()) {
  64. int ms = atoi(strTime.c_str());
  65. if (ms > 0) {
  66. // 在回调线程中可直接使用setTimeout/setInterval定时器接口
  67. hv::setTimeout(ms, [writer](hv::TimerID timerID){
  68. writer->Begin();
  69. HttpResponse* resp = writer->response.get();
  70. resp->Set("end_ms", gettimeofday_ms());
  71. response_status(resp, 0, "OK");
  72. writer->End();
  73. });
  74. }
  75. }
  76. }
  77. static int query(HttpRequest* req, HttpResponse* resp) {
  78. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  79. // ?query => HttpRequest::query_params
  80. // URL里携带的请求参数已被解析到query_params数据结构里,可通过GetParam("key")获取
  81. for (auto& param : req->query_params) {
  82. resp->Set(param.first.c_str(), param.second);
  83. }
  84. response_status(resp, 0, "OK");
  85. return 200;
  86. }
  87. static int kv(HttpRequest* req, HttpResponse* resp) {
  88. if (req->content_type != APPLICATION_URLENCODED) {
  89. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  90. }
  91. // 设置响应格式为application/x-www-form-urlencoded
  92. resp->content_type = APPLICATION_URLENCODED;
  93. resp->kv = req->kv;
  94. resp->kv["int"] = hv::to_string(123);
  95. resp->kv["float"] = hv::to_string(3.14);
  96. resp->kv["string"] = "hello";
  97. return 200;
  98. }
  99. static int json(HttpRequest* req, HttpResponse* resp) {
  100. if (req->content_type != APPLICATION_JSON) {
  101. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  102. }
  103. // 设置响应格式为application/json
  104. resp->content_type = APPLICATION_JSON;
  105. resp->json = req->json;
  106. resp->json["int"] = 123;
  107. resp->json["float"] = 3.14;
  108. resp->json["string"] = "hello";
  109. return 200;
  110. }
  111. static int form(HttpRequest* req, HttpResponse* resp) {
  112. if (req->content_type != MULTIPART_FORM_DATA) {
  113. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  114. }
  115. // 设置响应格式为multipart/form-data
  116. resp->content_type = MULTIPART_FORM_DATA;
  117. resp->form = req->form;
  118. resp->form["int"] = 123;
  119. resp->form["float"] = 3.14;
  120. resp->form["string"] = "hello";
  121. // 使用formdata格式传输文件
  122. // resp->form["file"] = FormData(NULL, "test.jpg");
  123. // resp->UploadFormFile("file", "test.jpg");
  124. return 200;
  125. }
  126. // 通过 Get/Set 接口可以 获取/设置 key:value 到对应数据结构体 json/form/kv
  127. static int test(HttpRequest* req, HttpResponse* resp) {
  128. // bool b = req->Get<bool>("bool");
  129. // int64_t n = req->Get<int64_t>("int");
  130. // double f = req->Get<double>("float");
  131. bool b = req->GetBool("bool");
  132. int64_t n = req->GetInt("int");
  133. double f = req->GetFloat("float");
  134. string str = req->GetString("string");
  135. resp->content_type = req->content_type;
  136. resp->Set("bool", b);
  137. resp->Set("int", n);
  138. resp->Set("float", f);
  139. resp->Set("string", str);
  140. response_status(resp, 0, "OK");
  141. return 200;
  142. }
  143. static int grpc(HttpRequest* req, HttpResponse* resp) {
  144. if (req->content_type != APPLICATION_GRPC) {
  145. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  146. }
  147. // 需引入protobuf库序列化/反序列化body
  148. // parse protobuf
  149. // ParseFromString(req->body);
  150. // resp->content_type = APPLICATION_GRPC;
  151. // serailize protobuf
  152. // resp->body = SerializeAsString(xxx);
  153. response_status(resp, 0, "OK");
  154. return 200;
  155. }
  156. static int restful(HttpRequest* req, HttpResponse* resp) {
  157. // RESTful /:field/ => HttpRequest::query_params
  158. // path=/group/:group_name/user/:user_id
  159. // restful风格URL里的参数已被解析到query_params数据结构里,可通过GetParam("key")获取
  160. // string group_name = req->GetParam("group_name");
  161. // string user_id = req->GetParam("user_id");
  162. for (auto& param : req->query_params) {
  163. resp->Set(param.first.c_str(), param.second);
  164. }
  165. response_status(resp, 0, "OK");
  166. return 200;
  167. }
  168. // 登录示例:校验用户名,密码,返回一个token
  169. static int login(HttpRequest* req, HttpResponse* resp) {
  170. string username = req->GetString("username");
  171. string password = req->GetString("password");
  172. if (username.empty() || password.empty()) {
  173. response_status(resp, 10001, "Miss username or password");
  174. return HTTP_STATUS_BAD_REQUEST;
  175. }
  176. else if (strcmp(username.c_str(), "admin") != 0) {
  177. response_status(resp, 10002, "Username not exist");
  178. return HTTP_STATUS_BAD_REQUEST;
  179. }
  180. else if (strcmp(password.c_str(), "123456") != 0) {
  181. response_status(resp, 10003, "Password wrong");
  182. return HTTP_STATUS_BAD_REQUEST;
  183. }
  184. else {
  185. resp->Set("token", "abcdefg");
  186. response_status(resp, 0, "OK");
  187. return HTTP_STATUS_OK;
  188. }
  189. }
  190. // 上传文件示例
  191. static int upload(HttpRequest* req, HttpResponse* resp) {
  192. // return resp->SaveFormFile("file", "html/uploads/test.jpg");
  193. if (req->content_type != MULTIPART_FORM_DATA) {
  194. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  195. }
  196. FormData file = req->form["file"];
  197. if (file.content.empty()) {
  198. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  199. }
  200. string filepath("html/uploads/");
  201. filepath += file.filename;
  202. FILE* fp = fopen(filepath.c_str(), "wb");
  203. if (fp) {
  204. fwrite(file.content.data(), 1, file.content.size(), fp);
  205. fclose(fp);
  206. }
  207. response_status(resp, 0, "OK");
  208. return 200;
  209. }
  210. private:
  211. // 统一的响应格式
  212. static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {
  213. resp->Set("code", code);
  214. if (message == NULL) message = http_status_str((enum http_status)code);
  215. resp->Set("message", message);
  216. resp->DumpBody();
  217. return code;
  218. }
  219. };
  220. #endif // HV_HTTPD_HANDLER_H