handler.h 5.7 KB

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