HttpService.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef HTTP_SERVICE_H_
  2. #define HTTP_SERVICE_H_
  3. #include <string.h>
  4. #include <string>
  5. #include <map>
  6. #include <list>
  7. #include <memory>
  8. #include "HttpRequest.h"
  9. #define DEFAULT_BASE_URL "/v1/api"
  10. #define DEFAULT_DOCUMENT_ROOT "/var/www/html"
  11. #define DEFAULT_HOME_PAGE "index.html"
  12. #define HANDLE_CONTINUE 0
  13. #define HANDLE_DONE 1
  14. typedef int (*http_api_handler)(HttpRequest* req, HttpResponse* res);
  15. struct http_method_handler {
  16. http_method method;
  17. http_api_handler handler;
  18. http_method_handler(http_method m = HTTP_POST, http_api_handler h = NULL) {
  19. method = m;
  20. handler = h;
  21. }
  22. };
  23. // Provide Restful API
  24. typedef std::list<http_method_handler> http_method_handlers;
  25. // path => http_method_handlers
  26. typedef std::map<std::string, std::shared_ptr<http_method_handlers>> http_api_handlers;
  27. struct HttpService {
  28. // preprocessor -> api -> web -> postprocessor
  29. http_api_handler preprocessor;
  30. http_api_handler postprocessor;
  31. // api service
  32. std::string base_url;
  33. http_api_handlers api_handlers;
  34. // web service
  35. std::string document_root;
  36. std::string home_page;
  37. std::string error_page;
  38. std::string index_of;
  39. HttpService() {
  40. preprocessor = NULL;
  41. postprocessor = NULL;
  42. base_url = DEFAULT_BASE_URL;
  43. document_root = DEFAULT_DOCUMENT_ROOT;
  44. home_page = DEFAULT_HOME_PAGE;
  45. }
  46. void AddApi(const char* path, http_method method, http_api_handler handler) {
  47. std::shared_ptr<http_method_handlers> method_handlers = NULL;
  48. auto iter = api_handlers.find(path);
  49. if (iter == api_handlers.end()) {
  50. // add path
  51. method_handlers = std::shared_ptr<http_method_handlers>(new http_method_handlers);
  52. api_handlers[path] = method_handlers;
  53. }
  54. else {
  55. method_handlers = iter->second;
  56. }
  57. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  58. if (iter->method == method) {
  59. // update
  60. iter->handler = handler;
  61. return;
  62. }
  63. }
  64. // add
  65. method_handlers->push_back(http_method_handler(method, handler));
  66. }
  67. int GetApi(const char* url, http_method method, http_api_handler* handler) {
  68. // {base_url}/path?query
  69. const char* s = url;
  70. const char* c = base_url.c_str();
  71. while (*s != '\0' && *c != '\0' && *s == *c) {++s;++c;}
  72. if (*c != '\0') {
  73. return HTTP_STATUS_NOT_FOUND;
  74. }
  75. const char* e = s;
  76. while (*e != '\0' && *e != '?') ++e;
  77. std::string path = std::string(s, e);
  78. auto iter = api_handlers.find(path);
  79. if (iter == api_handlers.end()) {
  80. *handler = NULL;
  81. return HTTP_STATUS_NOT_FOUND;
  82. }
  83. auto method_handlers = iter->second;
  84. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  85. if (iter->method == method) {
  86. *handler = iter->handler;
  87. return 0;
  88. }
  89. }
  90. *handler = NULL;
  91. return HTTP_STATUS_METHOD_NOT_ALLOWED;
  92. }
  93. };
  94. #endif // HTTP_SERVICE_H_