http_api_test.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef HTTP_API_TEST_H_
  2. #define HTTP_API_TEST_H_
  3. #include "HttpServer.h"
  4. // XXX(path, method, handler)
  5. #define HTTP_API_MAP(XXX) \
  6. XXX("/hello", GET, http_api_hello) \
  7. XXX("/query", GET, http_api_query) \
  8. XXX("/echo", POST, http_api_echo) \
  9. XXX("/kv", POST, http_api_kv) \
  10. XXX("/json", POST, http_api_json) \
  11. XXX("/form", POST, http_api_form) \
  12. XXX("/upload", POST, http_api_upload) \
  13. XXX("/grpc", POST, http_api_grpc) \
  14. \
  15. XXX("/test", POST, http_api_test) \
  16. XXX("/group/:group_name/user/:user_id", DELETE, http_api_restful) \
  17. inline void response_status(HttpResponse* res, int code, const char* message) {
  18. res->Set("code", code);
  19. res->Set("message", message);
  20. }
  21. inline int http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
  22. //printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  23. //printf("%s\n", req->Dump(true, true).c_str());
  24. req->ParseBody();
  25. res->content_type = APPLICATION_JSON;
  26. return 0;
  27. }
  28. inline int http_api_postprocessor(HttpRequest* req, HttpResponse* res) {
  29. res->DumpBody();
  30. //printf("%s\n", res->Dump(true, true).c_str());
  31. return 0;
  32. }
  33. inline int http_api_hello(HttpRequest* req, HttpResponse* res) {
  34. res->body = "hello";
  35. return 0;
  36. }
  37. inline int http_api_query(HttpRequest* req, HttpResponse* res) {
  38. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  39. // ?query => HttpRequest::query_params
  40. for (auto& param : req->query_params) {
  41. res->Set(param.first.c_str(), param.second);
  42. }
  43. response_status(res, 0, "Query completed.");
  44. return 0;
  45. }
  46. inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
  47. res->content_type = req->content_type;
  48. res->body = req->body;
  49. return 0;
  50. }
  51. inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
  52. if (req->content_type != APPLICATION_URLENCODED) {
  53. res->status_code = HTTP_STATUS_BAD_REQUEST;
  54. return 0;
  55. }
  56. res->content_type = APPLICATION_URLENCODED;
  57. res->kv = req->kv;
  58. return 0;
  59. }
  60. inline int http_api_json(HttpRequest* req, HttpResponse* res) {
  61. if (req->content_type != APPLICATION_JSON) {
  62. res->status_code = HTTP_STATUS_BAD_REQUEST;
  63. return 0;
  64. }
  65. res->content_type = APPLICATION_JSON;
  66. res->json = req->json;
  67. return 0;
  68. }
  69. inline int http_api_form(HttpRequest* req, HttpResponse* res) {
  70. if (req->content_type != MULTIPART_FORM_DATA) {
  71. res->status_code = HTTP_STATUS_BAD_REQUEST;
  72. return 0;
  73. }
  74. res->content_type = MULTIPART_FORM_DATA;
  75. res->form = req->form;
  76. return 0;
  77. }
  78. inline int http_api_upload(HttpRequest* req, HttpResponse* res) {
  79. if (req->content_type != MULTIPART_FORM_DATA) {
  80. res->status_code = HTTP_STATUS_BAD_REQUEST;
  81. return 0;
  82. }
  83. FormData file = req->form["file"];
  84. string filepath("html/uploads/");
  85. filepath += file.filename;
  86. FILE* fp = fopen(filepath.c_str(), "w");
  87. if (fp) {
  88. hlogi("Save as %s", filepath.c_str());
  89. fwrite(file.content.data(), 1, file.content.size(), fp);
  90. fclose(fp);
  91. }
  92. response_status(res, 0, "OK");
  93. return 0;
  94. }
  95. inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
  96. if (req->content_type != APPLICATION_GRPC) {
  97. res->status_code = HTTP_STATUS_BAD_REQUEST;
  98. return 0;
  99. }
  100. // parse protobuf: ParseFromString
  101. // req->body;
  102. // res->content_type = APPLICATION_GRPC;
  103. // serailize protobuf: SerializeAsString
  104. // res->body;
  105. return 0;
  106. }
  107. inline int http_api_test(HttpRequest* req, HttpResponse* res) {
  108. string str = req->GetString("string");
  109. //int64_t n = req->Get<int64_t>("int");
  110. //double f = req->Get<double>("float");
  111. //bool b = req->Get<bool>("bool");
  112. int64_t n = req->GetInt("int");
  113. double f = req->GetFloat("float");
  114. bool b = req->GetBool("bool");
  115. res->content_type = req->content_type;
  116. res->Set("string", str);
  117. res->Set("int", n);
  118. res->Set("float", f);
  119. res->Set("bool", b);
  120. response_status(res, 0, "OK");
  121. return 0;
  122. }
  123. inline int http_api_restful(HttpRequest* req, HttpResponse* res) {
  124. // RESTful /:field/ => HttpRequest::query_params
  125. // path=/group/:group_name/user/:user_id
  126. //string group_name = req->GetParam("group_name");
  127. //string user_id = req->GetParam("user_id");
  128. for (auto& param : req->query_params) {
  129. res->Set(param.first.c_str(), param.second);
  130. }
  131. response_status(res, 0, "Operation completed.");
  132. return 0;
  133. }
  134. #endif // HTTP_API_TEST_H_