diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 36b3b34a..ad8a3bc3 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/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/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index d57f8ac5..2e460cb7 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -66,6 +66,16 @@ TEST_CASE("test ssl client") { CHECK(result.status >= 200); } + { + coro_http_client client{}; + auto r = + async_simple::coro::syncAwait(client.connect("https://www.baidu.com")); + if (r.status == 200) { + auto result = client.get("/"); + CHECK(result.status >= 200); + } + } + { coro_http_client client{}; auto result = client.get("http://www.bing.com"); @@ -518,23 +528,6 @@ TEST_CASE("test bad uri") { CHECK(result.status == 404); } -TEST_CASE("test ssl without init ssl"){{coro_http_client client{}; -client.add_str_part("hello", "world"); -auto result = async_simple::coro::syncAwait( - client.async_upload_multipart("https://www.bing.com")); -CHECK(result.status == 404); -} - -#ifndef CINATRA_ENABLE_SSL -{ - coro_http_client client{}; - auto result = - async_simple::coro::syncAwait(client.async_get("https://www.bing.com")); - CHECK(result.status == 404); -} -#endif -} - TEST_CASE("test multiple ranges download") { coro_http_client client{}; std::string uri = "http://uniquegoodshiningmelody.neverssl.com/favicon.ico";