http_api_test.h 2.5 KB

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