HttpService.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. HttpService() {
  39. preprocessor = NULL;
  40. postprocessor = NULL;
  41. base_url = DEFAULT_BASE_URL;
  42. document_root = DEFAULT_DOCUMENT_ROOT;
  43. home_page = DEFAULT_HOME_PAGE;
  44. }
  45. void AddApi(const char* path, http_method method, http_api_handler handler) {
  46. std::shared_ptr<http_method_handlers> method_handlers = NULL;
  47. auto iter = api_handlers.find(path);
  48. if (iter == api_handlers.end()) {
  49. // add path
  50. method_handlers = std::shared_ptr<http_method_handlers>(new http_method_handlers);
  51. api_handlers[path] = method_handlers;
  52. }
  53. else {
  54. method_handlers = iter->second;
  55. }
  56. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  57. if (iter->method == method) {
  58. // update
  59. iter->handler = handler;
  60. return;
  61. }
  62. }
  63. // add
  64. method_handlers->push_back(http_method_handler(method, handler));
  65. }
  66. int GetApi(const char* url, http_method method, http_api_handler* handler) {
  67. // {base_url}/path?query
  68. const char* s = url;
  69. const char* c = base_url.c_str();
  70. while (*s != '\0' && *c != '\0' && *s == *c) {++s;++c;}
  71. if (*c != '\0') {
  72. return HTTP_STATUS_NOT_FOUND;
  73. }
  74. const char* e = s;
  75. while (*e != '\0' && *e != '?') ++e;
  76. std::string path = std::string(s, e);
  77. auto iter = api_handlers.find(path);
  78. if (iter == api_handlers.end()) {
  79. *handler = NULL;
  80. return HTTP_STATUS_NOT_FOUND;
  81. }
  82. auto method_handlers = iter->second;
  83. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  84. if (iter->method == method) {
  85. *handler = iter->handler;
  86. return 0;
  87. }
  88. }
  89. *handler = NULL;
  90. return HTTP_STATUS_METHOD_NOT_ALLOWED;
  91. }
  92. };
  93. #endif // HTTP_SERVICE_H_