Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_http_client][fix]init ssl when connect #514

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading