diff --git a/include/cinatra/coro_http_connection.hpp b/include/cinatra/coro_http_connection.hpp index c0cf383b..482b3b9f 100644 --- a/include/cinatra/coro_http_connection.hpp +++ b/include/cinatra/coro_http_connection.hpp @@ -204,7 +204,7 @@ class coro_http_connection } else { if (default_handler_) { - default_handler_(request_, response_); + co_await default_handler_(request_, response_); } else { bool is_exist = false; @@ -409,7 +409,8 @@ class coro_http_connection void set_multi_buf(bool r) { multi_buf_ = r; } void set_default_handler( - std::function &handler) { + std::function( + coro_http_request &, coro_http_response &)> &handler) { default_handler_ = handler; } @@ -904,7 +905,8 @@ class coro_http_connection #endif bool need_shrink_every_time_ = false; bool multi_buf_ = true; - std::function + std::function(coro_http_request &, + coro_http_response &)> default_handler_ = nullptr; std::string chunk_size_str_; std::string remote_addr_; diff --git a/include/cinatra/coro_http_response.hpp b/include/cinatra/coro_http_response.hpp index 0da9eba5..b3a55e46 100644 --- a/include/cinatra/coro_http_response.hpp +++ b/include/cinatra/coro_http_response.hpp @@ -173,13 +173,6 @@ class coro_http_response { : resp_str.append(CONN_CLOSE_SV); } - if (content_view_.empty()) { - resp_str.append(content_); - } - else { - resp_str.append(content_view_); - } - append_header_str(resp_str, resp_headers_); if (!resp_header_span_.empty()) { @@ -187,7 +180,12 @@ class coro_http_response { } resp_str.append(CRCF); - resp_str.append(content_); + if (content_view_.empty()) { + resp_str.append(content_); + } + else { + resp_str.append(content_view_); + } } void append_header_str(auto &resp_str, auto &resp_headers) { diff --git a/include/cinatra/coro_http_server.hpp b/include/cinatra/coro_http_server.hpp index 0bb6b66c..7c2aea83 100644 --- a/include/cinatra/coro_http_server.hpp +++ b/include/cinatra/coro_http_server.hpp @@ -550,8 +550,9 @@ class coro_http_server { void set_shrink_to_fit(bool r) { need_shrink_every_time_ = r; } - void set_default_handler( - std::function handler) { + void set_default_handler(std::function( + coro_http_request &, coro_http_response &)> + handler) { default_handler_ = std::move(handler); } @@ -923,7 +924,8 @@ class coro_http_server { #endif coro_http_router router_; bool need_shrink_every_time_ = false; - std::function + std::function(coro_http_request &, + coro_http_response &)> default_handler_ = nullptr; }; diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index a17ff3f9..6a3dcf81 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -399,10 +399,22 @@ async_simple::coro::Lazy test_collect_all() { TEST_CASE("test default http handler") { coro_http_server server(1, 9001); - server.set_default_handler([](coro_http_request &req, - coro_http_response &resp) { - resp.set_status_and_content(status_type::ok, "It is from default handler"); - }); + server.set_default_handler( + [](coro_http_request &req, + coro_http_response &resp) -> async_simple::coro::Lazy { + resp.set_status_and_content(status_type::ok, + "It is from default handler"); + co_return; + }); + server.set_http_handler( + "/view", + [](coro_http_request &req, + coro_http_response &resp) -> async_simple::coro::Lazy { + resp.set_delay(true); + resp.set_status_and_content_view(status_type::ok, + req.get_body()); // no copy + co_await resp.get_conn()->reply(); + }); server.async_start(); for (int i = 0; i < 5; i++) { @@ -414,6 +426,10 @@ TEST_CASE("test default http handler") { CHECK(data.resp_body == "It is from default handler"); data = client.get("/any"); CHECK(data.resp_body == "It is from default handler"); + data = async_simple::coro::syncAwait( + client.async_post("/view", "post string", req_content_type::string)); + CHECK(data.status == 200); + CHECK(data.resp_body == "post string"); } } @@ -562,6 +578,7 @@ TEST_CASE("test head put and some other request") { std::string result = ec ? "delete failed" : "delete ok"; resp.set_status_and_content(status_type::ok, result); }); + server.async_start(); std::this_thread::sleep_for(300ms);