1
0

HttpService.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef HV_HTTP_SERVICE_H_
  2. #define HV_HTTP_SERVICE_H_
  3. #include <string>
  4. #include <map>
  5. #include <list>
  6. #include <memory>
  7. #include <functional>
  8. #include "hexport.h"
  9. #include "HttpMessage.h"
  10. #include "HttpResponseWriter.h"
  11. #define DEFAULT_BASE_URL "/api/v1"
  12. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  13. #define DEFAULT_HOME_PAGE "index.html"
  14. #define DEFAULT_ERROR_PAGE "error.html"
  15. /*
  16. * @param[in] req: parsed structured http request
  17. * @param[out] resp: structured http response
  18. * @return 0: handle continue
  19. * http_status_code: handle done
  20. */
  21. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
  22. typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
  23. struct http_method_handler {
  24. http_method method;
  25. http_sync_handler sync_handler;
  26. http_async_handler async_handler;
  27. http_method_handler(http_method m = HTTP_POST,
  28. http_sync_handler s = NULL,
  29. http_async_handler a = NULL)
  30. {
  31. method = m;
  32. sync_handler = s;
  33. async_handler = a;
  34. }
  35. };
  36. // method => http_sync_handler
  37. typedef std::list<http_method_handler> http_method_handlers;
  38. // path => http_method_handlers
  39. typedef std::map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  40. struct HV_EXPORT HttpService {
  41. // preprocessor -> api service -> file service -> indexof service -> postprocessor
  42. http_sync_handler preprocessor;
  43. http_sync_handler postprocessor;
  44. // api service (that is http.APIServer)
  45. std::string base_url;
  46. http_api_handlers api_handlers;
  47. // file service (that is http.FileServer)
  48. std::string document_root;
  49. std::string home_page;
  50. std::string error_page;
  51. // indexof service (that is http.DirectoryServer)
  52. std::string index_of;
  53. HttpService() {
  54. preprocessor = NULL;
  55. postprocessor = NULL;
  56. // base_url = DEFAULT_BASE_URL;
  57. document_root = DEFAULT_DOCUMENT_ROOT;
  58. home_page = DEFAULT_HOME_PAGE;
  59. // error_page = DEFAULT_ERROR_PAGE;
  60. }
  61. void AddApi(const char* path, http_method method, http_sync_handler handler = NULL, http_async_handler async_handler = NULL);
  62. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  63. int GetApi(const char* url, http_method method, http_sync_handler* handler = NULL, http_async_handler* async_handler = NULL);
  64. // RESTful API /:field/ => req->query_params["field"]
  65. int GetApi(HttpRequest* req, http_sync_handler* handler = NULL, http_async_handler* async_handler = NULL);
  66. StringList Paths() {
  67. StringList paths;
  68. for (auto& pair : api_handlers) {
  69. paths.emplace_back(pair.first);
  70. }
  71. return paths;
  72. }
  73. // github.com/gin-gonic/gin
  74. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  75. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc, NULL);
  76. }
  77. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  78. AddApi(relativePath, http_method_enum(httpMethod), NULL, handlerFunc);
  79. }
  80. // HEAD
  81. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  82. Handle("HEAD", relativePath, handlerFunc);
  83. }
  84. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  85. Handle("HEAD", relativePath, handlerFunc);
  86. }
  87. // GET
  88. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  89. Handle("GET", relativePath, handlerFunc);
  90. }
  91. void GET(const char* relativePath, http_async_handler handlerFunc) {
  92. Handle("GET", relativePath, handlerFunc);
  93. }
  94. // POST
  95. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  96. Handle("POST", relativePath, handlerFunc);
  97. }
  98. void POST(const char* relativePath, http_async_handler handlerFunc) {
  99. Handle("POST", relativePath, handlerFunc);
  100. }
  101. // PUT
  102. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  103. Handle("PUT", relativePath, handlerFunc);
  104. }
  105. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  106. Handle("PUT", relativePath, handlerFunc);
  107. }
  108. // DELETE
  109. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  110. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  111. Handle("DELETE", relativePath, handlerFunc);
  112. }
  113. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  114. Handle("DELETE", relativePath, handlerFunc);
  115. }
  116. // PATCH
  117. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  118. Handle("PATCH", relativePath, handlerFunc);
  119. }
  120. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  121. Handle("PATCH", relativePath, handlerFunc);
  122. }
  123. // Any
  124. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  125. Handle("HEAD", relativePath, handlerFunc);
  126. Handle("GET", relativePath, handlerFunc);
  127. Handle("POST", relativePath, handlerFunc);
  128. Handle("PUT", relativePath, handlerFunc);
  129. Handle("DELETE", relativePath, handlerFunc);
  130. Handle("PATCH", relativePath, handlerFunc);
  131. }
  132. void Any(const char* relativePath, http_async_handler handlerFunc) {
  133. Handle("HEAD", relativePath, handlerFunc);
  134. Handle("GET", relativePath, handlerFunc);
  135. Handle("POST", relativePath, handlerFunc);
  136. Handle("PUT", relativePath, handlerFunc);
  137. Handle("DELETE", relativePath, handlerFunc);
  138. Handle("PATCH", relativePath, handlerFunc);
  139. }
  140. };
  141. #endif // HV_HTTP_SERVICE_H_