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

improve #527

Merged
merged 1 commit into from
Feb 19, 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
35 changes: 23 additions & 12 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cinatra/coro_http_client.hpp"
#include "cinatra/coro_http_response.hpp"
#include "cinatra/coro_http_router.hpp"
#include "cinatra/define.h"
#include "cinatra/mime_types.hpp"
#include "cinatra_log_wrapper.hpp"
#include "coro_http_connection.hpp"
Expand Down Expand Up @@ -178,21 +179,31 @@ class coro_http_server {
auto channel = std::make_shared<coro_io::channel<coro_http_client>>(
coro_io::channel<coro_http_client>::create(hosts, {.lba = type},
weights));
set_http_handler<method...>(
url_path,
auto handler =
[this, channel, type, url_path](
coro_http_request &req,
coro_http_response &response) -> async_simple::coro::Lazy<void> {
co_await channel->send_request(
[this, &req, &response](
coro_http_client &client,
std::string_view host) -> async_simple::coro::Lazy<void> {
uri_t uri;
uri.parse_from(host.data());
co_await reply(client, uri.get_path(), req, response);
});
},
std::forward<Aspects>(aspects)...);
co_await channel->send_request(
[this, &req, &response](
coro_http_client &client,
std::string_view host) -> async_simple::coro::Lazy<void> {
uri_t uri;
uri.parse_from(host.data());
co_await reply(client, uri.get_path(), req, response);
});
};

if constexpr (sizeof...(method) == 0) {
set_http_handler<http_method::GET, http_method::POST, http_method::DEL,
http_method::HEAD, http_method::PUT, http_method::PATCH,
http_method::CONNECT, http_method::TRACE,
http_method::OPTIONS>(url_path, std::move(handler),
std::forward<Aspects>(aspects)...);
}
else {
set_http_handler<method...>(url_path, std::move(handler),
std::forward<Aspects>(aspects)...);
}
}

void set_max_size_of_cache_files(size_t max_size = 3 * 1024 * 1024) {
Expand Down
25 changes: 18 additions & 7 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,23 +1512,28 @@ TEST_CASE("test reverse proxy") {

std::this_thread::sleep_for(200ms);

coro_http_server proxy_wrr(10, 8090);
coro_http_server proxy_wrr(2, 8090);
proxy_wrr.set_http_proxy_handler<GET, POST>(
"/wrr", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"},
coro_io::load_blance_algorithm::WRR, {10, 5, 5}, log_t{}, check_t{});

coro_http_server proxy_rr(10, 8091);
coro_http_server proxy_rr(2, 8091);
proxy_rr.set_http_proxy_handler<GET, POST>(
"/rr", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"},
coro_io::load_blance_algorithm::RR, {}, log_t{});

coro_http_server proxy_random(10, 8092);
coro_http_server proxy_random(2, 8092);
proxy_random.set_http_proxy_handler<GET, POST>(
"/random", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"});

coro_http_server proxy_all(2, 8093);
proxy_all.set_http_proxy_handler(
"/all", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"});

proxy_wrr.async_start();
proxy_rr.async_start();
proxy_random.async_start();
proxy_all.async_start();

std::this_thread::sleep_for(200ms);

Expand Down Expand Up @@ -1557,8 +1562,14 @@ TEST_CASE("test reverse proxy") {
resp = client_wrr.get("http://127.0.0.1:8090/wrr");
CHECK(resp.resp_body == "web3");

coro_http_client client_hash;
resp_data resp_hash = client_hash.get("http://127.0.0.1:8092/random");
std::cout << resp_hash.resp_body << "\n";
CHECK(!resp_hash.resp_body.empty());
coro_http_client client_random;
resp_data resp_random = client_random.get("http://127.0.0.1:8092/random");
std::cout << resp_random.resp_body << "\n";
CHECK(!resp_random.resp_body.empty());

coro_http_client client_all;
resp_random = client_all.post("http://127.0.0.1:8093/all", "test content",
req_content_type::text);
std::cout << resp_random.resp_body << "\n";
CHECK(!resp_random.resp_body.empty());
}
Loading