Skip to content

Commit

Permalink
Find the path to the certificate bundle in runtime
Browse files Browse the repository at this point in the history
The change makes the compiled library work on
different distributions, even if the path to the
certificate bundle in runtime is different compared
to the path where it was during compilation.

This can be useful when the compilation is done on
a manylinux Docker image which is based on CentOS,
but the compiled library/binaries are used on
Ubuntu or Debian.
  • Loading branch information
Matevz Morato committed Nov 5, 2024
1 parent 372b54c commit c6eaf7f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -134,6 +135,31 @@ Session::Session() : curl_(new CurlHolder()) {
curl_easy_setopt(curl_->handle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl_->handle, CURLOPT_ERRORBUFFER, curl_->error.data());
curl_easy_setopt(curl_->handle, CURLOPT_COOKIEFILE, "");

#ifdef __linux__
// Make the binary portable even when curl was compiled on a different distribution
static const char* cert_path = nullptr;

// Find the system certificate store
if (cert_path == nullptr) {
// List of possible paths:
// https://github.com/curl/curl/blob/25d45e89d140d6ab27103cd7f8f6d7d6cf548d47/CMakeLists.txt#L919
static constexpr const char* certificatePaths[] = {"/etc/ssl/certs/ca-certificates.crt", "/etc/pki/tls/certs/ca-bundle.crt", "/usr/share/ssl/certs/ca-bundle.crt", "/usr/local/share/certs/ca-root-nss.crt", "/etc/ssl/cert.pem"};

for (const auto& path : certificatePaths) {
if (std::filesystem::exists(path)) {
cert_path = path;
break;
}
}
}

// Set certificate path
if (cert_path != nullptr) {
curl_easy_setopt(curl_->handle, CURLOPT_CAINFO, cert_path);
}
#endif

#ifdef CPR_CURL_NOSIGNAL
curl_easy_setopt(curl_->handle, CURLOPT_NOSIGNAL, 1L);
#endif
Expand Down

0 comments on commit c6eaf7f

Please sign in to comment.