HttpService.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef HV_HTTP_SERVICE_H_
  2. #define HV_HTTP_SERVICE_H_
  3. #include <string>
  4. #include <map>
  5. #include <unordered_map>
  6. #include <list>
  7. #include <memory>
  8. #include <functional>
  9. #include "hexport.h"
  10. #include "HttpMessage.h"
  11. #include "HttpResponseWriter.h"
  12. #include "HttpContext.h"
  13. #define DEFAULT_BASE_URL "/api/v1"
  14. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  15. #define DEFAULT_HOME_PAGE "index.html"
  16. #define DEFAULT_ERROR_PAGE "error.html"
  17. #define DEFAULT_INDEXOF_DIR "/downloads/"
  18. #define DEFAULT_KEEPALIVE_TIMEOUT 75000 // ms
  19. /*
  20. * @param[in] req: parsed structured http request
  21. * @param[out] resp: structured http response
  22. * @return 0: handle unfinished
  23. * http_status_code: handle done
  24. */
  25. #define HTTP_STATUS_UNFINISHED 0
  26. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
  27. typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
  28. typedef std::function<int(const HttpContextPtr& ctx)> http_handler;
  29. struct http_method_handler {
  30. http_method method;
  31. http_sync_handler sync_handler;
  32. http_async_handler async_handler;
  33. http_handler handler;
  34. http_method_handler(http_method m = HTTP_POST,
  35. http_sync_handler s = NULL,
  36. http_async_handler a = NULL,
  37. http_handler h = NULL)
  38. {
  39. method = m;
  40. sync_handler = std::move(s);
  41. async_handler = std::move(a);
  42. handler = std::move(h);
  43. }
  44. };
  45. // method => http_method_handler
  46. typedef std::list<http_method_handler> http_method_handlers;
  47. // path => http_method_handlers
  48. typedef std::unordered_map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  49. namespace hv {
  50. struct HV_EXPORT HttpService {
  51. // preprocessor -> processor -> postprocessor
  52. http_sync_handler preprocessor;
  53. // processor: api_handlers -> staticHandler -> errorHandler
  54. http_handler processor;
  55. http_sync_handler postprocessor;
  56. // api service (that is http.APIServer)
  57. std::string base_url;
  58. http_api_handlers api_handlers;
  59. // file service (that is http.FileServer)
  60. http_handler staticHandler;
  61. http_handler largeFileHandler;
  62. std::string document_root;
  63. std::string home_page;
  64. std::string error_page;
  65. // indexof service (that is http.DirectoryServer)
  66. std::string index_of;
  67. http_handler errorHandler;
  68. // options
  69. int keepalive_timeout;
  70. HttpService() {
  71. preprocessor = NULL;
  72. processor = NULL;
  73. postprocessor = NULL;
  74. // base_url = DEFAULT_BASE_URL;
  75. staticHandler = NULL;
  76. largeFileHandler = NULL;
  77. document_root = DEFAULT_DOCUMENT_ROOT;
  78. home_page = DEFAULT_HOME_PAGE;
  79. // error_page = DEFAULT_ERROR_PAGE;
  80. // index_of = DEFAULT_INDEXOF_DIR;
  81. errorHandler = NULL;
  82. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  83. }
  84. void AddApi(const char* path, http_method method,
  85. http_sync_handler sync_handler = NULL,
  86. http_async_handler async_handler = NULL,
  87. http_handler handler = NULL);
  88. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  89. int GetApi(const char* url, http_method method,
  90. http_sync_handler* sync_handler = NULL,
  91. http_async_handler* async_handler = NULL,
  92. http_handler* handler = NULL);
  93. // RESTful API /:field/ => req->query_params["field"]
  94. int GetApi(HttpRequest* req,
  95. http_sync_handler* sync_handler = NULL,
  96. http_async_handler* async_handler = NULL,
  97. http_handler* handler = NULL);
  98. hv::StringList Paths() {
  99. hv::StringList paths;
  100. for (auto& pair : api_handlers) {
  101. paths.emplace_back(pair.first);
  102. }
  103. return paths;
  104. }
  105. // github.com/gin-gonic/gin
  106. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  107. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc, NULL, NULL);
  108. }
  109. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  110. AddApi(relativePath, http_method_enum(httpMethod), NULL, handlerFunc, NULL);
  111. }
  112. void Handle(const char* httpMethod, const char* relativePath, http_handler handlerFunc) {
  113. AddApi(relativePath, http_method_enum(httpMethod), NULL, NULL, handlerFunc);
  114. }
  115. // HEAD
  116. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  117. Handle("HEAD", relativePath, handlerFunc);
  118. }
  119. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  120. Handle("HEAD", relativePath, handlerFunc);
  121. }
  122. void HEAD(const char* relativePath, http_handler handlerFunc) {
  123. Handle("HEAD", relativePath, handlerFunc);
  124. }
  125. // GET
  126. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  127. Handle("GET", relativePath, handlerFunc);
  128. }
  129. void GET(const char* relativePath, http_async_handler handlerFunc) {
  130. Handle("GET", relativePath, handlerFunc);
  131. }
  132. void GET(const char* relativePath, http_handler handlerFunc) {
  133. Handle("GET", relativePath, handlerFunc);
  134. }
  135. // POST
  136. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  137. Handle("POST", relativePath, handlerFunc);
  138. }
  139. void POST(const char* relativePath, http_async_handler handlerFunc) {
  140. Handle("POST", relativePath, handlerFunc);
  141. }
  142. void POST(const char* relativePath, http_handler handlerFunc) {
  143. Handle("POST", relativePath, handlerFunc);
  144. }
  145. // PUT
  146. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  147. Handle("PUT", relativePath, handlerFunc);
  148. }
  149. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  150. Handle("PUT", relativePath, handlerFunc);
  151. }
  152. void PUT(const char* relativePath, http_handler handlerFunc) {
  153. Handle("PUT", relativePath, handlerFunc);
  154. }
  155. // DELETE
  156. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  157. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  158. Handle("DELETE", relativePath, handlerFunc);
  159. }
  160. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  161. Handle("DELETE", relativePath, handlerFunc);
  162. }
  163. void Delete(const char* relativePath, http_handler handlerFunc) {
  164. Handle("DELETE", relativePath, handlerFunc);
  165. }
  166. // PATCH
  167. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  168. Handle("PATCH", relativePath, handlerFunc);
  169. }
  170. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  171. Handle("PATCH", relativePath, handlerFunc);
  172. }
  173. void PATCH(const char* relativePath, http_handler handlerFunc) {
  174. Handle("PATCH", relativePath, handlerFunc);
  175. }
  176. // Any
  177. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  178. Handle("HEAD", relativePath, handlerFunc);
  179. Handle("GET", relativePath, handlerFunc);
  180. Handle("POST", relativePath, handlerFunc);
  181. Handle("PUT", relativePath, handlerFunc);
  182. Handle("DELETE", relativePath, handlerFunc);
  183. Handle("PATCH", relativePath, handlerFunc);
  184. }
  185. void Any(const char* relativePath, http_async_handler handlerFunc) {
  186. Handle("HEAD", relativePath, handlerFunc);
  187. Handle("GET", relativePath, handlerFunc);
  188. Handle("POST", relativePath, handlerFunc);
  189. Handle("PUT", relativePath, handlerFunc);
  190. Handle("DELETE", relativePath, handlerFunc);
  191. Handle("PATCH", relativePath, handlerFunc);
  192. }
  193. void Any(const char* relativePath, http_handler handlerFunc) {
  194. Handle("HEAD", relativePath, handlerFunc);
  195. Handle("GET", relativePath, handlerFunc);
  196. Handle("POST", relativePath, handlerFunc);
  197. Handle("PUT", relativePath, handlerFunc);
  198. Handle("DELETE", relativePath, handlerFunc);
  199. Handle("PATCH", relativePath, handlerFunc);
  200. }
  201. };
  202. }
  203. #endif // HV_HTTP_SERVICE_H_