Skip to content

Commit

Permalink
[coro_http_client][bug]fix ssl (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 31, 2024
1 parent 06103b5 commit a920fce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
26 changes: 14 additions & 12 deletions include/ylt/thirdparty/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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;
Expand Down Expand Up @@ -1102,21 +1102,23 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {

async_simple::coro::Lazy<std::error_code> 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);
Expand Down
7 changes: 6 additions & 1 deletion src/coro_http/examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,12 @@ async_simple::coro::Lazy<void> 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
}
Expand Down

0 comments on commit a920fce

Please sign in to comment.