diff --git a/include/cinatra/coro_http_server.hpp b/include/cinatra/coro_http_server.hpp index bfede5eb..01d57e62 100644 --- a/include/cinatra/coro_http_server.hpp +++ b/include/cinatra/coro_http_server.hpp @@ -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" @@ -178,21 +179,31 @@ class coro_http_server { auto channel = std::make_shared>( coro_io::channel::create(hosts, {.lba = type}, weights)); - set_http_handler( - url_path, + auto handler = [this, channel, type, url_path]( coro_http_request &req, coro_http_response &response) -> async_simple::coro::Lazy { - co_await channel->send_request( - [this, &req, &response]( - coro_http_client &client, - std::string_view host) -> async_simple::coro::Lazy { - uri_t uri; - uri.parse_from(host.data()); - co_await reply(client, uri.get_path(), req, response); - }); - }, - std::forward(aspects)...); + co_await channel->send_request( + [this, &req, &response]( + coro_http_client &client, + std::string_view host) -> async_simple::coro::Lazy { + 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(url_path, std::move(handler), + std::forward(aspects)...); + } + else { + set_http_handler(url_path, std::move(handler), + std::forward(aspects)...); + } } void set_max_size_of_cache_files(size_t max_size = 3 * 1024 * 1024) { diff --git a/tests/test_coro_http_server.cpp b/tests/test_coro_http_server.cpp index eea3ee9b..f780419e 100644 --- a/tests/test_coro_http_server.cpp +++ b/tests/test_coro_http_server.cpp @@ -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( "/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( "/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( "/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); @@ -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()); } \ No newline at end of file