HttpService.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef HV_HTTP_SERVICE_H_
  2. #define HV_HTTP_SERVICE_H_
  3. #include <string>
  4. #include <map>
  5. #include <list>
  6. #include <memory>
  7. #include <functional>
  8. #include "hexport.h"
  9. #include "HttpMessage.h"
  10. #include "HttpResponseWriter.h"
  11. #include "HttpContext.h"
  12. #define DEFAULT_BASE_URL "/api/v1"
  13. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  14. #define DEFAULT_HOME_PAGE "index.html"
  15. #define DEFAULT_ERROR_PAGE "error.html"
  16. #define DEFAULT_INDEXOF_DIR "/downloads/"
  17. #define DEFAULT_KEEPALIVE_TIMEOUT 75000 // ms
  18. /*
  19. * @param[in] req: parsed structured http request
  20. * @param[out] resp: structured http response
  21. * @return 0: handle unfinished
  22. * http_status_code: handle done
  23. */
  24. #define HTTP_STATUS_UNFINISHED 0
  25. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
  26. typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
  27. typedef std::function<int(const HttpContextPtr& ctx)> http_handler;
  28. struct http_method_handler {
  29. http_method method;
  30. http_sync_handler sync_handler;
  31. http_async_handler async_handler;
  32. http_handler handler;
  33. http_method_handler(http_method m = HTTP_POST,
  34. http_sync_handler s = NULL,
  35. http_async_handler a = NULL,
  36. http_handler h = NULL)
  37. {
  38. method = m;
  39. sync_handler = std::move(s);
  40. async_handler = std::move(a);
  41. handler = std::move(h);
  42. }
  43. };
  44. // method => http_method_handler
  45. typedef std::list<http_method_handler> http_method_handlers;
  46. // path => http_method_handlers
  47. typedef std::map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  48. namespace hv {
  49. struct HV_EXPORT HttpService {
  50. // preprocessor -> processor -> postprocessor
  51. http_sync_handler preprocessor;
  52. // processor: api_handlers -> staticHandler -> errorHandler
  53. http_handler processor;
  54. http_sync_handler postprocessor;
  55. // api service (that is http.APIServer)
  56. std::string base_url;
  57. http_api_handlers api_handlers;
  58. // file service (that is http.FileServer)
  59. http_handler staticHandler;
  60. http_handler largeFileHandler;
  61. std::string document_root;
  62. std::string home_page;
  63. std::string error_page;
  64. // indexof service (that is http.DirectoryServer)
  65. std::string index_of;
  66. http_handler errorHandler;
  67. // options
  68. int keepalive_timeout;
  69. HttpService() {
  70. preprocessor = NULL;
  71. processor = NULL;
  72. postprocessor = NULL;
  73. // base_url = DEFAULT_BASE_URL;
  74. staticHandler = NULL;
  75. largeFileHandler = NULL;
  76. document_root = DEFAULT_DOCUMENT_ROOT;
  77. home_page = DEFAULT_HOME_PAGE;
  78. // error_page = DEFAULT_ERROR_PAGE;
  79. // index_of = DEFAULT_INDEXOF_DIR;
  80. errorHandler = NULL;
  81. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  82. }
  83. void AddApi(const char* path, http_method method,
  84. http_sync_handler sync_handler = NULL,
  85. http_async_handler async_handler = NULL,
  86. http_handler handler = NULL);
  87. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  88. int GetApi(const char* url, http_method method,
  89. http_sync_handler* sync_handler = NULL,
  90. http_async_handler* async_handler = NULL,
  91. http_handler* handler = NULL);
  92. // RESTful API /:field/ => req->query_params["field"]
  93. int GetApi(HttpRequest* req,
  94. http_sync_handler* sync_handler = NULL,
  95. http_async_handler* async_handler = NULL,
  96. http_handler* handler = NULL);
  97. hv::StringList Paths() {
  98. hv::StringList paths;
  99. for (auto& pair : api_handlers) {
  100. paths.emplace_back(pair.first);
  101. }
  102. return paths;
  103. }
  104. // github.com/gin-gonic/gin
  105. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  106. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc, NULL, NULL);
  107. }
  108. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  109. AddApi(relativePath, http_method_enum(httpMethod), NULL, handlerFunc, NULL);
  110. }
  111. void Handle(const char* httpMethod, const char* relativePath, http_handler handlerFunc) {
  112. AddApi(relativePath, http_method_enum(httpMethod), NULL, NULL, handlerFunc);
  113. }
  114. // HEAD
  115. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  116. Handle("HEAD", relativePath, handlerFunc);
  117. }
  118. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  119. Handle("HEAD", relativePath, handlerFunc);
  120. }
  121. void HEAD(const char* relativePath, http_handler handlerFunc) {
  122. Handle("HEAD", relativePath, handlerFunc);
  123. }
  124. // GET
  125. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  126. Handle("GET", relativePath, handlerFunc);
  127. }
  128. void GET(const char* relativePath, http_async_handler handlerFunc) {
  129. Handle("GET", relativePath, handlerFunc);
  130. }
  131. void GET(const char* relativePath, http_handler handlerFunc) {
  132. Handle("GET", relativePath, handlerFunc);
  133. }
  134. // POST
  135. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  136. Handle("POST", relativePath, handlerFunc);
  137. }
  138. void POST(const char* relativePath, http_async_handler handlerFunc) {
  139. Handle("POST", relativePath, handlerFunc);
  140. }
  141. void POST(const char* relativePath, http_handler handlerFunc) {
  142. Handle("POST", relativePath, handlerFunc);
  143. }
  144. // PUT
  145. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  146. Handle("PUT", relativePath, handlerFunc);
  147. }
  148. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  149. Handle("PUT", relativePath, handlerFunc);
  150. }
  151. void PUT(const char* relativePath, http_handler handlerFunc) {
  152. Handle("PUT", relativePath, handlerFunc);
  153. }
  154. // DELETE
  155. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  156. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  157. Handle("DELETE", relativePath, handlerFunc);
  158. }
  159. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  160. Handle("DELETE", relativePath, handlerFunc);
  161. }
  162. void Delete(const char* relativePath, http_handler handlerFunc) {
  163. Handle("DELETE", relativePath, handlerFunc);
  164. }
  165. // PATCH
  166. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  167. Handle("PATCH", relativePath, handlerFunc);
  168. }
  169. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  170. Handle("PATCH", relativePath, handlerFunc);
  171. }
  172. void PATCH(const char* relativePath, http_handler handlerFunc) {
  173. Handle("PATCH", relativePath, handlerFunc);
  174. }
  175. // Any
  176. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  177. Handle("HEAD", relativePath, handlerFunc);
  178. Handle("GET", relativePath, handlerFunc);
  179. Handle("POST", relativePath, handlerFunc);
  180. Handle("PUT", relativePath, handlerFunc);
  181. Handle("DELETE", relativePath, handlerFunc);
  182. Handle("PATCH", relativePath, handlerFunc);
  183. }
  184. void Any(const char* relativePath, http_async_handler handlerFunc) {
  185. Handle("HEAD", relativePath, handlerFunc);
  186. Handle("GET", relativePath, handlerFunc);
  187. Handle("POST", relativePath, handlerFunc);
  188. Handle("PUT", relativePath, handlerFunc);
  189. Handle("DELETE", relativePath, handlerFunc);
  190. Handle("PATCH", relativePath, handlerFunc);
  191. }
  192. void Any(const char* relativePath, http_handler handlerFunc) {
  193. Handle("HEAD", relativePath, handlerFunc);
  194. Handle("GET", relativePath, handlerFunc);
  195. Handle("POST", relativePath, handlerFunc);
  196. Handle("PUT", relativePath, handlerFunc);
  197. Handle("DELETE", relativePath, handlerFunc);
  198. Handle("PATCH", relativePath, handlerFunc);
  199. }
  200. };
  201. }
  202. #endif // HV_HTTP_SERVICE_H_