1
0

HttpService.h 3.1 KB

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