From 42d4844316bfffafa183864f6ae2ffb6671f4a1a Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 24 Apr 2024 20:18:14 +0800 Subject: [PATCH] some fix (#563) --- include/cinatra/coro_http_client.hpp | 3 +-- include/cinatra/coro_http_connection.hpp | 28 ++++++++++++++++-------- include/cinatra/coro_http_response.hpp | 7 ++++-- include/cinatra/coro_http_server.hpp | 6 +++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 7dcb4157..1cd3d087 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -1722,7 +1722,6 @@ class coro_http_client : public std::enable_shared_from_this { if (chunk_size == 0) { // all finished, no more data chunked_buf_.consume(chunked_buf_.size()); - data.status = 200; data.eof = true; break; } @@ -2125,7 +2124,7 @@ class coro_http_client : public std::enable_shared_from_this { 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 out_buf_; diff --git a/include/cinatra/coro_http_connection.hpp b/include/cinatra/coro_http_connection.hpp index 749f6d63..c0cf383b 100644 --- a/include/cinatra/coro_http_connection.hpp +++ b/include/cinatra/coro_http_connection.hpp @@ -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; } @@ -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: @@ -898,5 +907,6 @@ class coro_http_connection std::function default_handler_ = nullptr; std::string chunk_size_str_; + std::string remote_addr_; }; } // namespace cinatra diff --git a/include/cinatra/coro_http_response.hpp b/include/cinatra/coro_http_response.hpp index af9e8c6c..0da9eba5 100644 --- a/include/cinatra/coro_http_response.hpp +++ b/include/cinatra/coro_http_response.hpp @@ -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_); diff --git a/include/cinatra/coro_http_server.hpp b/include/cinatra/coro_http_server.hpp index b9f0cb6f..0bb6b66c 100644 --- a/include/cinatra/coro_http_server.hpp +++ b/include/cinatra/coro_http_server.hpp @@ -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() + 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);