http_api_test.h 1.7 KB

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