api_test.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef HTTP_API_TEST_H_
  2. #define HTTP_API_TEST_H_
  3. #include "HttpRequest.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 void http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
  12. //printf("%s\n", req->dump(true, true).c_str());
  13. req->parse_url();
  14. req->parse_body();
  15. }
  16. inline void http_api_postprocessor(HttpRequest* req, HttpResponse* res) {
  17. res->dump_body();
  18. //printf("%s\n", res->dump(true, true).c_str());
  19. }
  20. inline void http_api_json(HttpRequest* req, HttpResponse* res) {
  21. if (req->content_type != APPLICATION_JSON) {
  22. res->status_code = HTTP_STATUS_BAD_REQUEST;
  23. return;
  24. }
  25. res->json = req->json;
  26. }
  27. inline void http_api_mp(HttpRequest* req, HttpResponse* res) {
  28. if (req->content_type != MULTIPART_FORM_DATA) {
  29. res->status_code = HTTP_STATUS_BAD_REQUEST;
  30. return;
  31. }
  32. res->mp = req->mp;
  33. }
  34. inline void http_api_kv(HttpRequest*req, HttpResponse* res) {
  35. if (req->content_type != X_WWW_FORM_URLENCODED) {
  36. res->status_code = HTTP_STATUS_BAD_REQUEST;
  37. return;
  38. }
  39. res->kv = req->kv;
  40. }
  41. inline void http_api_query(HttpRequest* req, HttpResponse* res) {
  42. res->kv = req->query_params;
  43. }
  44. inline void http_api_echo(HttpRequest* req, HttpResponse* res) {
  45. res->content_type = req->content_type;
  46. res->body = req->body;
  47. }
  48. #endif // HTTP_API_TEST_H_