handler.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef HV_HTTPD_HANDLER_H
  2. #define HV_HTTPD_HANDLER_H
  3. #include "HttpMessage.h"
  4. class Handler {
  5. public:
  6. // preprocessor => handler => postprocessor
  7. static int preprocessor(HttpRequest* req, HttpResponse* resp) {
  8. // printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  9. // printf("%s\n", req->Dump(true, true).c_str());
  10. // if (req->content_type != APPLICATION_JSON) {
  11. // return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  12. // }
  13. req->ParseBody();
  14. resp->content_type = APPLICATION_JSON;
  15. #if 0
  16. // authentication sample code
  17. if (strcmp(req->path.c_str(), "/login") != 0) {
  18. string token = req->GetHeader("token");
  19. if (token.empty()) {
  20. response_status(resp, 10011, "Miss token");
  21. return HTTP_STATUS_UNAUTHORIZED;
  22. }
  23. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  24. response_status(resp, 10012, "Token wrong");
  25. return HTTP_STATUS_UNAUTHORIZED;
  26. }
  27. return 0;
  28. }
  29. #endif
  30. return 0;
  31. }
  32. static int postprocessor(HttpRequest* req, HttpResponse* resp) {
  33. // printf("%s\n", resp->Dump(true, true).c_str());
  34. return 0;
  35. }
  36. static int sleep(HttpRequest* req, HttpResponse* resp) {
  37. time_t start_time = time(NULL);
  38. std::string strTime = req->GetParam("t");
  39. if (!strTime.empty()) {
  40. int sec = atoi(strTime.c_str());
  41. if (sec > 0) {
  42. hv_delay(sec*1000);
  43. }
  44. }
  45. time_t end_time = time(NULL);
  46. resp->Set("start_time", start_time);
  47. resp->Set("end_time", end_time);
  48. response_status(resp, 0, "OK");
  49. return 200;
  50. }
  51. static int query(HttpRequest* req, HttpResponse* resp) {
  52. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  53. // ?query => HttpRequest::query_params
  54. for (auto& param : req->query_params) {
  55. resp->Set(param.first.c_str(), param.second);
  56. }
  57. response_status(resp, 0, "OK");
  58. return 200;
  59. }
  60. static int kv(HttpRequest* req, HttpResponse* resp) {
  61. if (req->content_type != APPLICATION_URLENCODED) {
  62. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  63. }
  64. resp->content_type = APPLICATION_URLENCODED;
  65. resp->kv = req->kv;
  66. resp->kv["int"] = hv::to_string(123);
  67. resp->kv["float"] = hv::to_string(3.14);
  68. resp->kv["string"] = "hello";
  69. return 200;
  70. }
  71. static int json(HttpRequest* req, HttpResponse* resp) {
  72. if (req->content_type != APPLICATION_JSON) {
  73. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  74. }
  75. resp->content_type = APPLICATION_JSON;
  76. resp->json = req->json;
  77. resp->json["int"] = 123;
  78. resp->json["float"] = 3.14;
  79. resp->json["string"] = "hello";
  80. return 200;
  81. }
  82. static int form(HttpRequest* req, HttpResponse* resp) {
  83. if (req->content_type != MULTIPART_FORM_DATA) {
  84. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  85. }
  86. resp->content_type = MULTIPART_FORM_DATA;
  87. resp->form = req->form;
  88. resp->form["int"] = 123;
  89. resp->form["float"] = 3.14;
  90. resp->form["float"] = "hello";
  91. // resp->form["file"] = FormData(NULL, "test.jpg");
  92. // resp->UploadFormFile("file", "test.jpg");
  93. return 200;
  94. }
  95. static int test(HttpRequest* req, HttpResponse* resp) {
  96. // bool b = req->Get<bool>("bool");
  97. // int64_t n = req->Get<int64_t>("int");
  98. // double f = req->Get<double>("float");
  99. bool b = req->GetBool("bool");
  100. int64_t n = req->GetInt("int");
  101. double f = req->GetFloat("float");
  102. string str = req->GetString("string");
  103. resp->content_type = req->content_type;
  104. resp->Set("bool", b);
  105. resp->Set("int", n);
  106. resp->Set("float", f);
  107. resp->Set("string", str);
  108. response_status(resp, 0, "OK");
  109. return 200;
  110. }
  111. static int grpc(HttpRequest* req, HttpResponse* resp) {
  112. if (req->content_type != APPLICATION_GRPC) {
  113. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  114. }
  115. // parse protobuf
  116. // ParseFromString(req->body);
  117. // resp->content_type = APPLICATION_GRPC;
  118. // serailize protobuf
  119. // resp->body = SerializeAsString(xxx);
  120. response_status(resp, 0, "OK");
  121. return 200;
  122. }
  123. static int restful(HttpRequest* req, HttpResponse* resp) {
  124. // RESTful /:field/ => HttpRequest::query_params
  125. // path=/group/:group_name/user/:user_id
  126. // string group_name = req->GetParam("group_name");
  127. // string user_id = req->GetParam("user_id");
  128. for (auto& param : req->query_params) {
  129. resp->Set(param.first.c_str(), param.second);
  130. }
  131. response_status(resp, 0, "OK");
  132. return 200;
  133. }
  134. static int login(HttpRequest* req, HttpResponse* resp) {
  135. string username = req->GetString("username");
  136. string password = req->GetString("password");
  137. if (username.empty() || password.empty()) {
  138. response_status(resp, 10001, "Miss username or password");
  139. return HTTP_STATUS_BAD_REQUEST;
  140. }
  141. else if (strcmp(username.c_str(), "admin") != 0) {
  142. response_status(resp, 10002, "Username not exist");
  143. return HTTP_STATUS_BAD_REQUEST;
  144. }
  145. else if (strcmp(password.c_str(), "123456") != 0) {
  146. response_status(resp, 10003, "Password wrong");
  147. return HTTP_STATUS_BAD_REQUEST;
  148. }
  149. else {
  150. resp->Set("token", "abcdefg");
  151. response_status(resp, 0, "OK");
  152. return HTTP_STATUS_OK;
  153. }
  154. }
  155. static int upload(HttpRequest* req, HttpResponse* resp) {
  156. // return resp->SaveFormFile("file", "html/uploads/test.jpg");
  157. if (req->content_type != MULTIPART_FORM_DATA) {
  158. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  159. }
  160. FormData file = req->form["file"];
  161. if (file.content.empty()) {
  162. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  163. }
  164. string filepath("html/uploads/");
  165. filepath += file.filename;
  166. FILE* fp = fopen(filepath.c_str(), "w");
  167. if (fp) {
  168. fwrite(file.content.data(), 1, file.content.size(), fp);
  169. fclose(fp);
  170. }
  171. response_status(resp, 0, "OK");
  172. return 200;
  173. }
  174. private:
  175. static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {
  176. resp->Set("code", code);
  177. if (message == NULL) message = http_status_str((enum http_status)code);
  178. resp->Set("message", message);
  179. resp->DumpBody();
  180. return code;
  181. }
  182. };
  183. #endif // HV_HTTPD_HANDLER_H