HttpService.cpp 5.0 KB

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