Skip to content

Commit e7d481e

Browse files
authored
[coro_http_client][fix]init ssl when connect (#514)
1 parent 04ee6a9 commit e7d481e

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

include/cinatra/coro_http_client.hpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
10451045
else {
10461046
host = std::string{u.host};
10471047
}
1048-
bool r = init_ssl(asio::ssl::verify_peer, "", host);
1048+
bool r = init_ssl(asio::ssl::verify_none, "", host);
10491049
if (!r) {
10501050
data.net_err = std::make_error_code(std::errc::invalid_argument);
10511051
co_return data;
@@ -1102,21 +1102,23 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
11021102

11031103
async_simple::coro::Lazy<std::error_code> handle_shake() {
11041104
#ifdef CINATRA_ENABLE_SSL
1105-
if (has_init_ssl_) {
1106-
if (socket_->ssl_stream_ == nullptr) {
1107-
co_return std::make_error_code(std::errc::not_a_stream);
1105+
if (!has_init_ssl_) {
1106+
bool r = init_ssl(asio::ssl::verify_none, "", host_);
1107+
if (!r) {
1108+
co_return std::make_error_code(std::errc::invalid_argument);
11081109
}
1110+
}
11091111

1110-
auto ec = co_await coro_io::async_handshake(
1111-
socket_->ssl_stream_, asio::ssl::stream_base::client);
1112-
if (ec) {
1113-
CINATRA_LOG_ERROR << "handle failed " << ec.message();
1114-
}
1115-
co_return ec;
1112+
if (socket_->ssl_stream_ == nullptr) {
1113+
co_return std::make_error_code(std::errc::not_a_stream);
11161114
}
1117-
else {
1118-
co_return std::error_code{};
1115+
1116+
auto ec = co_await coro_io::async_handshake(socket_->ssl_stream_,
1117+
asio::ssl::stream_base::client);
1118+
if (ec) {
1119+
CINATRA_LOG_ERROR << "handle failed " << ec.message();
11191120
}
1121+
co_return ec;
11201122
#else
11211123
// please open CINATRA_ENABLE_SSL before request https!
11221124
co_return std::make_error_code(std::errc::protocol_error);

tests/test_cinatra.cpp

+10-17
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ TEST_CASE("test ssl client") {
6666
CHECK(result.status >= 200);
6767
}
6868

69+
{
70+
coro_http_client client{};
71+
auto r =
72+
async_simple::coro::syncAwait(client.connect("https://www.baidu.com"));
73+
if (r.status == 200) {
74+
auto result = client.get("/");
75+
CHECK(result.status >= 200);
76+
}
77+
}
78+
6979
{
7080
coro_http_client client{};
7181
auto result = client.get("http://www.bing.com");
@@ -518,23 +528,6 @@ TEST_CASE("test bad uri") {
518528
CHECK(result.status == 404);
519529
}
520530

521-
TEST_CASE("test ssl without init ssl"){{coro_http_client client{};
522-
client.add_str_part("hello", "world");
523-
auto result = async_simple::coro::syncAwait(
524-
client.async_upload_multipart("https://www.bing.com"));
525-
CHECK(result.status == 404);
526-
}
527-
528-
#ifndef CINATRA_ENABLE_SSL
529-
{
530-
coro_http_client client{};
531-
auto result =
532-
async_simple::coro::syncAwait(client.async_get("https://www.bing.com"));
533-
CHECK(result.status == 404);
534-
}
535-
#endif
536-
}
537-
538531
TEST_CASE("test multiple ranges download") {
539532
coro_http_client client{};
540533
std::string uri = "http://uniquegoodshiningmelody.neverssl.com/favicon.ico";

0 commit comments

Comments
 (0)