HttpService.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 enable_forward_proxy :1;
  129. HttpService() {
  130. // base_url = DEFAULT_BASE_URL;
  131. document_root = DEFAULT_DOCUMENT_ROOT;
  132. home_page = DEFAULT_HOME_PAGE;
  133. // error_page = DEFAULT_ERROR_PAGE;
  134. // index_of = DEFAULT_INDEXOF_DIR;
  135. proxy_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
  136. proxy_read_timeout = 0;
  137. proxy_write_timeout = 0;
  138. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  139. max_file_cache_size = MAX_FILE_CACHE_SIZE;
  140. file_cache_stat_interval = DEFAULT_FILE_CACHE_STAT_INTERVAL;
  141. file_cache_expired_time = DEFAULT_FILE_CACHE_EXPIRED_TIME;
  142. limit_rate = -1; // unlimited
  143. enable_forward_proxy = 0;
  144. }
  145. void AddApi(const char* path, http_method method, const http_handler& handler);
  146. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  147. int GetApi(const char* url, http_method method, http_handler** handler);
  148. // RESTful API /:field/ => req->query_params["field"]
  149. int GetApi(HttpRequest* req, http_handler** handler);
  150. // Static("/", "/var/www/html")
  151. void Static(const char* path, const char* dir);
  152. // @retval / => /var/www/html/index.html
  153. std::string GetStaticFilepath(const char* path);
  154. // forward proxy
  155. void enableForwardProxy() { enable_forward_proxy = 1; }
  156. // reverse proxy
  157. // Proxy("/api/v1/", "http://www.httpbin.org/");
  158. void Proxy(const char* path, const char* url);
  159. // @retval /api/v1/test => http://www.httpbin.org/test
  160. std::string GetProxyUrl(const char* path);
  161. hv::StringList Paths() {
  162. hv::StringList paths;
  163. for (auto& pair : api_handlers) {
  164. paths.emplace_back(pair.first);
  165. }
  166. return paths;
  167. }
  168. // github.com/gin-gonic/gin
  169. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  170. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  171. }
  172. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  173. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  174. }
  175. void Handle(const char* httpMethod, const char* relativePath, http_ctx_handler handlerFunc) {
  176. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  177. }
  178. void Handle(const char* httpMethod, const char* relativePath, http_state_handler handlerFunc) {
  179. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  180. }
  181. // HEAD
  182. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  183. Handle("HEAD", relativePath, handlerFunc);
  184. }
  185. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  186. Handle("HEAD", relativePath, handlerFunc);
  187. }
  188. void HEAD(const char* relativePath, http_ctx_handler handlerFunc) {
  189. Handle("HEAD", relativePath, handlerFunc);
  190. }
  191. void HEAD(const char* relativePath, http_state_handler handlerFunc) {
  192. Handle("HEAD", relativePath, handlerFunc);
  193. }
  194. // GET
  195. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  196. Handle("GET", relativePath, handlerFunc);
  197. }
  198. void GET(const char* relativePath, http_async_handler handlerFunc) {
  199. Handle("GET", relativePath, handlerFunc);
  200. }
  201. void GET(const char* relativePath, http_ctx_handler handlerFunc) {
  202. Handle("GET", relativePath, handlerFunc);
  203. }
  204. void GET(const char* relativePath, http_state_handler handlerFunc) {
  205. Handle("GET", relativePath, handlerFunc);
  206. }
  207. // POST
  208. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  209. Handle("POST", relativePath, handlerFunc);
  210. }
  211. void POST(const char* relativePath, http_async_handler handlerFunc) {
  212. Handle("POST", relativePath, handlerFunc);
  213. }
  214. void POST(const char* relativePath, http_ctx_handler handlerFunc) {
  215. Handle("POST", relativePath, handlerFunc);
  216. }
  217. void POST(const char* relativePath, http_state_handler handlerFunc) {
  218. Handle("POST", relativePath, handlerFunc);
  219. }
  220. // PUT
  221. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  222. Handle("PUT", relativePath, handlerFunc);
  223. }
  224. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  225. Handle("PUT", relativePath, handlerFunc);
  226. }
  227. void PUT(const char* relativePath, http_ctx_handler handlerFunc) {
  228. Handle("PUT", relativePath, handlerFunc);
  229. }
  230. void PUT(const char* relativePath, http_state_handler handlerFunc) {
  231. Handle("PUT", relativePath, handlerFunc);
  232. }
  233. // DELETE
  234. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  235. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  236. Handle("DELETE", relativePath, handlerFunc);
  237. }
  238. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  239. Handle("DELETE", relativePath, handlerFunc);
  240. }
  241. void Delete(const char* relativePath, http_ctx_handler handlerFunc) {
  242. Handle("DELETE", relativePath, handlerFunc);
  243. }
  244. void Delete(const char* relativePath, http_state_handler handlerFunc) {
  245. Handle("DELETE", relativePath, handlerFunc);
  246. }
  247. // PATCH
  248. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  249. Handle("PATCH", relativePath, handlerFunc);
  250. }
  251. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  252. Handle("PATCH", relativePath, handlerFunc);
  253. }
  254. void PATCH(const char* relativePath, http_ctx_handler handlerFunc) {
  255. Handle("PATCH", relativePath, handlerFunc);
  256. }
  257. void PATCH(const char* relativePath, http_state_handler handlerFunc) {
  258. Handle("PATCH", relativePath, handlerFunc);
  259. }
  260. // Any
  261. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  262. Handle("HEAD", relativePath, handlerFunc);
  263. Handle("GET", relativePath, handlerFunc);
  264. Handle("POST", relativePath, handlerFunc);
  265. Handle("PUT", relativePath, handlerFunc);
  266. Handle("DELETE", relativePath, handlerFunc);
  267. Handle("PATCH", relativePath, handlerFunc);
  268. }
  269. void Any(const char* relativePath, http_async_handler handlerFunc) {
  270. Handle("HEAD", relativePath, handlerFunc);
  271. Handle("GET", relativePath, handlerFunc);
  272. Handle("POST", relativePath, handlerFunc);
  273. Handle("PUT", relativePath, handlerFunc);
  274. Handle("DELETE", relativePath, handlerFunc);
  275. Handle("PATCH", relativePath, handlerFunc);
  276. }
  277. void Any(const char* relativePath, http_ctx_handler handlerFunc) {
  278. Handle("HEAD", relativePath, handlerFunc);
  279. Handle("GET", relativePath, handlerFunc);
  280. Handle("POST", relativePath, handlerFunc);
  281. Handle("PUT", relativePath, handlerFunc);
  282. Handle("DELETE", relativePath, handlerFunc);
  283. Handle("PATCH", relativePath, handlerFunc);
  284. }
  285. void Any(const char* relativePath, http_state_handler handlerFunc) {
  286. Handle("HEAD", relativePath, handlerFunc);
  287. Handle("GET", relativePath, handlerFunc);
  288. Handle("POST", relativePath, handlerFunc);
  289. Handle("PUT", relativePath, handlerFunc);
  290. Handle("DELETE", relativePath, handlerFunc);
  291. Handle("PATCH", relativePath, handlerFunc);
  292. }
  293. };
  294. }
  295. #endif // HV_HTTP_SERVICE_H_