Skip to content
Closed
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
13 changes: 8 additions & 5 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ class ClientImpl {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
void set_ca_cert_path(const std::string &ca_cert_file_path,
const std::string &ca_cert_dir_path = std::string());
void set_ca_cert_store(X509_STORE *ca_cert_store);
virtual void set_ca_cert_store(X509_STORE *ca_cert_store);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove virtual. It's not necessary.

X509_STORE *create_ca_cert_store(const char *ca_cert, std::size_t size) const;
#endif

Expand Down Expand Up @@ -8984,7 +8984,9 @@ inline bool ClientImpl::create_redirect_client(
}

// Handle CA certificate store and paths if available
if (ca_cert_store_) { redirect_client.set_ca_cert_store(ca_cert_store_); }
if (ca_cert_store_ && X509_STORE_up_ref(ca_cert_store_)) {
redirect_client.set_ca_cert_store(ca_cert_store_);
}
if (!ca_cert_file_path_.empty()) {
redirect_client.set_ca_cert_path(ca_cert_file_path_, ca_cert_dir_path_);
}
Expand Down Expand Up @@ -10871,6 +10873,7 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) {
// Free memory allocated for old cert and use new store `ca_cert_store`
SSL_CTX_set_cert_store(ctx_, ca_cert_store);
ca_cert_store_ = ca_cert_store;
}
} else {
X509_STORE_free(ca_cert_store);
Expand Down Expand Up @@ -11857,7 +11860,7 @@ inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path,

inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) {
if (is_ssl_) {
static_cast<SSLClient &>(*cli_).set_ca_cert_store(ca_cert_store);
dynamic_cast<SSLClient &>(*cli_).set_ca_cert_store(ca_cert_store);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert it to static_cast?

} else {
cli_->set_ca_cert_store(ca_cert_store);
}
Expand All @@ -11869,13 +11872,13 @@ inline void Client::load_ca_cert_store(const char *ca_cert, std::size_t size) {

inline long Client::get_openssl_verify_result() const {
if (is_ssl_) {
return static_cast<SSLClient &>(*cli_).get_openssl_verify_result();
return dynamic_cast<SSLClient &>(*cli_).get_openssl_verify_result();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert it to static_cast, too?

}
return -1; // NOTE: -1 doesn't match any of X509_V_ERR_???
}

inline SSL_CTX *Client::ssl_context() const {
if (is_ssl_) { return static_cast<SSLClient &>(*cli_).ssl_context(); }
if (is_ssl_) { return dynamic_cast<SSLClient &>(*cli_).ssl_context(); }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert it to static_cast, too?

return nullptr;
}
#endif
Expand Down
39 changes: 39 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8963,6 +8963,45 @@ TEST(HttpToHttpsRedirectTest, CertFile) {
ASSERT_EQ(StatusCode::OK_200, res->status);
}

TEST(SSLClientRedirectTest, CertFile) {
SSLServer ssl_svr1(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(ssl_svr1.is_valid());
ssl_svr1.Get("/index", [&](const Request &, Response &res) {
res.set_redirect("https://127.0.0.1:1235/index");
ssl_svr1.stop();
});

SSLServer ssl_svr2(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(ssl_svr2.is_valid());
ssl_svr2.Get("/index", [&](const Request &, Response &res) {
res.set_content("test", "text/plain");
ssl_svr2.stop();
});

thread t = thread([&]() { ASSERT_TRUE(ssl_svr1.listen("127.0.0.1", PORT)); });
thread t2 = thread([&]() { ASSERT_TRUE(ssl_svr2.listen("127.0.0.1", 1235)); });
auto se = detail::scope_exit([&] {
t2.join();
t.join();
ASSERT_FALSE(ssl_svr1.is_running());
});

ssl_svr1.wait_until_ready();
ssl_svr2.wait_until_ready();

SSLClient cli("127.0.0.1", PORT);
std::string cert;
read_file(SERVER_CERT2_FILE, cert);
cli.load_ca_cert_store(cert.c_str(), cert.size());
cli.enable_server_certificate_verification(true);
cli.set_follow_location(true);
cli.set_connection_timeout(30);

auto res = cli.Get("/index");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
}

TEST(MultipartFormDataTest, LargeData) {
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);

Expand Down
Loading