From 40a81b4fed5e8ac0858c6a8c959dacd40536ff16 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 2 Aug 2023 15:47:11 +0800 Subject: [PATCH] fix ssl_stream --- include/cinatra/coro_http_client.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index a7f4e66b..4e885f2b 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -265,12 +265,12 @@ class coro_http_client { ssl_ctx_->set_verify_callback( asio::ssl::host_name_verification(domain)); - ssl_stream_ = + socket_->ssl_stream_ = std::make_unique>( socket_->impl_, *ssl_ctx_); // Set SNI Hostname (many hosts need this to handshake successfully) if (!sni_hostname_.empty()) { - SSL_set_tlsext_host_name(ssl_stream_->native_handle(), + SSL_set_tlsext_host_name(socket_->ssl_stream_->native_handle(), sni_hostname_.c_str()); } use_ssl_ = true; @@ -937,12 +937,12 @@ class coro_http_client { async_simple::coro::Lazy handle_shake() { #ifdef CINATRA_ENABLE_SSL if (use_ssl_) { - if (ssl_stream_ == nullptr) { + if (socket_->ssl_stream_ == nullptr) { co_return std::make_error_code(std::errc::not_a_stream); } auto ec = co_await coro_io::async_handshake( - ssl_stream_, asio::ssl::stream_base::client); + socket_->ssl_stream_, asio::ssl::stream_base::client); if (ec) { CINATRA_LOG_ERROR << "handle failed " << ec.message(); } @@ -1006,6 +1006,9 @@ class coro_http_client { asio::ip::tcp::socket impl_; std::atomic has_closed_ = true; asio::streambuf read_buf_; +#ifdef CINATRA_ENABLE_SSL + std::unique_ptr> ssl_stream_; +#endif template socket_t(ioc_t &&ioc) : impl_(std::forward(ioc)) {} }; @@ -1573,7 +1576,7 @@ class coro_http_client { AsioBuffer &&buffer, size_t size_to_read) noexcept { #ifdef CINATRA_ENABLE_SSL if (use_ssl_) { - return coro_io::async_read(*ssl_stream_, buffer, size_to_read); + return coro_io::async_read(*socket_->ssl_stream_, buffer, size_to_read); } else { #endif @@ -1588,7 +1591,7 @@ class coro_http_client { AsioBuffer &&buffer) { #ifdef CINATRA_ENABLE_SSL if (use_ssl_) { - return coro_io::async_write(*ssl_stream_, buffer); + return coro_io::async_write(*socket_->ssl_stream_, buffer); } else { #endif @@ -1603,7 +1606,7 @@ class coro_http_client { AsioBuffer &buffer, asio::string_view delim) noexcept { #ifdef CINATRA_ENABLE_SSL if (use_ssl_) { - return coro_io::async_read_until(*ssl_stream_, buffer, delim); + return coro_io::async_read_until(*socket_->ssl_stream_, buffer, delim); } else { #endif @@ -1675,7 +1678,6 @@ class coro_http_client { #ifdef CINATRA_ENABLE_SSL std::unique_ptr ssl_ctx_ = nullptr; - std::unique_ptr> ssl_stream_; bool ssl_init_ret_ = true; bool use_ssl_ = false; std::string sni_hostname_ = "";