HttpService.h 8.1 KB

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