HttpService.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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(rhs.sync_handler))
  52. , async_handler(std::move(rhs.async_handler))
  53. , ctx_handler(std::move(rhs.ctx_handler))
  54. , state_handler(std::move(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. // preprocessor -> middleware -> processor -> postprocessor
  95. http_handler preprocessor;
  96. http_handlers middleware;
  97. // processor: pathHandlers -> staticHandler -> errorHandler
  98. http_handler processor;
  99. http_handler postprocessor;
  100. // api service (that is http.ApiServer)
  101. std::string base_url;
  102. http_path_handlers pathHandlers;
  103. // file service (that is http.FileServer)
  104. http_handler staticHandler;
  105. http_handler largeFileHandler;
  106. std::string document_root;
  107. std::string home_page;
  108. std::string error_page;
  109. // nginx: location => root
  110. std::map<std::string, std::string, std::greater<std::string>> staticDirs;
  111. // indexof service (that is http.DirectoryServer)
  112. std::string index_of;
  113. http_handler errorHandler;
  114. // proxy service (that is http.ProxyServer)
  115. // nginx: location => proxy_pass
  116. std::map<std::string, std::string, std::greater<std::string>> proxies;
  117. StringList trustProxies;
  118. StringList noProxies;
  119. int proxy_connect_timeout;
  120. int proxy_read_timeout;
  121. int proxy_write_timeout;
  122. // options
  123. int keepalive_timeout;
  124. int max_file_cache_size; // cache small file
  125. int file_cache_stat_interval; // stat file is modified
  126. int file_cache_expired_time; // remove expired file cache
  127. /*
  128. * @test limit_rate
  129. * @build make examples
  130. * @server bin/httpd -c etc/httpd.conf -s restart -d
  131. * @client bin/wget http://127.0.0.1:8080/downloads/test.zip
  132. */
  133. int limit_rate; // limit send rate, unit: KB/s
  134. unsigned enable_access_log :1;
  135. unsigned enable_forward_proxy :1;
  136. HttpService() {
  137. // base_url = DEFAULT_BASE_URL;
  138. document_root = DEFAULT_DOCUMENT_ROOT;
  139. home_page = DEFAULT_HOME_PAGE;
  140. // error_page = DEFAULT_ERROR_PAGE;
  141. // index_of = DEFAULT_INDEXOF_DIR;
  142. proxy_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
  143. proxy_read_timeout = 0;
  144. proxy_write_timeout = 0;
  145. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  146. max_file_cache_size = MAX_FILE_CACHE_SIZE;
  147. file_cache_stat_interval = DEFAULT_FILE_CACHE_STAT_INTERVAL;
  148. file_cache_expired_time = DEFAULT_FILE_CACHE_EXPIRED_TIME;
  149. limit_rate = -1; // unlimited
  150. enable_access_log = 1;
  151. enable_forward_proxy = 0;
  152. }
  153. void AddRoute(const char* path, http_method method, const http_handler& handler);
  154. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  155. int GetRoute(const char* url, http_method method, http_handler** handler);
  156. // RESTful API /:field/ => req->query_params["field"]
  157. int GetRoute(HttpRequest* req, http_handler** handler);
  158. // Static("/", "/var/www/html")
  159. void Static(const char* path, const char* dir);
  160. // @retval / => /var/www/html/index.html
  161. std::string GetStaticFilepath(const char* path);
  162. // https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
  163. void AllowCORS();
  164. // proxy
  165. void AddTrustProxy(const char* host);
  166. void AddNoProxy(const char* host);
  167. bool IsTrustProxy(const char* host);
  168. // forward proxy
  169. void EnableForwardProxy() { enable_forward_proxy = 1; }
  170. // reverse proxy
  171. // Proxy("/api/v1/", "http://www.httpbin.org/");
  172. void Proxy(const char* path, const char* url);
  173. // @retval /api/v1/test => http://www.httpbin.org/test
  174. std::string GetProxyUrl(const char* path);
  175. hv::StringList Paths() {
  176. hv::StringList paths;
  177. for (auto& pair : pathHandlers) {
  178. paths.emplace_back(pair.first);
  179. }
  180. return paths;
  181. }
  182. // Handler = [ http_sync_handler, http_ctx_handler ]
  183. template<typename Handler>
  184. void Use(Handler handlerFunc) {
  185. middleware.emplace_back(handlerFunc);
  186. }
  187. // Inspired by github.com/gin-gonic/gin
  188. // Handler = [ http_sync_handler, http_async_handler, http_ctx_handler, http_state_handler ]
  189. template<typename Handler>
  190. void Handle(const char* httpMethod, const char* relativePath, Handler handlerFunc) {
  191. AddRoute(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  192. }
  193. // HEAD
  194. template<typename Handler>
  195. void HEAD(const char* relativePath, Handler handlerFunc) {
  196. Handle("HEAD", relativePath, handlerFunc);
  197. }
  198. // GET
  199. template<typename Handler>
  200. void GET(const char* relativePath, Handler handlerFunc) {
  201. Handle("GET", relativePath, handlerFunc);
  202. }
  203. // POST
  204. template<typename Handler>
  205. void POST(const char* relativePath, Handler handlerFunc) {
  206. Handle("POST", relativePath, handlerFunc);
  207. }
  208. // PUT
  209. template<typename Handler>
  210. void PUT(const char* relativePath, Handler handlerFunc) {
  211. Handle("PUT", relativePath, handlerFunc);
  212. }
  213. // DELETE
  214. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  215. template<typename Handler>
  216. void Delete(const char* relativePath, Handler handlerFunc) {
  217. Handle("DELETE", relativePath, handlerFunc);
  218. }
  219. // PATCH
  220. template<typename Handler>
  221. void PATCH(const char* relativePath, Handler handlerFunc) {
  222. Handle("PATCH", relativePath, handlerFunc);
  223. }
  224. // Any
  225. template<typename Handler>
  226. void Any(const char* relativePath, Handler handlerFunc) {
  227. Handle("HEAD", relativePath, handlerFunc);
  228. Handle("GET", relativePath, handlerFunc);
  229. Handle("POST", relativePath, handlerFunc);
  230. Handle("PUT", relativePath, handlerFunc);
  231. Handle("DELETE", relativePath, handlerFunc);
  232. Handle("PATCH", relativePath, handlerFunc);
  233. }
  234. };
  235. }
  236. #endif // HV_HTTP_SERVICE_H_