HttpService.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef HTTP_SERVICE_H_
  2. #define HTTP_SERVICE_H_
  3. #include <string>
  4. #include <map>
  5. #include <list>
  6. #include <memory>
  7. #include "hexport.h"
  8. #include "HttpMessage.h"
  9. #define DEFAULT_BASE_URL "/v1/api"
  10. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  11. #define DEFAULT_HOME_PAGE "index.html"
  12. /*
  13. * @param[in] req: parsed structured http request
  14. * @param[out] res: structured http response
  15. * @return 0: handle continue
  16. * http_status_code: handle done
  17. */
  18. typedef int (*http_api_handler)(HttpRequest* req, HttpResponse* res);
  19. struct http_method_handler {
  20. http_method method;
  21. http_api_handler handler;
  22. http_method_handler(http_method m = HTTP_POST, http_api_handler h = NULL) {
  23. method = m;
  24. handler = h;
  25. }
  26. };
  27. // method => http_api_handler
  28. typedef std::list<http_method_handler> http_method_handlers;
  29. // path => http_method_handlers
  30. typedef std::map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  31. struct HV_EXPORT HttpService {
  32. // preprocessor -> api -> web -> postprocessor
  33. http_api_handler preprocessor;
  34. http_api_handler postprocessor;
  35. // api service (that is http.APIServer)
  36. std::string base_url;
  37. http_api_handlers api_handlers;
  38. // web service (that is http.FileServer)
  39. std::string document_root;
  40. std::string home_page;
  41. std::string error_page;
  42. // indexof service (that is http.DirectoryServer)
  43. std::string index_of;
  44. HttpService() {
  45. preprocessor = NULL;
  46. postprocessor = NULL;
  47. // base_url = DEFAULT_BASE_URL;
  48. document_root = DEFAULT_DOCUMENT_ROOT;
  49. home_page = DEFAULT_HOME_PAGE;
  50. }
  51. void AddApi(const char* path, http_method method, http_api_handler handler);
  52. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  53. int GetApi(const char* url, http_method method, http_api_handler* handler);
  54. // RESTful API /:field/ => req->query_params["field"]
  55. int GetApi(HttpRequest* req, http_api_handler* handler);
  56. // github.com/gin-gonic/gin
  57. void Handle(const char* httpMethod, const char* relativePath, http_api_handler handlerFunc) {
  58. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc);
  59. }
  60. void HEAD(const char* relativePath, http_api_handler handlerFunc) {
  61. Handle("HEAD", relativePath, handlerFunc);
  62. }
  63. void GET(const char* relativePath, http_api_handler handlerFunc) {
  64. Handle("GET", relativePath, handlerFunc);
  65. }
  66. void POST(const char* relativePath, http_api_handler handlerFunc) {
  67. Handle("POST", relativePath, handlerFunc);
  68. }
  69. void PUT(const char* relativePath, http_api_handler handlerFunc) {
  70. Handle("PUT", relativePath, handlerFunc);
  71. }
  72. void DELETE(const char* relativePath, http_api_handler handlerFunc) {
  73. Handle("DELETE", relativePath, handlerFunc);
  74. }
  75. void PATCH(const char* relativePath, http_api_handler handlerFunc) {
  76. Handle("PATCH", relativePath, handlerFunc);
  77. }
  78. void Any(const char* relativePath, http_api_handler handlerFunc) {
  79. Handle("HEAD", relativePath, handlerFunc);
  80. Handle("GET", relativePath, handlerFunc);
  81. Handle("POST", relativePath, handlerFunc);
  82. Handle("PUT", relativePath, handlerFunc);
  83. Handle("DELETE", relativePath, handlerFunc);
  84. Handle("PATCH", relativePath, handlerFunc);
  85. }
  86. };
  87. #endif // HTTP_SERVICE_H_