handler.h 5.6 KB

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