HttpService.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef HTTP_SERVICE_H_
  2. #define 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. #define DEFAULT_BASE_URL "/v1/api"
  11. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  12. #define DEFAULT_HOME_PAGE "index.html"
  13. /*
  14. * @param[in] req: parsed structured http request
  15. * @param[out] res: structured http response
  16. * @return 0: handle continue
  17. * http_status_code: handle done
  18. */
  19. // typedef int (*http_api_handler)(HttpRequest* req, HttpResponse* res);
  20. // NOTE: use std::function/std::bind is more convenient and more flexible.
  21. typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_api_handler;
  22. struct http_method_handler {
  23. http_method method;
  24. http_api_handler handler;
  25. http_method_handler(http_method m = HTTP_POST, http_api_handler h = NULL) {
  26. method = m;
  27. handler = h;
  28. }
  29. };
  30. // method => http_api_handler
  31. typedef std::list<http_method_handler> http_method_handlers;
  32. // path => http_method_handlers
  33. typedef std::map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  34. struct HV_EXPORT HttpService {
  35. // preprocessor -> api -> web -> postprocessor
  36. http_api_handler preprocessor;
  37. http_api_handler postprocessor;
  38. // api service (that is http.APIServer)
  39. std::string base_url;
  40. http_api_handlers api_handlers;
  41. // web service (that is http.FileServer)
  42. std::string document_root;
  43. std::string home_page;
  44. std::string error_page;
  45. // indexof service (that is http.DirectoryServer)
  46. std::string index_of;
  47. HttpService() {
  48. preprocessor = NULL;
  49. postprocessor = NULL;
  50. // base_url = DEFAULT_BASE_URL;
  51. document_root = DEFAULT_DOCUMENT_ROOT;
  52. home_page = DEFAULT_HOME_PAGE;
  53. }
  54. void AddApi(const char* path, http_method method, http_api_handler handler);
  55. // @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
  56. int GetApi(const char* url, http_method method, http_api_handler* handler);
  57. // RESTful API /:field/ => req->query_params["field"]
  58. int GetApi(HttpRequest* req, http_api_handler* handler);
  59. StringList Paths() {
  60. StringList paths;
  61. for (auto& pair : api_handlers) {
  62. paths.emplace_back(pair.first);
  63. }
  64. return paths;
  65. }
  66. // github.com/gin-gonic/gin
  67. void Handle(const char* httpMethod, const char* relativePath, http_api_handler handlerFunc) {
  68. AddApi(relativePath, http_method_enum(httpMethod), handlerFunc);
  69. }
  70. void HEAD(const char* relativePath, http_api_handler handlerFunc) {
  71. Handle("HEAD", relativePath, handlerFunc);
  72. }
  73. void GET(const char* relativePath, http_api_handler handlerFunc) {
  74. Handle("GET", relativePath, handlerFunc);
  75. }
  76. void POST(const char* relativePath, http_api_handler handlerFunc) {
  77. Handle("POST", relativePath, handlerFunc);
  78. }
  79. void PUT(const char* relativePath, http_api_handler handlerFunc) {
  80. Handle("PUT", relativePath, handlerFunc);
  81. }
  82. // NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
  83. void Delete(const char* relativePath, http_api_handler handlerFunc) {
  84. Handle("DELETE", relativePath, handlerFunc);
  85. }
  86. void PATCH(const char* relativePath, http_api_handler handlerFunc) {
  87. Handle("PATCH", relativePath, handlerFunc);
  88. }
  89. void Any(const char* relativePath, http_api_handler handlerFunc) {
  90. Handle("HEAD", relativePath, handlerFunc);
  91. Handle("GET", relativePath, handlerFunc);
  92. Handle("POST", relativePath, handlerFunc);
  93. Handle("PUT", relativePath, handlerFunc);
  94. Handle("DELETE", relativePath, handlerFunc);
  95. Handle("PATCH", relativePath, handlerFunc);
  96. }
  97. };
  98. #endif // HTTP_SERVICE_H_