From 98528db067bad2020c5423f4643ce42a1814402c Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 12 Aug 2024 18:11:18 +0800 Subject: [PATCH] fix out buf (#622) --- include/cinatra/coro_http_client.hpp | 6 +++++- tests/test_cinatra.cpp | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 68461518..66e7e892 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -1733,11 +1733,14 @@ class coro_http_client : public std::enable_shared_from_this { co_return data; } + bool is_out_buf = false; + bool is_ranges = parser_.is_resp_ranges(); if (is_ranges) { is_keep_alive = true; } if (parser_.is_chunked()) { + out_buf_ = {}; is_keep_alive = true; if (head_buf_.size() > 0) { const char *data_ptr = @@ -1750,6 +1753,7 @@ class coro_http_client : public std::enable_shared_from_this { } if (parser_.is_multipart()) { + out_buf_ = {}; is_keep_alive = true; if (head_buf_.size() > 0) { const char *data_ptr = @@ -1786,7 +1790,7 @@ class coro_http_client : public std::enable_shared_from_this { total_len_ = parser_.total_len(); #endif - bool is_out_buf = !out_buf_.empty(); + is_out_buf = !out_buf_.empty(); if (is_out_buf) { if (content_len > 0 && out_buf_.size() < content_len) { out_buf_ = {}; diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index 28108afb..ab8c5ca1 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -664,11 +664,18 @@ TEST_CASE("test request with out buffer") { resp.set_status_and_content(status_type::ok, "it is a test string, more than 10 bytes"); }); + server.set_http_handler( + "/test1", [](coro_http_request &req, coro_http_response &resp) { + resp.set_format_type(format_type::chunked); + resp.set_status_and_content(status_type::ok, + "it is a test string, more than 10 bytes"); + }); server.async_start(); std::string str; str.resize(10); std::string url = "http://127.0.0.1:8090/test"; + std::string url1 = "http://127.0.0.1:8090/test"; { coro_http_client client; @@ -682,6 +689,18 @@ TEST_CASE("test request with out buffer") { CHECK(!client.is_body_in_out_buf()); } + { + coro_http_client client; + auto ret = client.async_request(url1, http_method::GET, req_context<>{}, {}, + std::span{str.data(), str.size()}); + auto result = async_simple::coro::syncAwait(ret); + std::cout << result.status << "\n"; + std::cout << result.net_err.message() << "\n"; + std::cout << result.resp_body << "\n"; + CHECK(result.status == 200); + CHECK(!client.is_body_in_out_buf()); + } + { detail::resize(str, 1024); coro_http_client client;