Skip to content

Commit

Permalink
fix ssl_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Aug 2, 2023
1 parent 5eebded commit 40a81b4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<asio::ssl::stream<asio::ip::tcp::socket &>>(
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;
Expand Down Expand Up @@ -937,12 +937,12 @@ class coro_http_client {
async_simple::coro::Lazy<std::error_code> 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();
}
Expand Down Expand Up @@ -1006,6 +1006,9 @@ class coro_http_client {
asio::ip::tcp::socket impl_;
std::atomic<bool> has_closed_ = true;
asio::streambuf read_buf_;
#ifdef CINATRA_ENABLE_SSL
std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket &>> ssl_stream_;
#endif
template <typename ioc_t>
socket_t(ioc_t &&ioc) : impl_(std::forward<ioc_t>(ioc)) {}
};
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1675,7 +1678,6 @@ class coro_http_client {

#ifdef CINATRA_ENABLE_SSL
std::unique_ptr<asio::ssl::context> ssl_ctx_ = nullptr;
std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket &>> ssl_stream_;
bool ssl_init_ret_ = true;
bool use_ssl_ = false;
std::string sni_hostname_ = "";
Expand Down

0 comments on commit 40a81b4

Please sign in to comment.