Skip to content

Commit

Permalink
improve when content-len is 0 (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jun 26, 2023
1 parent eab89d8 commit 6818b2c
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions thirdparty/cinatra/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,11 @@ class coro_http_client {
}
#endif

// it may return nullptr, because maybe no body_ allocated, the read_buf_ may
// read all head and body, at that time the body_ is nullptr.
// return body_, the user will own body's lifetime.
std::unique_ptr<char[]> release_buf() {
if (body_.size() == 0) {
return nullptr;
}
return std::unique_ptr<char[]>(body_.release());
}

Expand Down Expand Up @@ -1228,10 +1230,12 @@ class coro_http_client {
if ((size_t)parser.body_len() <= read_buf_.size()) {
// Now get entire content, additional data will discard.
// copy body.
body_.init(content_len);
auto data_ptr = asio::buffer_cast<const char *>(read_buf_.data());
memcpy(body_.data(), data_ptr, content_len);
read_buf_.consume(read_buf_.size());
if (content_len > 0) {
body_.init(content_len);
auto data_ptr = asio::buffer_cast<const char *>(read_buf_.data());
memcpy(body_.data(), data_ptr, content_len);
read_buf_.consume(read_buf_.size());
}
co_await handle_entire_content(data, content_len, is_ranges, ctx);
break;
}
Expand Down

0 comments on commit 6818b2c

Please sign in to comment.