From 074fc55a55cfa9b1f81d95562342851736d82715 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 9 Dec 2024 17:59:48 +0800 Subject: [PATCH 1/2] timeout duration --- include/cinatra/coro_http_client.hpp | 3 ++- tests/test_cinatra.cpp | 33 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index a9304415..d9ae32b5 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -617,7 +617,7 @@ class coro_http_client : public std::enable_shared_from_this { : self(self) { self->socket_->is_timeout_ = false; - if (self->enable_timeout_) { + if (self->enable_timeout_ && duration.count() >= 0) { self->timeout(self->timer_, duration, std::move(msg)) .start([](auto &&) { }); @@ -1972,6 +1972,7 @@ class coro_http_client : public std::enable_shared_from_this { async_simple::coro::Lazy connect(const uri_t &u) { if (socket_->has_closed_) { + socket_->is_timeout_ = false; host_ = proxy_host_.empty() ? u.get_host() : proxy_host_; port_ = proxy_port_.empty() ? u.get_port() : proxy_port_; if (auto ec = co_await coro_io::async_connect( diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index be8c85b0..00e3f0fc 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -72,6 +72,39 @@ TEST_CASE("test for gzip") { CHECK(result.resp_body == "hello world"); } + { + coro_http_client client{}; + client.add_header("Content-Encoding", "none"); + client.set_conn_timeout(0ms); + std::string uri = "http://127.0.0.1:8090/none"; + auto result = async_simple::coro::syncAwait(client.connect(uri)); + std::cout << result.net_err.message() << "\n"; + CHECK(result.net_err == std::errc::timed_out); + + client.set_conn_timeout(-1ms); + client.set_req_timeout(0ms); + result = async_simple::coro::syncAwait(client.connect(uri)); + CHECK(!result.net_err); + + result = async_simple::coro::syncAwait(client.async_get("/none")); + CHECK(result.net_err == std::errc::timed_out); + + client.add_header("Content-Encoding", "none"); + client.set_req_timeout(-1ms); + result = async_simple::coro::syncAwait(client.async_get(uri)); + CHECK(!result.net_err); + client.add_header("Content-Encoding", "none"); + result = async_simple::coro::syncAwait(client.async_get(uri)); + CHECK(!result.net_err); + + client.add_header("Content-Encoding", "none"); + coro_http_client::config conf{}; + conf.req_timeout_duration = 0ms; + client.init_config(conf); + result = async_simple::coro::syncAwait(client.async_get(uri)); + CHECK(result.net_err == std::errc::timed_out); + } + { coro_http_client client{}; std::string uri = "http://127.0.0.1:8090/deflate"; From 4ab48c80d5c041c751c16f6d059e7bd816f1a6ca Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 9 Dec 2024 18:08:04 +0800 Subject: [PATCH 2/2] fix case --- tests/test_cinatra.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index 00e3f0fc..632e82ad 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -78,16 +78,18 @@ TEST_CASE("test for gzip") { client.set_conn_timeout(0ms); std::string uri = "http://127.0.0.1:8090/none"; auto result = async_simple::coro::syncAwait(client.connect(uri)); - std::cout << result.net_err.message() << "\n"; - CHECK(result.net_err == std::errc::timed_out); + if (result.net_err) + CHECK(result.net_err == std::errc::timed_out); client.set_conn_timeout(-1ms); client.set_req_timeout(0ms); result = async_simple::coro::syncAwait(client.connect(uri)); - CHECK(!result.net_err); + if (result.net_err) + CHECK(!result.net_err); result = async_simple::coro::syncAwait(client.async_get("/none")); - CHECK(result.net_err == std::errc::timed_out); + if (result.net_err) + CHECK(result.net_err == std::errc::timed_out); client.add_header("Content-Encoding", "none"); client.set_req_timeout(-1ms); @@ -102,7 +104,8 @@ TEST_CASE("test for gzip") { conf.req_timeout_duration = 0ms; client.init_config(conf); result = async_simple::coro::syncAwait(client.async_get(uri)); - CHECK(result.net_err == std::errc::timed_out); + if (result.net_err) + CHECK(result.net_err == std::errc::timed_out); } {