handler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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* res) {
  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(res, HTTP_STATUS_BAD_REQUEST);
  12. // }
  13. req->ParseBody();
  14. res->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(res, 10011, "Miss token");
  21. return HTTP_STATUS_UNAUTHORIZED;
  22. }
  23. else if (strcmp(token.c_str(), "abcdefg") != 0) {
  24. response_status(res, 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* res) {
  33. // printf("%s\n", res->Dump(true, true).c_str());
  34. return 0;
  35. }
  36. static int sleep(HttpRequest* req, HttpResponse* res) {
  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. res->Set("start_time", start_time);
  47. res->Set("end_time", end_time);
  48. response_status(res, 0, "OK");
  49. return 200;
  50. }
  51. static int query(HttpRequest* req, HttpResponse* res) {
  52. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  53. // ?query => HttpRequest::query_params
  54. for (auto& param : req->query_params) {
  55. res->Set(param.first.c_str(), param.second);
  56. }
  57. response_status(res, 0, "OK");
  58. return 200;
  59. }
  60. static int kv(HttpRequest* req, HttpResponse* res) {
  61. if (req->content_type != APPLICATION_URLENCODED) {
  62. return response_status(res, HTTP_STATUS_BAD_REQUEST);
  63. }
  64. res->content_type = APPLICATION_URLENCODED;
  65. res->kv = req->kv;
  66. return 200;
  67. }
  68. static int json(HttpRequest* req, HttpResponse* res) {
  69. if (req->content_type != APPLICATION_JSON) {
  70. return response_status(res, HTTP_STATUS_BAD_REQUEST);
  71. }
  72. res->content_type = APPLICATION_JSON;
  73. res->json = req->json;
  74. return 200;
  75. }
  76. static int form(HttpRequest* req, HttpResponse* res) {
  77. if (req->content_type != MULTIPART_FORM_DATA) {
  78. return response_status(res, HTTP_STATUS_BAD_REQUEST);
  79. }
  80. res->content_type = MULTIPART_FORM_DATA;
  81. res->form = req->form;
  82. return 200;
  83. }
  84. static int test(HttpRequest* req, HttpResponse* res) {
  85. // bool b = req->Get<bool>("bool");
  86. // int64_t n = req->Get<int64_t>("int");
  87. // double f = req->Get<double>("float");
  88. bool b = req->GetBool("bool");
  89. int64_t n = req->GetInt("int");
  90. double f = req->GetFloat("float");
  91. string str = req->GetString("string");
  92. res->content_type = req->content_type;
  93. res->Set("bool", b);
  94. res->Set("int", n);
  95. res->Set("float", f);
  96. res->Set("string", str);
  97. response_status(res, 0, "OK");
  98. return 200;
  99. }
  100. static int grpc(HttpRequest* req, HttpResponse* res) {
  101. if (req->content_type != APPLICATION_GRPC) {
  102. return response_status(res, HTTP_STATUS_BAD_REQUEST);
  103. }
  104. // parse protobuf
  105. // ParseFromString(req->body);
  106. // res->content_type = APPLICATION_GRPC;
  107. // serailize protobuf
  108. // res->body = SerializeAsString(xxx);
  109. response_status(res, 0, "OK");
  110. return 200;
  111. }
  112. static int restful(HttpRequest* req, HttpResponse* res) {
  113. // RESTful /:field/ => HttpRequest::query_params
  114. // path=/group/:group_name/user/:user_id
  115. // string group_name = req->GetParam("group_name");
  116. // string user_id = req->GetParam("user_id");
  117. for (auto& param : req->query_params) {
  118. res->Set(param.first.c_str(), param.second);
  119. }
  120. response_status(res, 0, "OK");
  121. return 200;
  122. }
  123. static int login(HttpRequest* req, HttpResponse* res) {
  124. string username = req->GetString("username");
  125. string password = req->GetString("password");
  126. if (username.empty() || password.empty()) {
  127. response_status(res, 10001, "Miss username or password");
  128. return HTTP_STATUS_BAD_REQUEST;
  129. }
  130. else if (strcmp(username.c_str(), "admin") != 0) {
  131. response_status(res, 10002, "Username not exist");
  132. return HTTP_STATUS_BAD_REQUEST;
  133. }
  134. else if (strcmp(password.c_str(), "123456") != 0) {
  135. response_status(res, 10003, "Password wrong");
  136. return HTTP_STATUS_BAD_REQUEST;
  137. }
  138. else {
  139. res->Set("token", "abcdefg");
  140. response_status(res, 0, "OK");
  141. return HTTP_STATUS_OK;
  142. }
  143. }
  144. static int upload(HttpRequest* req, HttpResponse* res) {
  145. if (req->content_type != MULTIPART_FORM_DATA) {
  146. return response_status(res, HTTP_STATUS_BAD_REQUEST);
  147. }
  148. FormData file = req->form["file"];
  149. string filepath("html/uploads/");
  150. filepath += file.filename;
  151. FILE* fp = fopen(filepath.c_str(), "w");
  152. if (fp) {
  153. fwrite(file.content.data(), 1, file.content.size(), fp);
  154. fclose(fp);
  155. }
  156. response_status(res, 0, "OK");
  157. return 200;
  158. }
  159. private:
  160. static int response_status(HttpResponse* res, int code = 200, const char* message = NULL) {
  161. res->Set("code", code);
  162. if (message == NULL) message = http_status_str((enum http_status)code);
  163. res->Set("message", message);
  164. res->DumpBody();
  165. return code;
  166. }
  167. };
  168. #endif // HV_HTTPD_HANDLER_H