From da88211a7d1b3ea4f54986d09f424a959672cfeb Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 24 Dec 2024 11:46:59 +0800 Subject: [PATCH] [coro_http][feat]support available (#860) --- .../standalone/cinatra/coro_http_client.hpp | 5 +++++ .../cinatra/coro_http_connection.hpp | 5 +++++ src/coro_http/tests/test_cinatra.cpp | 19 +++++++++++++++++++ .../tests/test_cinatra_websocket.cpp | 14 ++++++++++---- src/coro_http/tests/test_coro_http_server.cpp | 4 +++- 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/include/ylt/standalone/cinatra/coro_http_client.hpp b/include/ylt/standalone/cinatra/coro_http_client.hpp index f6ae118bf..71dd95196 100644 --- a/include/ylt/standalone/cinatra/coro_http_client.hpp +++ b/include/ylt/standalone/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/ylt/standalone/cinatra/coro_http_connection.hpp b/include/ylt/standalone/cinatra/coro_http_connection.hpp index cae086f0e..db6d3ad3e 100644 --- a/include/ylt/standalone/cinatra/coro_http_connection.hpp +++ b/include/ylt/standalone/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/src/coro_http/tests/test_cinatra.cpp b/src/coro_http/tests/test_cinatra.cpp index 8eecfc49f..8d39fc13b 100644 --- a/src/coro_http/tests/test_cinatra.cpp +++ b/src/coro_http/tests/test_cinatra.cpp @@ -759,6 +759,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(); { @@ -882,6 +887,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/src/coro_http/tests/test_cinatra_websocket.cpp b/src/coro_http/tests/test_cinatra_websocket.cpp index e9d3e7ef4..f57095fc7 100644 --- a/src/coro_http/tests/test_cinatra_websocket.cpp +++ b/src/coro_http/tests/test_cinatra_websocket.cpp @@ -277,10 +277,12 @@ TEST_CASE("test send after server stop") { TEST_CASE("test read write in different threads") { cinatra::coro_http_server server(1, 8090); + size_t count = 0; + std::promise promise; server.set_http_handler( "/", - [](coro_http_request &req, - coro_http_response &resp) -> async_simple::coro::Lazy { + [&](coro_http_request &req, + coro_http_response &resp) -> async_simple::coro::Lazy { CHECK(req.get_content_type() == content_type::websocket); websocket_result result{}; while (true) { @@ -288,7 +290,11 @@ TEST_CASE("test read write in different threads") { if (result.ec) { break; } - + count++; + if (count == 100) { + promise.set_value(); + break; + } auto ec = co_await req.get_conn()->write_websocket(result.data); if (ec) { break; @@ -326,7 +332,7 @@ TEST_CASE("test read write in different threads") { async_simple::coro::syncAwait(lazy()); - std::this_thread::sleep_for(std::chrono::milliseconds(300)); + promise.get_future().wait_for(std::chrono::seconds(2)); server.stop(); } diff --git a/src/coro_http/tests/test_coro_http_server.cpp b/src/coro_http/tests/test_coro_http_server.cpp index 4e84b9c24..11c19584f 100644 --- a/src/coro_http/tests/test_coro_http_server.cpp +++ b/src/coro_http/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); }