Skip to content

Commit

Permalink
feat:improve router, each http method per tree
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Feb 26, 2024
1 parent a30136a commit 7d5d310
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
15 changes: 5 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,9 @@ 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
61 changes: 27 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,37 @@ 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 +127,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 +159,7 @@ class radix_tree {
++param_count;
}

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

Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -207,18 +200,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 +228,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 +260,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 +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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -308,18 +301,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

0 comments on commit 7d5d310

Please sign in to comment.