Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:improve router, each http method per tree #532

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions include/cinatra/coro_http_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ class coro_http_router {
}

if (whole_str.find(":") != std::string::npos) {
std::vector<std::string> 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 ||
Expand Down Expand Up @@ -138,11 +135,8 @@ class coro_http_router {
}

if (whole_str.find(':') != std::string::npos) {
std::vector<std::string> 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) {
Expand Down
60 changes: 26 additions & 34 deletions include/cinatra/coro_radix_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ struct coro_handler_t {

struct radix_tree_node {
std::string path;
std::vector<handler_t> handlers;
std::vector<coro_handler_t> coro_handlers;
handler_t handler;
coro_handler_t coro_handler;
std::string indices;
std::vector<std::shared_ptr<radix_tree_node>> children;
int max_params;
Expand All @@ -54,44 +54,36 @@ struct radix_tree_node {

std::function<void(coro_http_request &req, coro_http_response &resp)>
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<async_simple::coro::Lazy<void>(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;
}

int add_handler(
std::function<void(coro_http_request &req, coro_http_response &resp)>
handler,
const std::vector<std::string> &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<async_simple::coro::Lazy<void>(
coro_http_request &req, coro_http_response &resp)>
coro_handler,
const std::vector<std::string> &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;
}

Expand Down Expand Up @@ -134,7 +126,7 @@ class radix_tree {
const std::string &path,
std::function<void(coro_http_request &req, coro_http_response &resp)>
handler,
const std::vector<std::string> &methods) {
const std::string &method) {
auto root = this->root;
int i = 0, n = path.size(), param_count = 0, code = 0;
while (i < n) {
Expand Down Expand Up @@ -166,7 +158,7 @@ class radix_tree {
++param_count;
}

code = root->add_handler(handler, methods);
code = root->add_handler(handler, method);
break;
}

Expand All @@ -181,7 +173,7 @@ class radix_tree {
++param_count;

if (i == n) {
code = root->add_handler(handler, methods);
code = root->add_handler(handler, method);
break;
}
}
Expand All @@ -193,7 +185,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;
}
}
Expand All @@ -207,18 +199,18 @@ class radix_tree {
if (j < m) {
std::shared_ptr<radix_tree_node> child(
std::make_shared<radix_tree_node>(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;
}
}
Expand All @@ -235,7 +227,7 @@ class radix_tree {
std::function<async_simple::coro::Lazy<void>(
coro_http_request &req, coro_http_response &resp)>
coro_handler,
const std::vector<std::string> &methods) {
std::string &method) {
auto root = this->root;
int i = 0, n = path.size(), param_count = 0, code = 0;
while (i < n) {
Expand Down Expand Up @@ -267,7 +259,7 @@ class radix_tree {
++param_count;
}

code = root->add_coro_handler(coro_handler, methods);
code = root->add_coro_handler(coro_handler, method);
break;
}

Expand All @@ -282,7 +274,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;
}
}
Expand All @@ -294,7 +286,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;
}
}
Expand All @@ -308,18 +300,18 @@ class radix_tree {
if (j < m) {
std::shared_ptr<radix_tree_node> child(
std::make_shared<radix_tree_node>(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;
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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") {
Expand Down
Loading