Skip to content

Commit

Permalink
fix out buf (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Aug 12, 2024
1 parent 4a1c14f commit 98528db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1733,11 +1733,14 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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 =
Expand All @@ -1750,6 +1753,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}

if (parser_.is_multipart()) {
out_buf_ = {};
is_keep_alive = true;
if (head_buf_.size() > 0) {
const char *data_ptr =
Expand Down Expand Up @@ -1786,7 +1790,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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_ = {};
Expand Down
19 changes: 19 additions & 0 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GET>(
"/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;
Expand All @@ -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<char>{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;
Expand Down

0 comments on commit 98528db

Please sign in to comment.