http_api_test.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // 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, "Query completed.");
  43. return 0;
  44. }
  45. inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
  46. res->content_type = req->content_type;
  47. res->body = req->body;
  48. return 0;
  49. }
  50. inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
  51. if (req->content_type != APPLICATION_URLENCODED) {
  52. res->status_code = HTTP_STATUS_BAD_REQUEST;
  53. return 0;
  54. }
  55. res->content_type = APPLICATION_URLENCODED;
  56. res->kv = req->kv;
  57. return 0;
  58. }
  59. inline int http_api_json(HttpRequest* req, HttpResponse* res) {
  60. if (req->content_type != APPLICATION_JSON) {
  61. res->status_code = HTTP_STATUS_BAD_REQUEST;
  62. return 0;
  63. }
  64. res->content_type = APPLICATION_JSON;
  65. res->json = req->json;
  66. return 0;
  67. }
  68. inline int http_api_form(HttpRequest* req, HttpResponse* res) {
  69. if (req->content_type != MULTIPART_FORM_DATA) {
  70. res->status_code = HTTP_STATUS_BAD_REQUEST;
  71. return 0;
  72. }
  73. res->content_type = MULTIPART_FORM_DATA;
  74. res->form = req->form;
  75. return 0;
  76. }
  77. inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
  78. if (req->content_type != APPLICATION_GRPC) {
  79. res->status_code = HTTP_STATUS_BAD_REQUEST;
  80. return 0;
  81. }
  82. // parse protobuf: ParseFromString
  83. // req->body;
  84. // res->content_type = APPLICATION_GRPC;
  85. // serailize protobuf: SerializeAsString
  86. // res->body;
  87. return 0;
  88. }
  89. inline int http_api_test(HttpRequest* req, HttpResponse* res) {
  90. string str = req->GetString("string");
  91. //int64_t n = req->Get<int64_t>("int");
  92. //double f = req->Get<double>("float");
  93. //bool b = req->Get<bool>("bool");
  94. int64_t n = req->GetInt("int");
  95. double f = req->GetFloat("float");
  96. bool b = req->GetBool("bool");
  97. res->content_type = req->content_type;
  98. res->Set("string", str);
  99. res->Set("int", n);
  100. res->Set("float", f);
  101. res->Set("bool", b);
  102. response_status(res, 0, "OK");
  103. return 0;
  104. }
  105. inline int http_api_restful(HttpRequest*req, HttpResponse* res) {
  106. // RESTful /:field/ => HttpRequest::query_params
  107. // path=/group/:group_name/user/:user_id
  108. //string group_name = req->GetParam("group_name");
  109. //string user_id = req->GetParam("user_id");
  110. for (auto& param : req->query_params) {
  111. res->Set(param.first.c_str(), param.second);
  112. }
  113. response_status(res, 0, "Operation completed.");
  114. return 0;
  115. }
  116. #endif // HTTP_API_TEST_H_