http_api_test.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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("/json", POST, http_api_json) \
  10. XXX("/mp", POST, http_api_mp) \
  11. XXX("/kv", POST, http_api_kv) \
  12. XXX("/grpc", POST, http_api_grpc) \
  13. XXX("/group/:group_name/user/:user_id", DELETE, http_api_restful) \
  14. inline int http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
  15. //printf("%s\n", req->Dump(true, true).c_str());
  16. req->ParseBody();
  17. return 0;
  18. }
  19. inline int http_api_postprocessor(HttpRequest* req, HttpResponse* res) {
  20. res->DumpBody();
  21. //printf("%s\n", res->Dump(true, true).c_str());
  22. return 0;
  23. }
  24. inline int http_api_hello(HttpRequest* req, HttpResponse* res) {
  25. res->body = "hello";
  26. return 0;
  27. }
  28. inline int http_api_query(HttpRequest* req, HttpResponse* res) {
  29. res->kv = req->query_params;
  30. return 0;
  31. }
  32. inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
  33. res->content_type = req->content_type;
  34. res->body = req->body;
  35. return 0;
  36. }
  37. inline int http_api_json(HttpRequest* req, HttpResponse* res) {
  38. if (req->content_type != APPLICATION_JSON) {
  39. res->status_code = HTTP_STATUS_BAD_REQUEST;
  40. return 0;
  41. }
  42. res->json = req->json;
  43. return 0;
  44. }
  45. inline int http_api_mp(HttpRequest* req, HttpResponse* res) {
  46. if (req->content_type != MULTIPART_FORM_DATA) {
  47. res->status_code = HTTP_STATUS_BAD_REQUEST;
  48. return 0;
  49. }
  50. res->mp = req->mp;
  51. return 0;
  52. }
  53. inline int http_api_kv(HttpRequest*req, HttpResponse* res) {
  54. if (req->content_type != APPLICATION_URLENCODED) {
  55. res->status_code = HTTP_STATUS_BAD_REQUEST;
  56. return 0;
  57. }
  58. res->kv = req->kv;
  59. return 0;
  60. }
  61. inline int http_api_grpc(HttpRequest* req, HttpResponse* res) {
  62. if (req->content_type != APPLICATION_GRPC) {
  63. res->status_code = HTTP_STATUS_BAD_REQUEST;
  64. return 0;
  65. }
  66. // parse protobuf: ParseFromString
  67. // req->body;
  68. // serailize protobuf: SerializeAsString
  69. // res->body;
  70. return 0;
  71. }
  72. inline int http_api_restful(HttpRequest*req, HttpResponse* res) {
  73. // RESTful /:field/ => req->query_params
  74. res->kv = req->query_params;
  75. return 0;
  76. }
  77. #endif // HTTP_API_TEST_H_