http_api_test.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 int 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. return 0;
  16. }
  17. inline int http_api_postprocessor(HttpRequest* req, HttpResponse* res) {
  18. res->dump_body();
  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_kv(HttpRequest*req, HttpResponse* res) {
  39. if (req->content_type != X_WWW_FORM_URLENCODED) {
  40. res->status_code = HTTP_STATUS_BAD_REQUEST;
  41. return 0;
  42. }
  43. res->kv = req->kv;
  44. return 0;
  45. }
  46. inline int http_api_query(HttpRequest* req, HttpResponse* res) {
  47. res->kv = req->query_params;
  48. return 0;
  49. }
  50. inline int http_api_echo(HttpRequest* req, HttpResponse* res) {
  51. res->content_type = req->content_type;
  52. res->body = req->body;
  53. return 0;
  54. }
  55. #endif // HTTP_API_TEST_H_