handler.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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("test.jpg");
  92. return 200;
  93. }
  94. static int test(HttpRequest* req, HttpResponse* resp) {
  95. // bool b = req->Get<bool>("bool");
  96. // int64_t n = req->Get<int64_t>("int");
  97. // double f = req->Get<double>("float");
  98. bool b = req->GetBool("bool");
  99. int64_t n = req->GetInt("int");
  100. double f = req->GetFloat("float");
  101. string str = req->GetString("string");
  102. resp->content_type = req->content_type;
  103. resp->Set("bool", b);
  104. resp->Set("int", n);
  105. resp->Set("float", f);
  106. resp->Set("string", str);
  107. response_status(resp, 0, "OK");
  108. return 200;
  109. }
  110. static int grpc(HttpRequest* req, HttpResponse* resp) {
  111. if (req->content_type != APPLICATION_GRPC) {
  112. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  113. }
  114. // parse protobuf
  115. // ParseFromString(req->body);
  116. // resp->content_type = APPLICATION_GRPC;
  117. // serailize protobuf
  118. // resp->body = SerializeAsString(xxx);
  119. response_status(resp, 0, "OK");
  120. return 200;
  121. }
  122. static int restful(HttpRequest* req, HttpResponse* resp) {
  123. // RESTful /:field/ => HttpRequest::query_params
  124. // path=/group/:group_name/user/:user_id
  125. // string group_name = req->GetParam("group_name");
  126. // string user_id = req->GetParam("user_id");
  127. for (auto& param : req->query_params) {
  128. resp->Set(param.first.c_str(), param.second);
  129. }
  130. response_status(resp, 0, "OK");
  131. return 200;
  132. }
  133. static int login(HttpRequest* req, HttpResponse* resp) {
  134. string username = req->GetString("username");
  135. string password = req->GetString("password");
  136. if (username.empty() || password.empty()) {
  137. response_status(resp, 10001, "Miss username or password");
  138. return HTTP_STATUS_BAD_REQUEST;
  139. }
  140. else if (strcmp(username.c_str(), "admin") != 0) {
  141. response_status(resp, 10002, "Username not exist");
  142. return HTTP_STATUS_BAD_REQUEST;
  143. }
  144. else if (strcmp(password.c_str(), "123456") != 0) {
  145. response_status(resp, 10003, "Password wrong");
  146. return HTTP_STATUS_BAD_REQUEST;
  147. }
  148. else {
  149. resp->Set("token", "abcdefg");
  150. response_status(resp, 0, "OK");
  151. return HTTP_STATUS_OK;
  152. }
  153. }
  154. static int upload(HttpRequest* req, HttpResponse* resp) {
  155. if (req->content_type != MULTIPART_FORM_DATA) {
  156. return response_status(resp, HTTP_STATUS_BAD_REQUEST);
  157. }
  158. FormData file = req->form["file"];
  159. string filepath("html/uploads/");
  160. filepath += file.filename;
  161. FILE* fp = fopen(filepath.c_str(), "w");
  162. if (fp) {
  163. fwrite(file.content.data(), 1, file.content.size(), fp);
  164. fclose(fp);
  165. }
  166. response_status(resp, 0, "OK");
  167. return 200;
  168. }
  169. private:
  170. static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {
  171. resp->Set("code", code);
  172. if (message == NULL) message = http_status_str((enum http_status)code);
  173. resp->Set("message", message);
  174. resp->DumpBody();
  175. return code;
  176. }
  177. };
  178. #endif // HV_HTTPD_HANDLER_H