HttpService.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. // for FileCache
  20. #define MAX_FILE_CACHE_SIZE (1 << 22) // 4M
  21. #define DEFAULT_FILE_CACHE_STAT_INTERVAL 10 // s
  22. #define DEFAULT_FILE_CACHE_EXPIRED_TIME 60 // s
  23. /*
  24. * @param[in] req: parsed structured http request
  25. * @param[out] resp: structured http response
  26. * @return 0: handle unfinished
  27. * http_status_code: handle done
  28. */
  29. #define HTTP_STATUS_UNFINISHED 0
  30. // NOTE: http_sync_handler run on IO thread
  31. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
  32. // NOTE: http_async_handler run on hv::async threadpool
  33. typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
  34. // NOTE: http_ctx_handler run on IO thread, you can easily post HttpContextPtr to your consumer thread for processing.
  35. typedef std::function<int(const HttpContextPtr& ctx)> http_ctx_handler;
  36. // NOTE: http_state_handler run on IO thread
  37. typedef std::function<int(const HttpContextPtr& ctx, http_parser_state state, const char* data, size_t size)> http_state_handler;
  38. struct http_handler {
  39. http_sync_handler sync_handler;
  40. http_async_handler async_handler;
  41. http_ctx_handler ctx_handler;
  42. http_state_handler state_handler;
  43. http_handler() {}
  44. http_handler(http_sync_handler fn) : sync_handler(std::move(fn)) {}
  45. http_handler(http_async_handler fn) : async_handler(std::move(fn)) {}
  46. http_handler(http_ctx_handler fn) : ctx_handler(std::move(fn)) {}
  47. http_handler(http_state_handler fn) : state_handler(std::move(fn)) {}
  48. http_handler(const http_handler& rhs)
  49. : sync_handler(std::move(rhs.sync_handler))
  50. , async_handler(std::move(rhs.async_handler))
  51. , ctx_handler(std::move(rhs.ctx_handler))
  52. , state_handler(std::move(rhs.state_handler))
  53. {}
  54. const http_handler& operator=(http_sync_handler fn) {
  55. sync_handler = std::move(fn);
  56. return *this;
  57. }
  58. const http_handler& operator=(http_async_handler fn) {
  59. async_handler = std::move(fn);
  60. return *this;
  61. }
  62. const http_handler& operator=(http_ctx_handler fn) {
  63. ctx_handler = std::move(fn);
  64. return *this;
  65. }
  66. const http_handler& operator=(http_state_handler fn) {
  67. state_handler = std::move(fn);
  68. return *this;
  69. }
  70. bool isNull() {
  71. return sync_handler == NULL &&
  72. async_handler == NULL &&
  73. ctx_handler == NULL;
  74. }
  75. operator bool() {
  76. return !isNull();
  77. }
  78. };
  79. struct http_method_handler {
  80. http_method method;
  81. http_handler handler;
  82. http_method_handler() {}
  83. http_method_handler(http_method m, const http_handler& h) : method(m), handler(h) {}
  84. };
  85. // method => http_method_handler
  86. typedef std::list<http_method_handler> http_method_handlers;
  87. // path => http_method_handlers
  88. typedef std::unordered_map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  89. namespace hv {
  90. struct HV_EXPORT HttpService {
  91. // preprocessor -> processor -> postprocessor
  92. http_handler preprocessor;
  93. // processor: api_handlers -> staticHandler -> errorHandler
  94. http_handler processor;
  95. http_handler postprocessor;
  96. // api service (that is http.ApiServer)
  97. std::string base_url;
  98. http_api_handlers api_handlers;
  99. // file service (that is http.FileServer)
  100. http_handler staticHandler;
  101. http_handler largeFileHandler;
  102. std::string document_root;
  103. std::string home_page;
  104. std::string error_page;
  105. // nginx: location => root
  106. std::map<std::string, std::string, std::greater<std::string>> staticDirs;
  107. // indexof service (that is http.DirectoryServer)
  108. std::string index_of;
  109. http_handler errorHandler;
  110. // proxy service (that is http.ProxyServer)
  111. // nginx: location => proxy_pass
  112. std::map<std::string, std::string, std::greater<std::string>> proxies;
  113. int proxy_connect_timeout;
  114. int proxy_read_timeout;
  115. int proxy_write_timeout;
  116. // options
  117. int keepalive_timeout;
  118. int max_file_cache_size; // cache small file
  119. int file_cache_stat_interval; // stat file is modified
  120. int file_cache_expired_time; // remove expired file cache
  121. /*
  122. * @test limit_rate
  123. * @build make examples
  124. * @server bin/httpd -c etc/httpd.conf -s restart -d
  125. * @client bin/wget http://127.0.0.1:8080/downloads/test.zip
  126. */
  127. int limit_rate; // limit send rate, unit: KB/s
  128. unsigned allow_cors :1;
  129. unsigned enable_forward_proxy :1;
  130. HttpService() {
  131. // base_url = DEFAULT_BASE_URL;
  132. document_root = DEFAULT_DOCUMENT_ROOT;
  133. home_page = DEFAULT_HOME_PAGE;
  134. // error_page = DEFAULT_ERROR_PAGE;
  135. // index_of = DEFAULT_INDEXOF_DIR;
  136. proxy_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
  137. proxy_read_timeout = 0;
  138. proxy_write_timeout = 0;
  139. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  140. max_file_cache_size = MAX_FILE_CACHE_SIZE;
  141. file_cache_stat_interval = DEFAULT_FILE_CACHE_STAT_INTERVAL;
  142. file_cache_expired_time = DEFAULT_FILE_CACHE_EXPIRED_TIME;
  143. limit_rate = -1; // unlimited
  144. allow_cors = 0;
  145. enable_forward_proxy = 0;
  146. }
  147. void AddApi(const char* path, http_method method, const http_handler& handler);
  148. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  149. int GetApi(const char* url, http_method method, http_handler** handler);
  150. // RESTful API /:field/ => req->query_params["field"]
  151. int GetApi(HttpRequest* req, http_handler** handler);
  152. // Static("/", "/var/www/html")
  153. void Static(const char* path, const char* dir);
  154. // @retval / => /var/www/html/index.html
  155. std::string GetStaticFilepath(const char* path);
  156. // CORS
  157. void AllowCORS() { allow_cors = 1; }
  158. // forward proxy
  159. void EnableForwardProxy() { enable_forward_proxy = 1; }
  160. // reverse proxy
  161. // Proxy("/api/v1/", "http://www.httpbin.org/");
  162. void Proxy(const char* path, const char* url);
  163. // @retval /api/v1/test => http://www.httpbin.org/test
  164. std::string GetProxyUrl(const char* path);
  165. hv::StringList Paths() {
  166. hv::StringList paths;
  167. for (auto& pair : api_handlers) {
  168. paths.emplace_back(pair.first);
  169. }
  170. return paths;
  171. }
  172. // github.com/gin-gonic/gin
  173. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  174. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  175. }
  176. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  177. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  178. }
  179. void Handle(const char* httpMethod, const char* relativePath, http_ctx_handler handlerFunc) {
  180. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  181. }
  182. void Handle(const char* httpMethod, const char* relativePath, http_state_handler handlerFunc) {
  183. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  184. }
  185. // HEAD
  186. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  187. Handle("HEAD", relativePath, handlerFunc);
  188. }
  189. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  190. Handle("HEAD", relativePath, handlerFunc);
  191. }
  192. void HEAD(const char* relativePath, http_ctx_handler handlerFunc) {
  193. Handle("HEAD", relativePath, handlerFunc);
  194. }
  195. void HEAD(const char* relativePath, http_state_handler handlerFunc) {
  196. Handle("HEAD", relativePath, handlerFunc);
  197. }
  198. // GET
  199. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  200. Handle("GET", relativePath, handlerFunc);
  201. }
  202. void GET(const char* relativePath, http_async_handler handlerFunc) {
  203. Handle("GET", relativePath, handlerFunc);
  204. }
  205. void GET(const char* relativePath, http_ctx_handler handlerFunc) {
  206. Handle("GET", relativePath, handlerFunc);
  207. }
  208. void GET(const char* relativePath, http_state_handler handlerFunc) {
  209. Handle("GET", relativePath, handlerFunc);
  210. }
  211. // POST
  212. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  213. Handle("POST", relativePath, handlerFunc);
  214. }
  215. void POST(const char* relativePath, http_async_handler handlerFunc) {
  216. Handle("POST", relativePath, handlerFunc);
  217. }
  218. void POST(const char* relativePath, http_ctx_handler handlerFunc) {
  219. Handle("POST", relativePath, handlerFunc);
  220. }
  221. void POST(const char* relativePath, http_state_handler handlerFunc) {
  222. Handle("POST", relativePath, handlerFunc);
  223. }
  224. // PUT
  225. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  226. Handle("PUT", relativePath, handlerFunc);
  227. }
  228. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  229. Handle("PUT", relativePath, handlerFunc);
  230. }
  231. void PUT(const char* relativePath, http_ctx_handler handlerFunc) {
  232. Handle("PUT", relativePath, handlerFunc);
  233. }
  234. void PUT(const char* relativePath, http_state_handler handlerFunc) {
  235. Handle("PUT", relativePath, handlerFunc);
  236. }
  237. // DELETE
  238. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  239. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  240. Handle("DELETE", relativePath, handlerFunc);
  241. }
  242. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  243. Handle("DELETE", relativePath, handlerFunc);
  244. }
  245. void Delete(const char* relativePath, http_ctx_handler handlerFunc) {
  246. Handle("DELETE", relativePath, handlerFunc);
  247. }
  248. void Delete(const char* relativePath, http_state_handler handlerFunc) {
  249. Handle("DELETE", relativePath, handlerFunc);
  250. }
  251. // PATCH
  252. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  253. Handle("PATCH", relativePath, handlerFunc);
  254. }
  255. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  256. Handle("PATCH", relativePath, handlerFunc);
  257. }
  258. void PATCH(const char* relativePath, http_ctx_handler handlerFunc) {
  259. Handle("PATCH", relativePath, handlerFunc);
  260. }
  261. void PATCH(const char* relativePath, http_state_handler handlerFunc) {
  262. Handle("PATCH", relativePath, handlerFunc);
  263. }
  264. // Any
  265. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  266. Handle("HEAD", relativePath, handlerFunc);
  267. Handle("GET", relativePath, handlerFunc);
  268. Handle("POST", relativePath, handlerFunc);
  269. Handle("PUT", relativePath, handlerFunc);
  270. Handle("DELETE", relativePath, handlerFunc);
  271. Handle("PATCH", relativePath, handlerFunc);
  272. }
  273. void Any(const char* relativePath, http_async_handler handlerFunc) {
  274. Handle("HEAD", relativePath, handlerFunc);
  275. Handle("GET", relativePath, handlerFunc);
  276. Handle("POST", relativePath, handlerFunc);
  277. Handle("PUT", relativePath, handlerFunc);
  278. Handle("DELETE", relativePath, handlerFunc);
  279. Handle("PATCH", relativePath, handlerFunc);
  280. }
  281. void Any(const char* relativePath, http_ctx_handler handlerFunc) {
  282. Handle("HEAD", relativePath, handlerFunc);
  283. Handle("GET", relativePath, handlerFunc);
  284. Handle("POST", relativePath, handlerFunc);
  285. Handle("PUT", relativePath, handlerFunc);
  286. Handle("DELETE", relativePath, handlerFunc);
  287. Handle("PATCH", relativePath, handlerFunc);
  288. }
  289. void Any(const char* relativePath, http_state_handler handlerFunc) {
  290. Handle("HEAD", relativePath, handlerFunc);
  291. Handle("GET", relativePath, handlerFunc);
  292. Handle("POST", relativePath, handlerFunc);
  293. Handle("PUT", relativePath, handlerFunc);
  294. Handle("DELETE", relativePath, handlerFunc);
  295. Handle("PATCH", relativePath, handlerFunc);
  296. }
  297. };
  298. }
  299. #endif // HV_HTTP_SERVICE_H_