Skip to content

Commit

Permalink
some fix (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Apr 24, 2024
1 parent 46f35eb commit 42d4844
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
3 changes: 1 addition & 2 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,6 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
if (chunk_size == 0) {
// all finished, no more data
chunked_buf_.consume(chunked_buf_.size());
data.status = 200;
data.eof = true;
break;
}
Expand Down Expand Up @@ -2125,7 +2124,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::chrono::seconds(8);
std::chrono::steady_clock::duration req_timeout_duration_ =
std::chrono::seconds(60);
bool enable_tcp_no_delay_ = false;
bool enable_tcp_no_delay_ = true;
std::string resp_chunk_str_;
std::span<char> out_buf_;

Expand Down
28 changes: 19 additions & 9 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,19 @@ class coro_http_connection
co_return true;
}

std::string local_address() { return get_address_impl(false); }
std::string local_address() {
std::string addr;
set_address_impl(addr, false);
return addr;
}

std::string remote_address() { return get_address_impl(); }
std::string remote_address() {
if (!remote_addr_.empty()) {
return remote_addr_;
}
set_address_impl(remote_addr_);
return remote_addr_;
}

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

Expand Down Expand Up @@ -843,22 +853,21 @@ class coro_http_connection
}
}

std::string get_address_impl(bool remote = true) {
void set_address_impl(std::string &address, bool remote = true) {
if (has_closed_) {
return "";
return;
}

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

private:
Expand Down Expand Up @@ -898,5 +907,6 @@ class coro_http_connection
std::function<void(coro_http_request &, coro_http_response &)>
default_handler_ = nullptr;
std::string chunk_size_str_;
std::string remote_addr_;
};
} // namespace cinatra
7 changes: 5 additions & 2 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ class coro_http_response {
: resp_str.append(CONN_CLOSE_SV);
}

if (!content_type_.empty()) {
resp_str.append(content_type_);
if (content_view_.empty()) {
resp_str.append(content_);
}
else {
resp_str.append(content_view_);
}

append_header_str(resp_str, resp_headers_);
Expand Down
6 changes: 4 additions & 2 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,10 @@ class coro_http_server {
}

void init_address(std::string address) {
CINATRA_LOG_ERROR << "init log"; // init easylog singleton to make sure
// server destruct before easylog.
#if __has_include(<ylt/easylog.hpp>)
easylog::logger<>::instance(); // init easylog singleton to make sure
// server destruct before easylog.
#endif
if (size_t pos = address.find(':'); pos != std::string::npos) {
auto port_sv = std::string_view(address).substr(pos + 1);

Expand Down

0 comments on commit 42d4844

Please sign in to comment.