handler.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. req->ParseBody();
  17. resp->content_type = APPLICATION_JSON;
  18. // cors
  19. resp->headers["Access-Control-Allow-Origin"] = "*";
  20. if (req->method == HTTP_OPTIONS) {
  21. resp->headers["Access-Control-Allow-Origin"] = req->GetHeader("Origin", "*");
  22. resp->headers["Access-Control-Allow-Methods"] = req->GetHeader("Access-Control-Request-Method", "OPTIONS, HEAD, GET, POST, PUT, DELETE, PATCH");
  23. resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");
  24. return HTTP_STATUS_NO_CONTENT;
  25. }
  26. #if 0
  27. // authentication sample code
  28. if (strcmp(req->path.c_str(), "/login") != 0) {
  29. string token = req->GetHeader("token");
  30. if (token.empty()) {
  31. response_status(resp, 10011, "Miss token");
  32. return HTTP_STATUS_UNAUTHORIZED;
  33. }
  34. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  35. response_status(resp, 10012, "Token wrong");
  36. return HTTP_STATUS_UNAUTHORIZED;
  37. }
  38. return 0;
  39. }
  40. #endif
  41. return 0;
  42. }
  43. static int postprocessor(HttpRequest* req, HttpResponse* resp) {
  44. // printf("%s\n", resp->Dump(true, true).c_str());
  45. return 0;
  46. }
  47. static int sleep(HttpRequest* req, HttpResponse* resp) {
  48. resp->Set("start_ms", gettimeofday_ms());
  49. std::string strTime = req->GetParam("t");
  50. if (!strTime.empty()) {
  51. int ms = atoi(strTime.c_str());
  52. if (ms > 0) {
  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. hv::setTimeout(ms, [writer](hv::TimerID timerID){
  67. writer->Begin();
  68. HttpResponse* resp = writer->response.get();
  69. resp->Set("end_ms", gettimeofday_ms());
  70. response_status(resp, 0, "OK");
  71. writer->End();
  72. });
  73. }
  74. }
  75. }
  76. static int query(HttpRequest* req, HttpResponse* resp) {
  77. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  78. // ?query => HttpRequest::query_params
  79. for (auto& param : req->query_params) {
  80. resp->Set(param.first.c_str(), param.second);
  81. }
  82. response_status(resp, 0, "OK");
  83. return 200;
  84. }
  85. static int kv(HttpRequest* req, HttpResponse* resp) {
  86. if (req->content_type != APPLICATION_URLENCODED) {
  87. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  88. }
  89. resp->content_type = APPLICATION_URLENCODED;
  90. resp->kv = req->kv;
  91. resp->kv["int"] = hv::to_string(123);
  92. resp->kv["float"] = hv::to_string(3.14);
  93. resp->kv["string"] = "hello";
  94. return 200;
  95. }
  96. static int json(HttpRequest* req, HttpResponse* resp) {
  97. if (req->content_type != APPLICATION_JSON) {
  98. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  99. }
  100. resp->content_type = APPLICATION_JSON;
  101. resp->json = req->json;
  102. resp->json["int"] = 123;
  103. resp->json["float"] = 3.14;
  104. resp->json["string"] = "hello";
  105. return 200;
  106. }
  107. static int form(HttpRequest* req, HttpResponse* resp) {
  108. if (req->content_type != MULTIPART_FORM_DATA) {
  109. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  110. }
  111. resp->content_type = MULTIPART_FORM_DATA;
  112. resp->form = req->form;
  113. resp->form["int"] = 123;
  114. resp->form["float"] = 3.14;
  115. resp->form["string"] = "hello";
  116. // resp->form["file"] = FormData(NULL, "test.jpg");
  117. // resp->UploadFormFile("file", "test.jpg");
  118. return 200;
  119. }
  120. static int test(HttpRequest* req, HttpResponse* resp) {
  121. // bool b = req->Get<bool>("bool");
  122. // int64_t n = req->Get<int64_t>("int");
  123. // double f = req->Get<double>("float");
  124. bool b = req->GetBool("bool");
  125. int64_t n = req->GetInt("int");
  126. double f = req->GetFloat("float");
  127. string str = req->GetString("string");
  128. resp->content_type = req->content_type;
  129. resp->Set("bool", b);
  130. resp->Set("int", n);
  131. resp->Set("float", f);
  132. resp->Set("string", str);
  133. response_status(resp, 0, "OK");
  134. return 200;
  135. }
  136. static int grpc(HttpRequest* req, HttpResponse* resp) {
  137. if (req->content_type != APPLICATION_GRPC) {
  138. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  139. }
  140. // parse protobuf
  141. // ParseFromString(req->body);
  142. // resp->content_type = APPLICATION_GRPC;
  143. // serailize protobuf
  144. // resp->body = SerializeAsString(xxx);
  145. response_status(resp, 0, "OK");
  146. return 200;
  147. }
  148. static int restful(HttpRequest* req, HttpResponse* resp) {
  149. // RESTful /:field/ => HttpRequest::query_params
  150. // path=/group/:group_name/user/:user_id
  151. std::string group_name = req->GetParam("group_name");
  152. std::string user_id = req->GetParam("user_id");
  153. resp->Set("group_name", group_name);
  154. resp->Set("user_id", user_id);
  155. response_status(resp, 0, "OK");
  156. return 200;
  157. }
  158. static int login(HttpRequest* req, HttpResponse* resp) {
  159. string username = req->GetString("username");
  160. string password = req->GetString("password");
  161. if (username.empty() || password.empty()) {
  162. response_status(resp, 10001, "Miss username or password");
  163. return HTTP_STATUS_BAD_REQUEST;
  164. }
  165. else if (strcmp(username.c_str(), "admin") != 0) {
  166. response_status(resp, 10002, "Username not exist");
  167. return HTTP_STATUS_BAD_REQUEST;
  168. }
  169. else if (strcmp(password.c_str(), "123456") != 0) {
  170. response_status(resp, 10003, "Password wrong");
  171. return HTTP_STATUS_BAD_REQUEST;
  172. }
  173. else {
  174. resp->Set("token", "abcdefg");
  175. response_status(resp, 0, "OK");
  176. return HTTP_STATUS_OK;
  177. }
  178. }
  179. static int upload(HttpRequest* req, HttpResponse* resp) {
  180. // return resp->SaveFormFile("file", "html/uploads/test.jpg");
  181. if (req->content_type != MULTIPART_FORM_DATA) {
  182. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  183. }
  184. FormData file = req->form["file"];
  185. if (file.content.empty()) {
  186. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  187. }
  188. string filepath("html/uploads/");
  189. filepath += file.filename;
  190. FILE* fp = fopen(filepath.c_str(), "wb");
  191. if (fp) {
  192. fwrite(file.content.data(), 1, file.content.size(), fp);
  193. fclose(fp);
  194. }
  195. response_status(resp, 0, "OK");
  196. return 200;
  197. }
  198. private:
  199. static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {
  200. resp->Set("code", code);
  201. if (message == NULL) message = http_status_str((enum http_status)code);
  202. resp->Set("message", message);
  203. resp->DumpBody();
  204. return code;
  205. }
  206. };
  207. #endif // HV_HTTPD_HANDLER_H