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

add SSL options #276

Merged
merged 7 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 42 additions & 3 deletions cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Session::Impl {
void SetVerifySsl(const VerifySsl& verify);
void SetLimitRate(const LimitRate& limit_rate);
void SetUnixSocket(const UnixSocket& unix_socket);
void SetSslOptions(const SslOptions& options);

Response Delete();
Response Download(std::ofstream& file);
Expand Down Expand Up @@ -350,6 +351,38 @@ void Session::Impl::SetUnixSocket(const UnixSocket& unix_socket) {
}
}

void Session::Impl::SetSslOptions(const SslOptions& opts) {
auto curl = curl_->handle;
if (curl) {
curl_easy_setopt(curl, CURLOPT_SSLCERT, opts.cert_file.c_str());
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, opts.cert_type.c_str());
curl_easy_setopt(curl, CURLOPT_SSLKEY, opts.key_file.c_str());
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, opts.key_type.c_str());
if (!opts.key_pass.empty()) {
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, opts.key_pass.c_str());
}
curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, opts.enable_alpn ? 1L : 0L);
curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_NPN, opts.enable_npn ? 1L : 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, opts.verify_peer ? 1L : 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, opts.verify_host ? 2L : 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, opts.verify_status ? 1L : 0L);
curl_easy_setopt(curl, CURLOPT_SSLVERSION, opts.ssl_version | opts.max_version);
if (!opts.ca_info.empty()) {
curl_easy_setopt(curl, CURLOPT_CAINFO, opts.ca_info.c_str());
}
if (!opts.ca_path.empty()) {
curl_easy_setopt(curl, CURLOPT_CAPATH, opts.ca_path.c_str());
}
if (!opts.crl_file.empty()) {
curl_easy_setopt(curl, CURLOPT_CRLFILE, opts.crl_file.c_str());
}
if (!opts.ciphers.empty()) {
curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, opts.ciphers.c_str());
}
curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, opts.session_id_cache ? 1L : 0L);
}
}

Response Session::Impl::Delete() {
auto curl = curl_->handle;
if (curl) {
Expand Down Expand Up @@ -478,7 +511,12 @@ Response Session::Impl::makeDownloadRequest(CURL* curl, std::ofstream& file) {

auto header = cpr::util::parseHeader(header_string);
return Response{static_cast<std::int32_t>(response_code),
std::string{}, header, raw_url, elapsed, cookies, error};
std::string{},
header,
raw_url,
elapsed,
cookies,
error};
}

Response Session::Impl::makeRequest(CURL* curl) {
Expand All @@ -498,8 +536,8 @@ Response Session::Impl::makeRequest(CURL* curl) {

#if LIBCURL_VERSION_MAJOR >= 7
#if LIBCURL_VERSION_MINOR >= 21
/* enable all supported built-in compressions */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
/* enable all supported built-in compressions */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
#endif
#endif

Expand Down Expand Up @@ -596,6 +634,7 @@ void Session::SetOption(const LowSpeed& low_speed) { pimpl_->SetLowSpeed(low_spe
void Session::SetOption(const VerifySsl& verify) { pimpl_->SetVerifySsl(verify); }
void Session::SetOption(const Verbose& verbose) { pimpl_->SetVerbose(verbose); }
void Session::SetOption(const UnixSocket& unix_socket) { pimpl_->SetUnixSocket(unix_socket); }
void Session::SetOption(const SslOptions& options) { pimpl_->SetSslOptions(options); }

Response Session::Delete() { return pimpl_->Delete(); }
Response Session::Download(std::ofstream& file) { return pimpl_->Download(file); }
Expand Down
11 changes: 11 additions & 0 deletions gen-test-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

mkdir -p test/data
cd test/data

openssl genrsa -out ca.key 2048
openssl req -new -x509 -key ca.key -out ca.crt

openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out cert.csr
openssl x509 -req -in cert.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out cert.pem
1 change: 1 addition & 0 deletions include/cpr/cprtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct CaseInsensitiveCompare {

using Header = std::map<std::string, std::string, CaseInsensitiveCompare>;
using Url = std::string;
struct Verbose {};

} // namespace cpr

Expand Down
10 changes: 6 additions & 4 deletions include/cpr/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@

#include "cpr/auth.h"
#include "cpr/body.h"
#include "cpr/connect_timeout.h"
#include "cpr/cookies.h"
#include "cpr/cprtypes.h"
#include "cpr/digest.h"
#include "cpr/limit_rate.h"
#include "cpr/low_speed.h"
#include "cpr/max_redirects.h"
#include "cpr/multipart.h"
#include "cpr/parameters.h"
#include "cpr/payload.h"
#include "cpr/proxies.h"
#include "cpr/response.h"
#include "cpr/timeout.h"
#include "cpr/connect_timeout.h"
#include "cpr/ssl_options.h"
#include "cpr/timeout.h"
#include "cpr/unix_socket.h"
#include "cpr/user_agent.h"
#include "cpr/verbose.h"
#include "cpr/limit_rate.h"
#include "cpr/unix_socket.h"

namespace cpr {

Expand Down Expand Up @@ -56,6 +55,8 @@ class Session {
void SetLowSpeed(const LowSpeed& low_speed);
void SetVerifySsl(const VerifySsl& verify);
void SetUnixSocket(const UnixSocket& unix_socket);
void SetSslOptions(const SslOptions& options);
void SetVerbose(const Verbose& verbose);

// Used in templated functions
void SetOption(const Url& url);
Expand Down Expand Up @@ -83,6 +84,7 @@ class Session {
void SetOption(const VerifySsl& verify);
void SetOption(const Verbose& verbose);
void SetOption(const UnixSocket& unix_socket);
void SetOption(const SslOptions& options);

Response Delete();
Response Download(std::ofstream& file);
Expand Down
Loading