1
0

http_api_test.h 2.0 KB

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