HttpService.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 http_method_handler {
  25. http_method method;
  26. http_sync_handler sync_handler;
  27. http_async_handler async_handler;
  28. http_method_handler(http_method m = HTTP_POST,
  29. http_sync_handler s = NULL,
  30. http_async_handler a = NULL)
  31. {
  32. method = m;
  33. sync_handler = std::move(s);
  34. async_handler = std::move(a);
  35. }
  36. };
  37. // method => http_method_handler
  38. typedef std::list<http_method_handler> http_method_handlers;
  39. // path => http_method_handlers
  40. typedef std::map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  41. struct HttpService;
  42. struct HV_EXPORT HttpContext {
  43. HttpService* service;
  44. HttpRequestPtr request;
  45. HttpResponsePtr response;
  46. HttpResponseWriterPtr writer;
  47. };
  48. typedef std::shared_ptr<HttpContext> HttpContextPtr;
  49. typedef std::function<int(const HttpContextPtr& ctx)> http_handler;
  50. struct HV_EXPORT HttpService {
  51. // preprocessor -> processor -> postprocessor
  52. http_sync_handler preprocessor;
  53. // processor: api_handlers -> staticHandler -> errorHandler
  54. http_handler processor;
  55. http_sync_handler postprocessor;
  56. // api service (that is http.APIServer)
  57. std::string base_url;
  58. http_api_handlers api_handlers;
  59. // file service (that is http.FileServer)
  60. http_handler staticHandler;
  61. http_handler largeFileHandler;
  62. std::string document_root;
  63. std::string home_page;
  64. std::string error_page;
  65. // indexof service (that is http.DirectoryServer)
  66. std::string index_of;
  67. http_handler errorHandler;
  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. }
  81. void AddApi(const char* path, http_method method, http_sync_handler handler = NULL, http_async_handler async_handler = NULL);
  82. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  83. int GetApi(const char* url, http_method method, http_sync_handler* handler = NULL, http_async_handler* async_handler = NULL);
  84. // RESTful API /:field/ => req->query_params["field"]
  85. int GetApi(HttpRequest* req, http_sync_handler* handler = NULL, http_async_handler* async_handler = NULL);
  86. StringList Paths() {
  87. StringList paths;
  88. for (auto& pair : api_handlers) {
  89. paths.emplace_back(pair.first);
  90. }
  91. return paths;
  92. }
  93. // github.com/gin-gonic/gin
  94. void Handle(const char* httpMethod, const char* relativePath, http_sync_handler handlerFunc) {
  95. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc, NULL);
  96. }
  97. void Handle(const char* httpMethod, const char* relativePath, http_async_handler handlerFunc) {
  98. AddApi(relativePath, http_method_enum(httpMethod), NULL, handlerFunc);
  99. }
  100. // HEAD
  101. void HEAD(const char* relativePath, http_sync_handler handlerFunc) {
  102. Handle("HEAD", relativePath, handlerFunc);
  103. }
  104. void HEAD(const char* relativePath, http_async_handler handlerFunc) {
  105. Handle("HEAD", relativePath, handlerFunc);
  106. }
  107. // GET
  108. void GET(const char* relativePath, http_sync_handler handlerFunc) {
  109. Handle("GET", relativePath, handlerFunc);
  110. }
  111. void GET(const char* relativePath, http_async_handler handlerFunc) {
  112. Handle("GET", relativePath, handlerFunc);
  113. }
  114. // POST
  115. void POST(const char* relativePath, http_sync_handler handlerFunc) {
  116. Handle("POST", relativePath, handlerFunc);
  117. }
  118. void POST(const char* relativePath, http_async_handler handlerFunc) {
  119. Handle("POST", relativePath, handlerFunc);
  120. }
  121. // PUT
  122. void PUT(const char* relativePath, http_sync_handler handlerFunc) {
  123. Handle("PUT", relativePath, handlerFunc);
  124. }
  125. void PUT(const char* relativePath, http_async_handler handlerFunc) {
  126. Handle("PUT", relativePath, handlerFunc);
  127. }
  128. // DELETE
  129. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  130. void Delete(const char* relativePath, http_sync_handler handlerFunc) {
  131. Handle("DELETE", relativePath, handlerFunc);
  132. }
  133. void Delete(const char* relativePath, http_async_handler handlerFunc) {
  134. Handle("DELETE", relativePath, handlerFunc);
  135. }
  136. // PATCH
  137. void PATCH(const char* relativePath, http_sync_handler handlerFunc) {
  138. Handle("PATCH", relativePath, handlerFunc);
  139. }
  140. void PATCH(const char* relativePath, http_async_handler handlerFunc) {
  141. Handle("PATCH", relativePath, handlerFunc);
  142. }
  143. // Any
  144. void Any(const char* relativePath, http_sync_handler handlerFunc) {
  145. Handle("HEAD", relativePath, handlerFunc);
  146. Handle("GET", relativePath, handlerFunc);
  147. Handle("POST", relativePath, handlerFunc);
  148. Handle("PUT", relativePath, handlerFunc);
  149. Handle("DELETE", relativePath, handlerFunc);
  150. Handle("PATCH", relativePath, handlerFunc);
  151. }
  152. void Any(const char* relativePath, http_async_handler handlerFunc) {
  153. Handle("HEAD", relativePath, handlerFunc);
  154. Handle("GET", relativePath, handlerFunc);
  155. Handle("POST", relativePath, handlerFunc);
  156. Handle("PUT", relativePath, handlerFunc);
  157. Handle("DELETE", relativePath, handlerFunc);
  158. Handle("PATCH", relativePath, handlerFunc);
  159. }
  160. };
  161. #endif // HV_HTTP_SERVICE_H_