diff --git a/include/cinatra/coro_http_router.hpp b/include/cinatra/coro_http_router.hpp index a620ab2c..58ad19d4 100644 --- a/include/cinatra/coro_http_router.hpp +++ b/include/cinatra/coro_http_router.hpp @@ -88,12 +88,9 @@ class coro_http_router { } if (whole_str.find(":") != std::string::npos) { - std::vector coro_method_names = {}; - std::string coro_method_str; - coro_method_str.append(method_name); - coro_method_names.push_back(coro_method_str); + std::string method_str(method_name); coro_router_tree_->coro_insert(key, std::move(http_handler), - coro_method_names); + method_str); } else { if (whole_str.find("{") != std::string::npos || @@ -138,11 +135,9 @@ class coro_http_router { } if (whole_str.find(':') != std::string::npos) { - std::vector method_names = {}; - std::string method_str; - method_str.append(method_name); - method_names.push_back(method_str); - router_tree_->insert(whole_str, std::move(http_handler), method_names); + std::string method_str(method_name); + router_tree_->insert(whole_str, std::move(http_handler), + method_str); } else if (whole_str.find("{") != std::string::npos || whole_str.find(")") != std::string::npos) { diff --git a/include/cinatra/coro_radix_tree.hpp b/include/cinatra/coro_radix_tree.hpp index 585446b8..13d7b04f 100644 --- a/include/cinatra/coro_radix_tree.hpp +++ b/include/cinatra/coro_radix_tree.hpp @@ -42,8 +42,8 @@ struct coro_handler_t { struct radix_tree_node { std::string path; - std::vector handlers; - std::vector coro_handlers; + handler_t handler; + coro_handler_t coro_handler; std::string indices; std::vector> children; int max_params; @@ -54,21 +54,18 @@ struct radix_tree_node { std::function get_handler(const std::string &method) { - for (auto &h : this->handlers) { - if (h.method == method) { - return h.handler; - } + if (handler.method == method) { + return handler.handler; } + return nullptr; } std::function(coro_http_request &req, coro_http_response &resp)> get_coro_handler(const std::string &method) { - for (auto &h : this->coro_handlers) { - if (h.method == method) { - return h.coro_handler; - } + if (coro_handler.method == method) { + return coro_handler.coro_handler; } return nullptr; } @@ -76,22 +73,18 @@ struct radix_tree_node { int add_handler( std::function handler, - const std::vector &methods) { - for (auto &m : methods) { - auto old_handler = this->get_handler(m); - this->handlers.push_back(handler_t{m, handler}); - } + const std::string &method) { + + this->handler = handler_t{method, handler}; + return 0; } int add_coro_handler(std::function( coro_http_request &req, coro_http_response &resp)> coro_handler, - const std::vector &methods) { - for (auto &m : methods) { - auto old_coro_handler = this->get_coro_handler(m); - this->coro_handlers.push_back(coro_handler_t{m, coro_handler}); - } + const std::string &method) { + this->coro_handler = coro_handler_t{method, coro_handler}; return 0; } @@ -134,7 +127,7 @@ class radix_tree { const std::string &path, std::function handler, - const std::vector &methods) { + const std::string &method) { auto root = this->root; int i = 0, n = path.size(), param_count = 0, code = 0; while (i < n) { @@ -166,7 +159,7 @@ class radix_tree { ++param_count; } - code = root->add_handler(handler, methods); + code = root->add_handler(handler, method); break; } @@ -181,7 +174,7 @@ class radix_tree { ++param_count; if (i == n) { - code = root->add_handler(handler, methods); + code = root->add_handler(handler, method); break; } } @@ -193,7 +186,7 @@ class radix_tree { i += root->path.size() + 1; if (i == n) { - code = root->add_handler(handler, methods); + code = root->add_handler(handler, method); break; } } @@ -207,18 +200,18 @@ class radix_tree { if (j < m) { std::shared_ptr child( std::make_shared(root->path.substr(j))); - child->handlers = root->handlers; + child->handler = root->handler; child->indices = root->indices; child->children = root->children; root->path = root->path.substr(0, j); - root->handlers = {}; + root->handler = {}; root->indices = child->path[0]; root->children = {child}; } if (i == n) { - code = root->add_handler(handler, methods); + code = root->add_handler(handler, method); break; } } @@ -235,7 +228,7 @@ class radix_tree { std::function( coro_http_request &req, coro_http_response &resp)> coro_handler, - const std::vector &methods) { + std::string &method) { auto root = this->root; int i = 0, n = path.size(), param_count = 0, code = 0; while (i < n) { @@ -267,7 +260,7 @@ class radix_tree { ++param_count; } - code = root->add_coro_handler(coro_handler, methods); + code = root->add_coro_handler(coro_handler, method); break; } @@ -282,7 +275,7 @@ class radix_tree { ++param_count; if (i == n) { - code = root->add_coro_handler(coro_handler, methods); + code = root->add_coro_handler(coro_handler, method); break; } } @@ -294,7 +287,7 @@ class radix_tree { i += root->path.size() + 1; if (i == n) { - code = root->add_coro_handler(coro_handler, methods); + code = root->add_coro_handler(coro_handler, method); break; } } @@ -308,18 +301,18 @@ class radix_tree { if (j < m) { std::shared_ptr child( std::make_shared(root->path.substr(j))); - child->handlers = root->handlers; + child->handler = root->handler; child->indices = root->indices; child->children = root->children; root->path = root->path.substr(0, j); - root->handlers = {}; + root->handler = {}; root->indices = child->path[0]; root->children = {child}; } if (i == n) { - code = root->add_coro_handler(coro_handler, methods); + code = root->add_coro_handler(coro_handler, method); break; } } diff --git a/tests/test_coro_http_server.cpp b/tests/test_coro_http_server.cpp index f780419e..d4427617 100644 --- a/tests/test_coro_http_server.cpp +++ b/tests/test_coro_http_server.cpp @@ -1385,6 +1385,15 @@ TEST_CASE("test radix tree restful api") { client.get("http://127.0.0.1:9001/user/subid/subscriptions"); client.get("http://127.0.0.1:9001/user/ultramarines/subscriptions/guilliman"); client.get("http://127.0.0.1:9001/value/guilliman/cawl/yvraine"); + + client.post("http://127.0.0.1:9001/user/cinatra", + "hello", req_content_type::string); + client.post("http://127.0.0.1:9001/user/subid/subscriptions", "hello", + req_content_type::string); + client.post("http://127.0.0.1:9001/user/ultramarines/subscriptions/guilliman", + "hello", req_content_type::string); + client.post("http://127.0.0.1:9001/value/guilliman/cawl/yvraine", "hello", + req_content_type::string); } TEST_CASE("test coro radix tree restful api") { @@ -1450,6 +1459,15 @@ TEST_CASE("test coro radix tree restful api") { client.get("http://127.0.0.1:9001/user/subid/subscriptions"); client.get("http://127.0.0.1:9001/user/ultramarines/subscriptions/guilliman"); client.get("http://127.0.0.1:9001/value/guilliman/cawl/yvraine"); + + client.post("http://127.0.0.1:9001/user/cinatra", "hello", + req_content_type::string); + client.post("http://127.0.0.1:9001/user/subid/subscriptions", "hello", + req_content_type::string); + client.post("http://127.0.0.1:9001/user/ultramarines/subscriptions/guilliman", + "hello", req_content_type::string); + client.post("http://127.0.0.1:9001/value/guilliman/cawl/yvraine", "hello", + req_content_type::string); } TEST_CASE("test reverse proxy") {