http_api_test.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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("/grpc", POST, http_api_grpc) \
  13. \
  14. XXX("/test", POST, http_api_test) \
  15. XXX("/group/:group_name/user/:user_id", DELETE, http_api_restful) \
  16. inline void response_status(HttpResponse* res, int code, const char* message) {
  17. res->Set("code", code);
  18. res->Set("message", message);
  19. }
  20. inline int http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
  21. //printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
  22. //printf("%s\n", req->Dump(true, true).c_str());
  23. req->ParseBody();
  24. res->content_type = APPLICATION_JSON;
  25. return 0;
  26. }
  27. inline int http_api_postprocessor(HttpRequest* req, HttpResponse* res) {
  28. res->DumpBody();
  29. //printf("%s\n", res->Dump(true, true).c_str());
  30. return 0;
  31. }
  32. inline int http_api_hello(HttpRequest* req, HttpResponse* res) {
  33. res->body = "hello";
  34. return 0;
  35. }
  36. inline int http_api_query(HttpRequest* req, HttpResponse* res) {
  37. res->kv = req->query_params;
  38. return 0;
  39. }
  40. inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
  41. res->content_type = req->content_type;
  42. res->body = req->body;
  43. return 0;
  44. }
  45. inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
  46. if (req->content_type != APPLICATION_URLENCODED) {
  47. res->status_code = HTTP_STATUS_BAD_REQUEST;
  48. return 0;
  49. }
  50. res->content_type = APPLICATION_URLENCODED;
  51. res->kv = req->kv;
  52. return 0;
  53. }
  54. inline int http_api_json(HttpRequest* req, HttpResponse* res) {
  55. if (req->content_type != APPLICATION_JSON) {
  56. res->status_code = HTTP_STATUS_BAD_REQUEST;
  57. return 0;
  58. }
  59. res->content_type = APPLICATION_JSON;
  60. res->json = req->json;
  61. return 0;
  62. }
  63. inline int http_api_form(HttpRequest* req, HttpResponse* res) {
  64. if (req->content_type != MULTIPART_FORM_DATA) {
  65. res->status_code = HTTP_STATUS_BAD_REQUEST;
  66. return 0;
  67. }
  68. res->content_type = MULTIPART_FORM_DATA;
  69. res->form = req->form;
  70. return 0;
  71. }
  72. inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
  73. if (req->content_type != APPLICATION_GRPC) {
  74. res->status_code = HTTP_STATUS_BAD_REQUEST;
  75. return 0;
  76. }
  77. // parse protobuf: ParseFromString
  78. // req->body;
  79. // res->content_type = APPLICATION_GRPC;
  80. // serailize protobuf: SerializeAsString
  81. // res->body;
  82. return 0;
  83. }
  84. inline int http_api_test(HttpRequest* req, HttpResponse* res) {
  85. string str = req->GetValue("string");
  86. int64_t n = req->Get<int64_t>("int");
  87. double f = req->Get<double>("float");
  88. bool b = req->Get<bool>("bool");
  89. res->content_type = req->content_type;
  90. res->Set("string", str);
  91. res->Set("int", n);
  92. res->Set("float", f);
  93. res->Set("bool", b);
  94. response_status(res, 0, "OK");
  95. return 0;
  96. }
  97. inline int http_api_restful(HttpRequest*req, HttpResponse* res) {
  98. // RESTful /:field/ => req->query_params
  99. for (auto& param : req->query_params) {
  100. res->Set(param.first.c_str(), param.second);
  101. }
  102. response_status(res, 0, "Operation completed.");
  103. return 0;
  104. }
  105. #endif // HTTP_API_TEST_H_