HttpService.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #ifndef HV_HTTP_SERVICE_H_
  2. #define HV_HTTP_SERVICE_H_
  3. #include <string>
  4. #include <map>
  5. #include <unordered_map>
  6. #include <vector>
  7. #include <list>
  8. #include <memory>
  9. #include <functional>
  10. #include "hexport.h"
  11. #include "HttpMessage.h"
  12. #include "HttpResponseWriter.h"
  13. #include "HttpContext.h"
  14. #define DEFAULT_BASE_URL "/api/v1"
  15. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  16. #define DEFAULT_HOME_PAGE "index.html"
  17. #define DEFAULT_ERROR_PAGE "error.html"
  18. #define DEFAULT_INDEXOF_DIR "/downloads/"
  19. #define DEFAULT_KEEPALIVE_TIMEOUT 75000 // ms
  20. // for FileCache
  21. #define MAX_FILE_CACHE_SIZE (1 << 22) // 4M
  22. #define DEFAULT_FILE_CACHE_STAT_INTERVAL 10 // s
  23. #define DEFAULT_FILE_CACHE_EXPIRED_TIME 60 // s
  24. /*
  25. * @param[in] req: parsed structured http request
  26. * @param[out] resp: structured http response
  27. * @return 0: handle next
  28. * http_status_code: handle done
  29. */
  30. #define HTTP_STATUS_NEXT 0
  31. #define HTTP_STATUS_UNFINISHED 0
  32. // NOTE: http_sync_handler run on IO thread
  33. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
  34. // NOTE: http_async_handler run on hv::async threadpool
  35. typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
  36. // NOTE: http_ctx_handler run on IO thread, you can easily post HttpContextPtr to your consumer thread for processing.
  37. typedef std::function<int(const HttpContextPtr& ctx)> http_ctx_handler;
  38. // NOTE: http_state_handler run on IO thread
  39. typedef std::function<int(const HttpContextPtr& ctx, http_parser_state state, const char* data, size_t size)> http_state_handler;
  40. struct http_handler {
  41. http_sync_handler sync_handler;
  42. http_async_handler async_handler;
  43. http_ctx_handler ctx_handler;
  44. http_state_handler state_handler;
  45. http_handler() {}
  46. http_handler(http_sync_handler fn) : sync_handler(std::move(fn)) {}
  47. http_handler(http_async_handler fn) : async_handler(std::move(fn)) {}
  48. http_handler(http_ctx_handler fn) : ctx_handler(std::move(fn)) {}
  49. http_handler(http_state_handler fn) : state_handler(std::move(fn)) {}
  50. http_handler(const http_handler& rhs)
  51. : sync_handler(std::move(const_cast<http_handler&>(rhs).sync_handler))
  52. , async_handler(std::move(const_cast<http_handler&>(rhs).async_handler))
  53. , ctx_handler(std::move(const_cast<http_handler&>(rhs).ctx_handler))
  54. , state_handler(std::move(const_cast<http_handler&>(rhs).state_handler))
  55. {}
  56. const http_handler& operator=(http_sync_handler fn) {
  57. sync_handler = std::move(fn);
  58. return *this;
  59. }
  60. const http_handler& operator=(http_async_handler fn) {
  61. async_handler = std::move(fn);
  62. return *this;
  63. }
  64. const http_handler& operator=(http_ctx_handler fn) {
  65. ctx_handler = std::move(fn);
  66. return *this;
  67. }
  68. const http_handler& operator=(http_state_handler fn) {
  69. state_handler = std::move(fn);
  70. return *this;
  71. }
  72. bool isNull() {
  73. return sync_handler == NULL &&
  74. async_handler == NULL &&
  75. ctx_handler == NULL;
  76. }
  77. operator bool() {
  78. return !isNull();
  79. }
  80. };
  81. typedef std::vector<http_handler> http_handlers;
  82. struct http_method_handler {
  83. http_method method;
  84. http_handler handler;
  85. http_method_handler() {}
  86. http_method_handler(http_method m, const http_handler& h) : method(m), handler(h) {}
  87. };
  88. // method => http_method_handler
  89. typedef std::list<http_method_handler> http_method_handlers;
  90. // path => http_method_handlers
  91. typedef std::unordered_map<std::string, std::shared_ptr<http_method_handlers>> http_path_handlers;
  92. namespace hv {
  93. struct HV_EXPORT HttpService {
  94. /* handler chain */
  95. // preprocessor -> middleware -> processor -> postprocessor
  96. http_handler preprocessor;
  97. http_handlers middleware;
  98. // processor: pathHandlers -> staticHandler -> errorHandler
  99. http_handler processor;
  100. http_handler postprocessor;
  101. /* API handlers */
  102. std::string base_url;
  103. http_path_handlers pathHandlers;
  104. /* Static file service */
  105. http_handler staticHandler;
  106. http_handler largeFileHandler;
  107. std::string document_root;
  108. std::string home_page;
  109. std::string error_page;
  110. // nginx: location => root
  111. std::map<std::string, std::string, std::greater<std::string>> staticDirs;
  112. /* Indexof directory service */
  113. std::string index_of;
  114. http_handler errorHandler;
  115. /* Proxy service */
  116. /* Reverse proxy service */
  117. // nginx: location => proxy_pass
  118. std::map<std::string, std::string, std::greater<std::string>> proxies;
  119. /* Forward proxy service */
  120. StringList trustProxies;
  121. StringList noProxies;
  122. int proxy_connect_timeout;
  123. int proxy_read_timeout;
  124. int proxy_write_timeout;
  125. // options
  126. int keepalive_timeout;
  127. int max_file_cache_size; // cache small file
  128. int file_cache_stat_interval; // stat file is modified
  129. int file_cache_expired_time; // remove expired file cache
  130. /*
  131. * @test limit_rate
  132. * @build make examples
  133. * @server bin/httpd -c etc/httpd.conf -s restart -d
  134. * @client bin/wget http://127.0.0.1:8080/downloads/test.zip
  135. */
  136. int limit_rate; // limit send rate, unit: KB/s
  137. unsigned enable_access_log :1;
  138. unsigned enable_forward_proxy :1;
  139. HttpService() {
  140. // base_url = DEFAULT_BASE_URL;
  141. document_root = DEFAULT_DOCUMENT_ROOT;
  142. home_page = DEFAULT_HOME_PAGE;
  143. // error_page = DEFAULT_ERROR_PAGE;
  144. // index_of = DEFAULT_INDEXOF_DIR;
  145. proxy_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
  146. proxy_read_timeout = 0;
  147. proxy_write_timeout = 0;
  148. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  149. max_file_cache_size = MAX_FILE_CACHE_SIZE;
  150. file_cache_stat_interval = DEFAULT_FILE_CACHE_STAT_INTERVAL;
  151. file_cache_expired_time = DEFAULT_FILE_CACHE_EXPIRED_TIME;
  152. limit_rate = -1; // unlimited
  153. enable_access_log = 1;
  154. enable_forward_proxy = 0;
  155. }
  156. void AddRoute(const char* path, http_method method, const http_handler& handler);
  157. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  158. int GetRoute(const char* url, http_method method, http_handler** handler);
  159. // RESTful API /:field/ => req->query_params["field"]
  160. int GetRoute(HttpRequest* req, http_handler** handler);
  161. // Static("/", "/var/www/html")
  162. void Static(const char* path, const char* dir);
  163. // @retval / => /var/www/html/index.html
  164. std::string GetStaticFilepath(const char* path);
  165. // https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
  166. void AllowCORS();
  167. // proxy
  168. // forward proxy
  169. void EnableForwardProxy() { enable_forward_proxy = 1; }
  170. void AddTrustProxy(const char* host);
  171. void AddNoProxy(const char* host);
  172. bool IsTrustProxy(const char* host);
  173. // reverse proxy
  174. // Proxy("/api/v1/", "http://www.httpbin.org/");
  175. void Proxy(const char* path, const char* url);
  176. // @retval /api/v1/test => http://www.httpbin.org/test
  177. std::string GetProxyUrl(const char* path);
  178. hv::StringList Paths() {
  179. hv::StringList paths;
  180. for (auto& pair : pathHandlers) {
  181. paths.emplace_back(pair.first);
  182. }
  183. return paths;
  184. }
  185. // Handler = [ http_sync_handler, http_ctx_handler ]
  186. template<typename Handler>
  187. void Use(Handler handlerFunc) {
  188. middleware.emplace_back(handlerFunc);
  189. }
  190. // Inspired by github.com/gin-gonic/gin
  191. // Handler = [ http_sync_handler, http_async_handler, http_ctx_handler, http_state_handler ]
  192. template<typename Handler>
  193. void Handle(const char* httpMethod, const char* relativePath, Handler handlerFunc) {
  194. AddRoute(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  195. }
  196. // HEAD
  197. template<typename Handler>
  198. void HEAD(const char* relativePath, Handler handlerFunc) {
  199. Handle("HEAD", relativePath, handlerFunc);
  200. }
  201. // GET
  202. template<typename Handler>
  203. void GET(const char* relativePath, Handler handlerFunc) {
  204. Handle("GET", relativePath, handlerFunc);
  205. }
  206. // POST
  207. template<typename Handler>
  208. void POST(const char* relativePath, Handler handlerFunc) {
  209. Handle("POST", relativePath, handlerFunc);
  210. }
  211. // PUT
  212. template<typename Handler>
  213. void PUT(const char* relativePath, Handler handlerFunc) {
  214. Handle("PUT", relativePath, handlerFunc);
  215. }
  216. // DELETE
  217. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  218. template<typename Handler>
  219. void Delete(const char* relativePath, Handler handlerFunc) {
  220. Handle("DELETE", relativePath, handlerFunc);
  221. }
  222. // PATCH
  223. template<typename Handler>
  224. void PATCH(const char* relativePath, Handler handlerFunc) {
  225. Handle("PATCH", relativePath, handlerFunc);
  226. }
  227. // Any
  228. template<typename Handler>
  229. void Any(const char* relativePath, Handler handlerFunc) {
  230. Handle("HEAD", relativePath, handlerFunc);
  231. Handle("GET", relativePath, handlerFunc);
  232. Handle("POST", relativePath, handlerFunc);
  233. Handle("PUT", relativePath, handlerFunc);
  234. Handle("DELETE", relativePath, handlerFunc);
  235. Handle("PATCH", relativePath, handlerFunc);
  236. }
  237. };
  238. }
  239. #endif // HV_HTTP_SERVICE_H_