Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix static local #560

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
size_t rd_size =
source->read(file_data.data(), file_data.size()).gcount();
std::vector<asio::const_buffer> bufs;
cinatra::to_chunked_buffers(bufs, {file_data.data(), rd_size},
std::string size_str;
cinatra::to_chunked_buffers(bufs, size_str, {file_data.data(), rd_size},
source->eof());
if (std::tie(ec, size) = co_await async_write(bufs); ec) {
break;
Expand All @@ -1005,7 +1006,8 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
auto [rd_ec, rd_size] =
co_await file.async_read(file_data.data(), file_data.size());
std::vector<asio::const_buffer> bufs;
cinatra::to_chunked_buffers(bufs, {file_data.data(), rd_size},
std::string size_str;
cinatra::to_chunked_buffers(bufs, size_str, {file_data.data(), rd_size},
file.eof());
if (std::tie(ec, size) = co_await async_write(bufs); ec) {
break;
Expand All @@ -1016,8 +1018,9 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
while (true) {
auto result = co_await source();
std::vector<asio::const_buffer> bufs;
std::string size_str;
cinatra::to_chunked_buffers(
bufs, {result.buf.data(), result.buf.size()}, result.eof);
bufs, size_str, {result.buf.data(), result.buf.size()}, result.eof);
if (std::tie(ec, size) = co_await async_write(bufs); ec) {
break;
}
Expand Down
53 changes: 23 additions & 30 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class coro_http_connection
size_t size;
if (multi_buf_) {
if (need_to_bufffer) {
response_.to_buffers(buffers_);
response_.to_buffers(buffers_, chunk_size_str_);
}
std::tie(ec, size) = co_await async_write(buffers_);
}
Expand All @@ -392,35 +392,9 @@ class coro_http_connection
co_return true;
}

std::string local_address() {
if (has_closed_) {
return "";
}
std::string local_address() { return get_address_impl(false); }

std::stringstream ss;
std::error_code ec;
ss << socket_.local_endpoint(ec);
if (ec) {
return "";
}
return ss.str();
}

std::string remote_address() {
static std::string remote_addr;
if (has_closed_) {
return remote_addr;
}

std::stringstream ss;
std::error_code ec;
ss << socket_.remote_endpoint(ec);
if (ec) {
return remote_addr;
}
remote_addr = ss.str();
return ss.str();
}
std::string remote_address() { return get_address_impl(); }

void set_multi_buf(bool r) { multi_buf_ = r; }

Expand Down Expand Up @@ -459,7 +433,7 @@ class coro_http_connection
bool eof = false) {
response_.set_delay(true);
buffers_.clear();
to_chunked_buffers(buffers_, chunked_data, eof);
to_chunked_buffers(buffers_, chunk_size_str_, chunked_data, eof);
co_return co_await reply(false);
}

Expand Down Expand Up @@ -869,6 +843,24 @@ class coro_http_connection
}
}

std::string get_address_impl(bool remote = true) {
if (has_closed_) {
return "";
}

std::error_code ec;
auto pt = remote ? socket_.remote_endpoint(ec) : socket_.local_endpoint(ec);
if (ec) {
return "";
}
auto addr = pt.address().to_string(ec);
if (ec) {
return "";
}
addr.append(":").append(std::to_string(pt.port()));
return addr;
}

private:
friend class multipart_reader_t<coro_http_connection>;
async_simple::Executor *executor_;
Expand Down Expand Up @@ -905,5 +897,6 @@ class coro_http_connection
bool multi_buf_ = true;
std::function<void(coro_http_request &, coro_http_response &)>
default_handler_ = nullptr;
std::string chunk_size_str_;
};
} // namespace cinatra
11 changes: 6 additions & 5 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ class coro_http_response {

std::string_view get_boundary() { return boundary_; }

void to_buffers(std::vector<asio::const_buffer> &buffers) {
void to_buffers(std::vector<asio::const_buffer> &buffers,
std::string &size_str) {
buffers.push_back(asio::buffer(to_http_status_string(status_)));
build_resp_head(buffers);
if (!content_.empty()) {
handle_content(buffers, content_);
handle_content(buffers, size_str, content_);
}
else if (!content_view_.empty()) {
handle_content(buffers, content_view_);
handle_content(buffers, size_str, content_view_);
}
}

Expand Down Expand Up @@ -293,9 +294,9 @@ class coro_http_response {

private:
void handle_content(std::vector<asio::const_buffer> &buffers,
std::string_view content) {
std::string &size_str, std::string_view content) {
if (fmt_type_ == format_type::chunked) {
to_chunked_buffers(buffers, content, true);
to_chunked_buffers(buffers, size_str, content, true);
}
else {
buffers.push_back(asio::buffer(content));
Expand Down
13 changes: 6 additions & 7 deletions include/cinatra/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,17 @@ inline std::string_view trim_sv(std::string_view v) {
return v;
}

inline std::string_view to_hex_string(size_t val) {
static char buf[20];
auto [ptr, ec] = std::to_chars(std::begin(buf), std::end(buf), val, 16);
return std::string_view{buf, size_t(std::distance(buf, ptr))};
}

inline void to_chunked_buffers(std::vector<asio::const_buffer> &buffers,
std::string &size_str,
std::string_view chunk_data, bool eof) {
size_t length = chunk_data.size();
if (length > 0) {
// convert bytes transferred count to a hex string.
auto chunk_size = to_hex_string(length);
detail::resize(size_str, 20);
auto [ptr, ec] =
std::to_chars(size_str.data(), size_str.data() + 20, length, 16);
std::string_view chunk_size{size_str.data(),
size_t(std::distance(size_str.data(), ptr))};

// Construct chunk based on rfc2616 section 3.6.1
buffers.push_back(asio::buffer(chunk_size));
Expand Down
Loading