HttpService.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // NOTE: http_sync_handler run on IO thread
  27. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
  28. // NOTE: http_async_handler run on hv::async threadpool
  29. typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
  30. // NOTE: http_ctx_handler run on IO thread, you can easily post HttpContextPtr to your consumer thread for processing.
  31. typedef std::function<int(const HttpContextPtr& ctx)> http_ctx_handler;
  32. struct http_handler {
  33. http_sync_handler sync_handler;
  34. http_async_handler async_handler;
  35. http_ctx_handler ctx_handler;
  36. http_handler() {}
  37. http_handler(http_sync_handler fn) : sync_handler(std::move(fn)) {}
  38. http_handler(http_async_handler fn) : async_handler(std::move(fn)) {}
  39. http_handler(http_ctx_handler fn) : ctx_handler(std::move(fn)) {}
  40. http_handler(const http_handler& rhs)
  41. : sync_handler(std::move(rhs.sync_handler))
  42. , async_handler(std::move(rhs.async_handler))
  43. , ctx_handler(std::move(rhs.ctx_handler))
  44. {}
  45. const http_handler& operator=(http_sync_handler fn) {
  46. sync_handler = std::move(fn);
  47. return *this;
  48. }
  49. const http_handler& operator=(http_async_handler fn) {
  50. async_handler = std::move(fn);
  51. return *this;
  52. }
  53. const http_handler& operator=(http_ctx_handler fn) {
  54. ctx_handler = std::move(fn);
  55. return *this;
  56. }
  57. bool isNull() {
  58. return sync_handler == NULL &&
  59. async_handler == NULL &&
  60. ctx_handler == NULL;
  61. }
  62. operator bool() {
  63. return !isNull();
  64. }
  65. };
  66. struct http_method_handler {
  67. http_method method;
  68. http_handler handler;
  69. http_method_handler() {}
  70. http_method_handler(http_method m, const http_handler& h) : method(m), handler(h) {}
  71. };
  72. // method => http_method_handler
  73. typedef std::list<http_method_handler> http_method_handlers;
  74. // path => http_method_handlers
  75. typedef std::unordered_map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  76. namespace hv {
  77. struct HV_EXPORT HttpService {
  78. // preprocessor -> processor -> postprocessor
  79. http_handler preprocessor;
  80. // processor: api_handlers -> staticHandler -> errorHandler
  81. http_handler processor;
  82. http_handler postprocessor;
  83. // api service (that is http.APIServer)
  84. std::string base_url;
  85. http_api_handlers api_handlers;
  86. // file service (that is http.FileServer)
  87. http_handler staticHandler;
  88. http_handler largeFileHandler;
  89. std::string document_root;
  90. std::string home_page;
  91. std::string error_page;
  92. // indexof service (that is http.DirectoryServer)
  93. std::string index_of;
  94. int file_speed = 0; // file download speed limit(KB/s, <=0 no limit)
  95. http_handler errorHandler;
  96. // options
  97. int keepalive_timeout;
  98. HttpService() {
  99. // base_url = DEFAULT_BASE_URL;
  100. document_root = DEFAULT_DOCUMENT_ROOT;
  101. home_page = DEFAULT_HOME_PAGE;
  102. // error_page = DEFAULT_ERROR_PAGE;
  103. // index_of = DEFAULT_INDEXOF_DIR;
  104. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  105. }
  106. void AddApi(const char* path, http_method method, const http_handler& handler);
  107. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  108. int GetApi(const char* url, http_method method, http_handler** handler);
  109. // RESTful API /:field/ => req->query_params["field"]
  110. int GetApi(HttpRequest* req, http_handler** handler);
  111. hv::StringList Paths() {
  112. hv::StringList paths;
  113. for (auto& pair : api_handlers) {
  114. paths.emplace_back(pair.first);
  115. }
  116. return paths;
  117. }
  118. // github.com/gin-gonic/gin
  119. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  120. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  121. }
  122. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  123. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  124. }
  125. void Handle(const char* httpMethod, const char* relativePath, http_ctx_handler handlerFunc) {
  126. AddApi(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
  127. }
  128. // HEAD
  129. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  130. Handle("HEAD", relativePath, handlerFunc);
  131. }
  132. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  133. Handle("HEAD", relativePath, handlerFunc);
  134. }
  135. void HEAD(const char* relativePath, http_ctx_handler handlerFunc) {
  136. Handle("HEAD", relativePath, handlerFunc);
  137. }
  138. // GET
  139. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  140. Handle("GET", relativePath, handlerFunc);
  141. }
  142. void GET(const char* relativePath, http_async_handler handlerFunc) {
  143. Handle("GET", relativePath, handlerFunc);
  144. }
  145. void GET(const char* relativePath, http_ctx_handler handlerFunc) {
  146. Handle("GET", relativePath, handlerFunc);
  147. }
  148. // POST
  149. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  150. Handle("POST", relativePath, handlerFunc);
  151. }
  152. void POST(const char* relativePath, http_async_handler handlerFunc) {
  153. Handle("POST", relativePath, handlerFunc);
  154. }
  155. void POST(const char* relativePath, http_ctx_handler handlerFunc) {
  156. Handle("POST", relativePath, handlerFunc);
  157. }
  158. // PUT
  159. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  160. Handle("PUT", relativePath, handlerFunc);
  161. }
  162. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  163. Handle("PUT", relativePath, handlerFunc);
  164. }
  165. void PUT(const char* relativePath, http_ctx_handler handlerFunc) {
  166. Handle("PUT", relativePath, handlerFunc);
  167. }
  168. // DELETE
  169. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  170. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  171. Handle("DELETE", relativePath, handlerFunc);
  172. }
  173. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  174. Handle("DELETE", relativePath, handlerFunc);
  175. }
  176. void Delete(const char* relativePath, http_ctx_handler handlerFunc) {
  177. Handle("DELETE", relativePath, handlerFunc);
  178. }
  179. // PATCH
  180. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  181. Handle("PATCH", relativePath, handlerFunc);
  182. }
  183. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  184. Handle("PATCH", relativePath, handlerFunc);
  185. }
  186. void PATCH(const char* relativePath, http_ctx_handler handlerFunc) {
  187. Handle("PATCH", relativePath, handlerFunc);
  188. }
  189. // Any
  190. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  191. Handle("HEAD", relativePath, handlerFunc);
  192. Handle("GET", relativePath, handlerFunc);
  193. Handle("POST", relativePath, handlerFunc);
  194. Handle("PUT", relativePath, handlerFunc);
  195. Handle("DELETE", relativePath, handlerFunc);
  196. Handle("PATCH", relativePath, handlerFunc);
  197. }
  198. void Any(const char* relativePath, http_async_handler handlerFunc) {
  199. Handle("HEAD", relativePath, handlerFunc);
  200. Handle("GET", relativePath, handlerFunc);
  201. Handle("POST", relativePath, handlerFunc);
  202. Handle("PUT", relativePath, handlerFunc);
  203. Handle("DELETE", relativePath, handlerFunc);
  204. Handle("PATCH", relativePath, handlerFunc);
  205. }
  206. void Any(const char* relativePath, http_ctx_handler handlerFunc) {
  207. Handle("HEAD", relativePath, handlerFunc);
  208. Handle("GET", relativePath, handlerFunc);
  209. Handle("POST", relativePath, handlerFunc);
  210. Handle("PUT", relativePath, handlerFunc);
  211. Handle("DELETE", relativePath, handlerFunc);
  212. Handle("PATCH", relativePath, handlerFunc);
  213. }
  214. };
  215. }
  216. #endif // HV_HTTP_SERVICE_H_