From a920fce8b0d7b6a3a42e459c17ee5b9925d5a2ff Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 31 Jan 2024 15:50:41 +0800 Subject: [PATCH] [coro_http_client][bug]fix ssl (#587) --- .../thirdparty/cinatra/coro_http_client.hpp | 26 ++++++++++--------- src/coro_http/examples/example.cpp | 7 ++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/ylt/thirdparty/cinatra/coro_http_client.hpp b/include/ylt/thirdparty/cinatra/coro_http_client.hpp index 36b3b34a5..ad8a3bc3f 100644 --- a/include/ylt/thirdparty/cinatra/coro_http_client.hpp +++ b/include/ylt/thirdparty/cinatra/coro_http_client.hpp @@ -1045,7 +1045,7 @@ class coro_http_client : public std::enable_shared_from_this { else { host = std::string{u.host}; } - bool r = init_ssl(asio::ssl::verify_peer, "", host); + bool r = init_ssl(asio::ssl::verify_none, "", host); if (!r) { data.net_err = std::make_error_code(std::errc::invalid_argument); co_return data; @@ -1102,21 +1102,23 @@ class coro_http_client : public std::enable_shared_from_this { async_simple::coro::Lazy handle_shake() { #ifdef CINATRA_ENABLE_SSL - if (has_init_ssl_) { - if (socket_->ssl_stream_ == nullptr) { - co_return std::make_error_code(std::errc::not_a_stream); + if (!has_init_ssl_) { + bool r = init_ssl(asio::ssl::verify_none, "", host_); + if (!r) { + co_return std::make_error_code(std::errc::invalid_argument); } + } - auto ec = co_await coro_io::async_handshake( - socket_->ssl_stream_, asio::ssl::stream_base::client); - if (ec) { - CINATRA_LOG_ERROR << "handle failed " << ec.message(); - } - co_return ec; + if (socket_->ssl_stream_ == nullptr) { + co_return std::make_error_code(std::errc::not_a_stream); } - else { - co_return std::error_code{}; + + auto ec = co_await coro_io::async_handshake(socket_->ssl_stream_, + asio::ssl::stream_base::client); + if (ec) { + CINATRA_LOG_ERROR << "handle failed " << ec.message(); } + co_return ec; #else // please open CINATRA_ENABLE_SSL before request https! co_return std::make_error_code(std::errc::protocol_error); diff --git a/src/coro_http/examples/example.cpp b/src/coro_http/examples/example.cpp index 607310e44..a7c81db77 100644 --- a/src/coro_http/examples/example.cpp +++ b/src/coro_http/examples/example.cpp @@ -400,7 +400,12 @@ async_simple::coro::Lazy basic_usage() { // make sure you have install openssl and enable CINATRA_ENABLE_SSL #ifdef CINATRA_ENABLE_SSL coro_http_client client2{}; - result = co_await client2.async_get("https://baidu.com"); + result = co_await client2.async_get("https://www.baidu.com"); + assert(result.status == 200); + + coro_http_client client3{}; + co_await client3.connect("https://www.baidu.com"); + result = co_await client3.async_get("/"); assert(result.status == 200); #endif }