1
0

HttpService.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "HttpService.h"
  2. #include "hbase.h" // import strendswith
  3. void HttpService::AddApi(const char* path, http_method method, http_sync_handler handler, http_async_handler async_handler) {
  4. std::shared_ptr<http_method_handlers> method_handlers = NULL;
  5. auto iter = api_handlers.find(path);
  6. if (iter == api_handlers.end()) {
  7. // add path
  8. method_handlers = std::shared_ptr<http_method_handlers>(new http_method_handlers);
  9. api_handlers[path] = method_handlers;
  10. }
  11. else {
  12. method_handlers = iter->second;
  13. }
  14. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  15. if (iter->method == method) {
  16. // update
  17. iter->sync_handler = handler;
  18. iter->async_handler = async_handler;
  19. return;
  20. }
  21. }
  22. // add
  23. method_handlers->push_back(http_method_handler(method, handler, async_handler));
  24. }
  25. int HttpService::GetApi(const char* url, http_method method, http_sync_handler* handler, http_async_handler* async_handler) {
  26. // {base_url}/path?query
  27. const char* s = url;
  28. const char* b = base_url.c_str();
  29. while (*s && *b && *s == *b) {++s;++b;}
  30. if (*b != '\0') {
  31. return HTTP_STATUS_NOT_FOUND;
  32. }
  33. const char* e = s;
  34. while (*e && *e != '?') ++e;
  35. std::string path = std::string(s, e);
  36. auto iter = api_handlers.find(path);
  37. if (iter == api_handlers.end()) {
  38. if (handler) *handler = NULL;
  39. if (async_handler) *async_handler = NULL;
  40. return HTTP_STATUS_NOT_FOUND;
  41. }
  42. auto method_handlers = iter->second;
  43. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  44. if (iter->method == method) {
  45. if (handler) *handler = iter->sync_handler;
  46. if (async_handler) *async_handler = iter->async_handler;
  47. return 0;
  48. }
  49. }
  50. if (handler) *handler = NULL;
  51. if (async_handler) *async_handler = NULL;
  52. return HTTP_STATUS_METHOD_NOT_ALLOWED;
  53. }
  54. int HttpService::GetApi(HttpRequest* req, http_sync_handler* handler, http_async_handler* async_handler) {
  55. // {base_url}/path?query
  56. const char* s = req->path.c_str();
  57. const char* b = base_url.c_str();
  58. while (*s && *b && *s == *b) {++s;++b;}
  59. if (*b != '\0') {
  60. return HTTP_STATUS_NOT_FOUND;
  61. }
  62. const char* e = s;
  63. while (*e && *e != '?') ++e;
  64. std::string path = std::string(s, e);
  65. const char *kp, *ks, *vp, *vs;
  66. bool match;
  67. for (auto iter = api_handlers.begin(); iter != api_handlers.end(); ++iter) {
  68. kp = iter->first.c_str();
  69. vp = path.c_str();
  70. match = false;
  71. std::map<std::string, std::string> params;
  72. while (*kp && *vp) {
  73. if (kp[0] == '*') {
  74. // wildcard *
  75. if (strendswith(vp, kp+1)) {
  76. match = true;
  77. }
  78. break;
  79. } else if (*kp == *vp) {
  80. if (kp[0] == '/' && kp[1] == ':') {
  81. // RESTful /:field/
  82. kp += 2;
  83. ks = kp;
  84. while (*kp && *kp != '/') {++kp;}
  85. vp += 1;
  86. vs = vp;
  87. while (*vp && *vp != '/') {++vp;}
  88. params[std::string(ks, kp-ks)] = std::string(vs, vp-vs);
  89. } else {
  90. ++kp;
  91. ++vp;
  92. }
  93. } else {
  94. match = false;
  95. break;
  96. }
  97. }
  98. match = match ? match : (*kp == '\0' && *vp == '\0');
  99. if (match) {
  100. auto method_handlers = iter->second;
  101. for (auto iter = method_handlers->begin(); iter != method_handlers->end(); ++iter) {
  102. if (iter->method == req->method) {
  103. for (auto& param : params) {
  104. // RESTful /:field/ => req->query_params[field]
  105. req->query_params[param.first] = param.second;
  106. }
  107. if (handler) *handler = iter->sync_handler;
  108. if (async_handler) *async_handler = iter->async_handler;
  109. return 0;
  110. }
  111. }
  112. if (params.size() == 0) {
  113. if (handler) *handler = NULL;
  114. if (async_handler) *async_handler = NULL;
  115. return HTTP_STATUS_METHOD_NOT_ALLOWED;
  116. }
  117. }
  118. }
  119. if (handler) *handler = NULL;
  120. if (async_handler) *async_handler = NULL;
  121. return HTTP_STATUS_NOT_FOUND;
  122. }