From cae4ab2a00d78284e462eb5a77d2a0d2934581f7 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 19 Dec 2024 17:29:41 +0800 Subject: [PATCH] add available (#678) --- include/cinatra/coro_http_client.hpp | 5 +++++ include/cinatra/coro_http_connection.hpp | 5 +++++ tests/test_cinatra.cpp | 19 +++++++++++++++++++ tests/test_coro_http_server.cpp | 4 +++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 10056329..f8d190db 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -369,6 +369,11 @@ class coro_http_client : public std::enable_shared_from_this { max_http_body_len_ = max_size; } + size_t available() { + std::error_code ec{}; + return socket_->impl_.available(ec); + } + async_simple::coro::Lazy read_websocket() { auto time_out_guard = timer_guard(this, req_timeout_duration_, "websocket timer"); diff --git a/include/cinatra/coro_http_connection.hpp b/include/cinatra/coro_http_connection.hpp index cae086f0..db6d3ad3 100644 --- a/include/cinatra/coro_http_connection.hpp +++ b/include/cinatra/coro_http_connection.hpp @@ -445,6 +445,11 @@ class coro_http_connection return remote_addr_; } + size_t available() { + std::error_code ec{}; + return socket_.available(ec); + } + void set_multi_buf(bool r) { multi_buf_ = r; } void set_default_handler( diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index b29f75f3..f7d75406 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -768,6 +768,11 @@ TEST_CASE("test pipeline") { res.set_status_and_content(status_type::ok, "hello coro"); co_return; }); + server.set_http_handler( + "/test_available", [](coro_http_request &req, coro_http_response &res) { + std::string str(1400, 'a'); + res.set_status_and_content(status_type::ok, std::move(str)); + }); server.async_start(); { @@ -891,6 +896,20 @@ TEST_CASE("test pipeline") { result.resp_body.size(), 0); CHECK(parser.status() != 200); } + + { + coro_http_client client{}; + std::string uri = "http://127.0.0.1:9001"; + async_simple::coro::syncAwait(client.connect(uri)); + auto ec = async_simple::coro::syncAwait(client.async_write_raw( + "GET /test_available HTTP/1.1\r\nHost: 127.0.0.1:8090\r\n\r\n")); + CHECK(!ec); + + auto result = + async_simple::coro::syncAwait(client.async_read_raw(http_method::GET)); + auto sz = client.available(); + CHECK(sz > 0); + } } #endif diff --git a/tests/test_coro_http_server.cpp b/tests/test_coro_http_server.cpp index 6d06cf42..11fbf83a 100644 --- a/tests/test_coro_http_server.cpp +++ b/tests/test_coro_http_server.cpp @@ -751,13 +751,15 @@ TEST_CASE("chunked request") { while (true) { result = co_await req.get_conn()->read_chunked(); + auto size = req.get_conn()->available(); if (result.ec) { co_return; } if (result.eof) { + CHECK(size == 0); break; } - + CHECK(size >= 0); content.append(result.data); }