HttpService.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. struct HV_EXPORT HttpService {
  49. // preprocessor -> processor -> postprocessor
  50. http_sync_handler preprocessor;
  51. // processor: api_handlers -> staticHandler -> errorHandler
  52. http_handler processor;
  53. http_sync_handler postprocessor;
  54. // api service (that is http.APIServer)
  55. std::string base_url;
  56. http_api_handlers api_handlers;
  57. // file service (that is http.FileServer)
  58. http_handler staticHandler;
  59. http_handler largeFileHandler;
  60. std::string document_root;
  61. std::string home_page;
  62. std::string error_page;
  63. // indexof service (that is http.DirectoryServer)
  64. std::string index_of;
  65. http_handler errorHandler;
  66. // options
  67. int keepalive_timeout;
  68. HttpService() {
  69. preprocessor = NULL;
  70. processor = NULL;
  71. postprocessor = NULL;
  72. // base_url = DEFAULT_BASE_URL;
  73. staticHandler = NULL;
  74. largeFileHandler = NULL;
  75. document_root = DEFAULT_DOCUMENT_ROOT;
  76. home_page = DEFAULT_HOME_PAGE;
  77. // error_page = DEFAULT_ERROR_PAGE;
  78. // index_of = DEFAULT_INDEXOF_DIR;
  79. errorHandler = NULL;
  80. keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
  81. }
  82. void AddApi(const char* path, http_method method,
  83. http_sync_handler sync_handler = NULL,
  84. http_async_handler async_handler = NULL,
  85. http_handler handler = NULL);
  86. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  87. int GetApi(const char* url, http_method method,
  88. http_sync_handler* sync_handler = NULL,
  89. http_async_handler* async_handler = NULL,
  90. http_handler* handler = NULL);
  91. // RESTful API /:field/ => req->query_params["field"]
  92. int GetApi(HttpRequest* req,
  93. http_sync_handler* sync_handler = NULL,
  94. http_async_handler* async_handler = NULL,
  95. http_handler* handler = NULL);
  96. hv::StringList Paths() {
  97. hv::StringList paths;
  98. for (auto& pair : api_handlers) {
  99. paths.emplace_back(pair.first);
  100. }
  101. return paths;
  102. }
  103. // github.com/gin-gonic/gin
  104. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  105. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc, NULL, NULL);
  106. }
  107. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  108. AddApi(relativePath, http_method_enum(httpMethod), NULL, handlerFunc, NULL);
  109. }
  110. void Handle(const char* httpMethod, const char* relativePath, http_handler handlerFunc) {
  111. AddApi(relativePath, http_method_enum(httpMethod), NULL, NULL, handlerFunc);
  112. }
  113. // HEAD
  114. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  115. Handle("HEAD", relativePath, handlerFunc);
  116. }
  117. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  118. Handle("HEAD", relativePath, handlerFunc);
  119. }
  120. void HEAD(const char* relativePath, http_handler handlerFunc) {
  121. Handle("HEAD", relativePath, handlerFunc);
  122. }
  123. // GET
  124. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  125. Handle("GET", relativePath, handlerFunc);
  126. }
  127. void GET(const char* relativePath, http_async_handler handlerFunc) {
  128. Handle("GET", relativePath, handlerFunc);
  129. }
  130. void GET(const char* relativePath, http_handler handlerFunc) {
  131. Handle("GET", relativePath, handlerFunc);
  132. }
  133. // POST
  134. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  135. Handle("POST", relativePath, handlerFunc);
  136. }
  137. void POST(const char* relativePath, http_async_handler handlerFunc) {
  138. Handle("POST", relativePath, handlerFunc);
  139. }
  140. void POST(const char* relativePath, http_handler handlerFunc) {
  141. Handle("POST", relativePath, handlerFunc);
  142. }
  143. // PUT
  144. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  145. Handle("PUT", relativePath, handlerFunc);
  146. }
  147. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  148. Handle("PUT", relativePath, handlerFunc);
  149. }
  150. void PUT(const char* relativePath, http_handler handlerFunc) {
  151. Handle("PUT", relativePath, handlerFunc);
  152. }
  153. // DELETE
  154. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  155. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  156. Handle("DELETE", relativePath, handlerFunc);
  157. }
  158. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  159. Handle("DELETE", relativePath, handlerFunc);
  160. }
  161. void Delete(const char* relativePath, http_handler handlerFunc) {
  162. Handle("DELETE", relativePath, handlerFunc);
  163. }
  164. // PATCH
  165. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  166. Handle("PATCH", relativePath, handlerFunc);
  167. }
  168. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  169. Handle("PATCH", relativePath, handlerFunc);
  170. }
  171. void PATCH(const char* relativePath, http_handler handlerFunc) {
  172. Handle("PATCH", relativePath, handlerFunc);
  173. }
  174. // Any
  175. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  176. Handle("HEAD", relativePath, handlerFunc);
  177. Handle("GET", relativePath, handlerFunc);
  178. Handle("POST", relativePath, handlerFunc);
  179. Handle("PUT", relativePath, handlerFunc);
  180. Handle("DELETE", relativePath, handlerFunc);
  181. Handle("PATCH", relativePath, handlerFunc);
  182. }
  183. void Any(const char* relativePath, http_async_handler handlerFunc) {
  184. Handle("HEAD", relativePath, handlerFunc);
  185. Handle("GET", relativePath, handlerFunc);
  186. Handle("POST", relativePath, handlerFunc);
  187. Handle("PUT", relativePath, handlerFunc);
  188. Handle("DELETE", relativePath, handlerFunc);
  189. Handle("PATCH", relativePath, handlerFunc);
  190. }
  191. void Any(const char* relativePath, http_handler handlerFunc) {
  192. Handle("HEAD", relativePath, handlerFunc);
  193. Handle("GET", relativePath, handlerFunc);
  194. Handle("POST", relativePath, handlerFunc);
  195. Handle("PUT", relativePath, handlerFunc);
  196. Handle("DELETE", relativePath, handlerFunc);
  197. Handle("PATCH", relativePath, handlerFunc);
  198. }
  199. };
  200. #endif // HV_HTTP_SERVICE_H_