Skip to content

Commit 98528db

Browse files
authored
fix out buf (#622)
1 parent 4a1c14f commit 98528db

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

include/cinatra/coro_http_client.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,11 +1733,14 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
17331733
co_return data;
17341734
}
17351735

1736+
bool is_out_buf = false;
1737+
17361738
bool is_ranges = parser_.is_resp_ranges();
17371739
if (is_ranges) {
17381740
is_keep_alive = true;
17391741
}
17401742
if (parser_.is_chunked()) {
1743+
out_buf_ = {};
17411744
is_keep_alive = true;
17421745
if (head_buf_.size() > 0) {
17431746
const char *data_ptr =
@@ -1750,6 +1753,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
17501753
}
17511754

17521755
if (parser_.is_multipart()) {
1756+
out_buf_ = {};
17531757
is_keep_alive = true;
17541758
if (head_buf_.size() > 0) {
17551759
const char *data_ptr =
@@ -1786,7 +1790,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
17861790
total_len_ = parser_.total_len();
17871791
#endif
17881792

1789-
bool is_out_buf = !out_buf_.empty();
1793+
is_out_buf = !out_buf_.empty();
17901794
if (is_out_buf) {
17911795
if (content_len > 0 && out_buf_.size() < content_len) {
17921796
out_buf_ = {};

tests/test_cinatra.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,11 +664,18 @@ TEST_CASE("test request with out buffer") {
664664
resp.set_status_and_content(status_type::ok,
665665
"it is a test string, more than 10 bytes");
666666
});
667+
server.set_http_handler<GET>(
668+
"/test1", [](coro_http_request &req, coro_http_response &resp) {
669+
resp.set_format_type(format_type::chunked);
670+
resp.set_status_and_content(status_type::ok,
671+
"it is a test string, more than 10 bytes");
672+
});
667673
server.async_start();
668674

669675
std::string str;
670676
str.resize(10);
671677
std::string url = "http://127.0.0.1:8090/test";
678+
std::string url1 = "http://127.0.0.1:8090/test";
672679

673680
{
674681
coro_http_client client;
@@ -682,6 +689,18 @@ TEST_CASE("test request with out buffer") {
682689
CHECK(!client.is_body_in_out_buf());
683690
}
684691

692+
{
693+
coro_http_client client;
694+
auto ret = client.async_request(url1, http_method::GET, req_context<>{}, {},
695+
std::span<char>{str.data(), str.size()});
696+
auto result = async_simple::coro::syncAwait(ret);
697+
std::cout << result.status << "\n";
698+
std::cout << result.net_err.message() << "\n";
699+
std::cout << result.resp_body << "\n";
700+
CHECK(result.status == 200);
701+
CHECK(!client.is_body_in_out_buf());
702+
}
703+
685704
{
686705
detail::resize(str, 1024);
687706
coro_http_client client;

0 commit comments

Comments
 (0)