Skip to content

Commit 731a212

Browse files
bas-vkmrtazz
authored andcommitted
Add support for limiting the number of redirects
1 parent 3b2d507 commit 731a212

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ conn->SetUserAgent("foo/cool");
6565

6666
// enable following of redirects (default is off)
6767
conn->FollowRedirects(true);
68+
// and limit the number of redirects (default is -1, unlimited)
69+
conn->FollowRedirects(true, 3);
6870

6971
// set headers
7072
RestClient::HeaderFields headers;

include/restclient-cpp/connection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class Connection {
7979
* Member 'timeout' contains the configured timeout
8080
* @var Info::followRedirects
8181
* Member 'followRedirects' contains whether or not to follow redirects
82+
* @var Info::maxRedirects
83+
* Member 'maxRedirects' contains the maximum number of redirect to follow (-1 unlimited)
8284
* @var Info::basicAuth
8385
* Member 'basicAuth' contains information about basic auth
8486
* @var basicAuth::username
@@ -105,6 +107,7 @@ class Connection {
105107
RestClient::HeaderFields headers;
106108
int timeout;
107109
bool followRedirects;
110+
int maxRedirects;
108111
bool noSignal;
109112
struct {
110113
std::string username;
@@ -138,6 +141,9 @@ class Connection {
138141
// set whether to follow redirects
139142
void FollowRedirects(bool follow);
140143

144+
// set whether to follow redirects (-1 for unlimited)
145+
void FollowRedirects(bool follow, int maxRedirects);
146+
141147
// set custom user agent
142148
// (this will result in the UA "foo/cool restclient-cpp/VERSION")
143149
void SetUserAgent(const std::string& userAgent);
@@ -191,6 +197,7 @@ class Connection {
191197
RestClient::HeaderFields headerFields;
192198
int timeout;
193199
bool followRedirects;
200+
int maxRedirects;
194201
bool noSignal;
195202
struct {
196203
std::string username;

source/connection.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ RestClient::Connection::Connection(const std::string& baseUrl)
3434
this->baseUrl = baseUrl;
3535
this->timeout = 0;
3636
this->followRedirects = false;
37+
this->maxRedirects = -1l;
3738
this->noSignal = false;
3839
}
3940

@@ -57,6 +58,7 @@ RestClient::Connection::GetInfo() {
5758
ret.headers = this->GetHeaders();
5859
ret.timeout = this->timeout;
5960
ret.followRedirects = this->followRedirects;
61+
ret.maxRedirects = this->maxRedirects;
6062
ret.noSignal = this->noSignal;
6163
ret.basicAuth.username = this->basicAuth.username;
6264
ret.basicAuth.password = this->basicAuth.password;
@@ -120,6 +122,19 @@ RestClient::Connection::GetHeaders() {
120122
void
121123
RestClient::Connection::FollowRedirects(bool follow) {
122124
this->followRedirects = follow;
125+
this->maxRedirects = -1l;
126+
}
127+
128+
/**
129+
* @brief configure whether to follow redirects on this connection
130+
*
131+
* @param follow - boolean whether to follow redirects
132+
* @param maxRedirects - int indicating the maximum number of redirect to follow (-1 unlimited)
133+
*/
134+
void
135+
RestClient::Connection::FollowRedirects(bool follow, int maxRedirects) {
136+
this->followRedirects = follow;
137+
this->maxRedirects = maxRedirects;
123138
}
124139

125140
/**
@@ -327,6 +342,8 @@ RestClient::Connection::performCurlRequest(const std::string& uri) {
327342
// set follow redirect
328343
if (this->followRedirects == true) {
329344
curl_easy_setopt(this->curlHandle, CURLOPT_FOLLOWLOCATION, 1L);
345+
curl_easy_setopt(this->curlHandle, CURLOPT_MAXREDIRS,
346+
static_cast<int64_t>(this->maxRedirects));
330347
}
331348

332349
if (this->noSignal) {

test/test_connection.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ TEST_F(ConnectionTest, TestFollowRedirect)
185185
EXPECT_EQ(200, res.code);
186186
}
187187

188+
TEST_F(ConnectionTest, TestFollowRedirectLimited)
189+
{
190+
RestClient::Response res = conn->get("/redirect/2");
191+
EXPECT_EQ(302, res.code);
192+
conn->FollowRedirects(true, 1);
193+
res = conn->get("/redirect/2");
194+
EXPECT_EQ(-1, res.code);
195+
conn->FollowRedirects(true, 2);
196+
res = conn->get("/redirect/2");
197+
EXPECT_EQ(200, res.code);
198+
conn->FollowRedirects(true, -1);
199+
res = conn->get("/redirect/2");
200+
EXPECT_EQ(200, res.code);
201+
}
202+
188203
TEST_F(ConnectionTest, TestGetInfoFromRedirect)
189204
{
190205
conn->FollowRedirects(true);

0 commit comments

Comments
 (0)