Skip to content

Commit a10c980

Browse files
committed
Merge pull request #1181 from simue/fix/proxy-authentication-segfault
Fix Seg-fault when setting proxy username + password
1 parent 4380d9a commit a10c980

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

cpr/proxyauth.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "cpr/proxyauth.h"
2+
#include "cpr/util.h"
23
#include <string>
3-
#include <string_view>
44

55
namespace cpr {
66
EncodedAuthentication::~EncodedAuthentication() noexcept {

cpr/session.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ void Session::prepareHeader() {
115115
curl_->chunk = chunk;
116116
}
117117

118+
void Session::prepareProxy() {
119+
const std::string protocol = url_.str().substr(0, url_.str().find(':'));
120+
if (proxies_.has(protocol)) {
121+
curl_easy_setopt(curl_->handle, CURLOPT_PROXY, proxies_[protocol].c_str());
122+
if (proxyAuth_.has(protocol)) {
123+
curl_easy_setopt(curl_->handle, CURLOPT_PROXYUSERNAME, proxyAuth_.GetUsername(protocol));
124+
curl_easy_setopt(curl_->handle, CURLOPT_PROXYPASSWORD, proxyAuth_.GetPassword(protocol));
125+
}
126+
}
127+
}
128+
118129
// Only supported with libcurl >= 7.61.0.
119130
// As an alternative use SetHeader and add the token manually.
120131
#if LIBCURL_VERSION_NUM >= 0x073D00
@@ -172,14 +183,7 @@ void Session::prepareCommonShared() {
172183
}
173184

174185
// Proxy:
175-
const std::string protocol = url_.str().substr(0, url_.str().find(':'));
176-
if (proxies_.has(protocol)) {
177-
curl_easy_setopt(curl_->handle, CURLOPT_PROXY, proxies_[protocol].c_str());
178-
if (proxyAuth_.has(protocol)) {
179-
curl_easy_setopt(curl_->handle, CURLOPT_PROXYUSERNAME, proxyAuth_.GetUsername(protocol));
180-
curl_easy_setopt(curl_->handle, CURLOPT_PROXYPASSWORD, proxyAuth_.GetPassword(protocol));
181-
}
182-
}
186+
prepareProxy();
183187

184188
#if LIBCURL_VERSION_NUM >= 0x071506 // 7.21.6
185189
if (acceptEncoding_.empty()) {
@@ -648,14 +652,7 @@ cpr_off_t Session::GetDownloadFileLength() {
648652
cpr_off_t downloadFileLength = -1;
649653
curl_easy_setopt(curl_->handle, CURLOPT_URL, url_.c_str());
650654

651-
const std::string protocol = url_.str().substr(0, url_.str().find(':'));
652-
if (proxies_.has(protocol)) {
653-
curl_easy_setopt(curl_->handle, CURLOPT_PROXY, proxies_[protocol].c_str());
654-
if (proxyAuth_.has(protocol)) {
655-
curl_easy_setopt(curl_->handle, CURLOPT_PROXYUSERNAME, proxyAuth_.GetUsername(protocol));
656-
curl_easy_setopt(curl_->handle, CURLOPT_PROXYPASSWORD, proxyAuth_.GetPassword(protocol));
657-
}
658-
}
655+
prepareProxy();
659656

660657
curl_easy_setopt(curl_->handle, CURLOPT_HTTPGET, 1);
661658
curl_easy_setopt(curl_->handle, CURLOPT_NOBODY, 1);

cpr/util.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "cpr/cookies.h"
44
#include "cpr/cprtypes.h"
55
#include "cpr/curlholder.h"
6-
#include "cpr/secure_string.h"
76
#include <algorithm>
87
#include <cctype>
98
#include <chrono>

include/cpr/session.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ class Session : public std::enable_shared_from_this<Session> {
296296
**/
297297
void prepareCommonDownload();
298298
void prepareHeader();
299+
void prepareProxy();
299300
CURLcode DoEasyPerform();
300301
void prepareBodyPayloadOrMultipart() const;
301302
/**

test/proxy_auth_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ using namespace cpr;
1717

1818
static HttpServer* server = new HttpServer();
1919

20+
TEST(ProxyAuthTests, SetProxyCredentials) {
21+
Url url{server->GetBaseUrl() + "/hello.html"};
22+
Session session;
23+
session.SetUrl(url);
24+
session.SetProxies(Proxies{{"http", HTTP_PROXY}, {"https", HTTPS_PROXY}});
25+
session.SetProxyAuth({{"http", EncodedAuthentication{PROXY_USER, PROXY_PASS}}, {"https", EncodedAuthentication{PROXY_USER, PROXY_PASS}}});
26+
session.PrepareGet();
27+
EXPECT_TRUE(true);
28+
}
29+
2030
// TODO: These should be fixed after a source code implementation of a proxy
2131
#if defined(false)
2232
TEST(ProxyAuthTests, SingleProxyTest) {

0 commit comments

Comments
 (0)