[coro_http][fix]coro_http_client tcp_no_delay as default, fix response content view (#664)

This commit is contained in:
qicosmos 2024-04-24 16:08:40 +08:00 committed by GitHub
parent cc271bddf2
commit 47b781824e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 14 deletions

View File

@ -2125,7 +2125,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
std::chrono::seconds(8); std::chrono::seconds(8);
std::chrono::steady_clock::duration req_timeout_duration_ = std::chrono::steady_clock::duration req_timeout_duration_ =
std::chrono::seconds(60); std::chrono::seconds(60);
bool enable_tcp_no_delay_ = false; bool enable_tcp_no_delay_ = true;
std::string resp_chunk_str_; std::string resp_chunk_str_;
std::span<char> out_buf_; std::span<char> out_buf_;

View File

@ -392,9 +392,19 @@ class coro_http_connection
co_return true; 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_, false);
return remote_addr_;
}
void set_multi_buf(bool r) { multi_buf_ = r; } void set_multi_buf(bool r) { multi_buf_ = r; }
@ -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_) { if (has_closed_) {
return ""; return;
} }
std::error_code ec; std::error_code ec;
auto pt = remote ? socket_.remote_endpoint(ec) : socket_.local_endpoint(ec); auto pt = remote ? socket_.remote_endpoint(ec) : socket_.local_endpoint(ec);
if (ec) { if (ec) {
return ""; return;
} }
auto addr = pt.address().to_string(ec); address = pt.address().to_string(ec);
if (ec) { if (ec) {
return ""; return;
} }
addr.append(":").append(std::to_string(pt.port())); address.append(":").append(std::to_string(pt.port()));
return addr;
} }
private: private:
@ -898,5 +907,6 @@ class coro_http_connection
std::function<void(coro_http_request &, coro_http_response &)> std::function<void(coro_http_request &, coro_http_response &)>
default_handler_ = nullptr; default_handler_ = nullptr;
std::string chunk_size_str_; std::string chunk_size_str_;
std::string remote_addr_;
}; };
} // namespace cinatra } // namespace cinatra

View File

@ -173,8 +173,11 @@ class coro_http_response {
: resp_str.append(CONN_CLOSE_SV); : resp_str.append(CONN_CLOSE_SV);
} }
if (!content_type_.empty()) { if (content_view_.empty()) {
resp_str.append(content_type_); resp_str.append(content_);
}
else {
resp_str.append(content_view_);
} }
append_header_str(resp_str, resp_headers_); append_header_str(resp_str, resp_headers_);

View File

@ -863,8 +863,8 @@ class coro_http_server {
} }
void init_address(std::string address) { void init_address(std::string address) {
CINATRA_LOG_ERROR << "init log"; // init easylog singleton to make sure easylog::logger<>::instance(); // init easylog singleton to make sure
// server destruct before easylog. // server destruct before easylog.
if (size_t pos = address.find(':'); pos != std::string::npos) { if (size_t pos = address.find(':'); pos != std::string::npos) {
auto port_sv = std::string_view(address).substr(pos + 1); auto port_sv = std::string_view(address).substr(pos + 1);

View File

@ -317,6 +317,8 @@ async_simple::coro::Lazy<void> basic_usage() {
server.set_http_handler<POST, PUT>( server.set_http_handler<POST, PUT>(
"/post", [](coro_http_request &req, coro_http_response &resp) { "/post", [](coro_http_request &req, coro_http_response &resp) {
assert(resp.get_conn()->remote_address().find("127.0.0.1") !=
std::string::npos);
assert(resp.get_conn()->remote_address().find("127.0.0.1") != assert(resp.get_conn()->remote_address().find("127.0.0.1") !=
std::string::npos); std::string::npos);
assert(resp.get_conn()->local_address() == "127.0.0.1:9001"); assert(resp.get_conn()->local_address() == "127.0.0.1:9001");