Skip to content

Commit

Permalink
[coro_http_client][fix]init ssl when connect (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 31, 2024
1 parent 04ee6a9 commit e7d481e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
26 changes: 14 additions & 12 deletions include/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
27 changes: 10 additions & 17 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit e7d481e

Please sign in to comment.