handler.h 6.9 KB

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