Skip to content

Commit

Permalink
Implementing callback for redirect processing #85
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaa committed Mar 26, 2019
1 parent 5a8abb5 commit 8e93037
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/restc-cpp/restc-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Request {
class Properties {
public:
using ptr_t = std::shared_ptr<Properties>;
using redirect_fn_t = std::function<void (std::string& url)>;

int maxRedirects = 3;
int connectTimeoutMs = (1000 * 12);
Expand All @@ -155,6 +156,7 @@ class Request {
headers_t headers;
args_t args;
Proxy proxy;
redirect_fn_t redirectFn;
};

virtual const Properties& GetProperties() const = 0;
Expand Down
11 changes: 9 additions & 2 deletions src/RequestImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ class RequestImpl : public Request {
try {
return DoExecute((ctx));
} catch(RedirectException& ex) {

auto url = move(ex.url);

if (properties_->redirectFn) {
properties_->redirectFn(url);
}

if ((properties_->maxRedirects >= 0)
&& (++redirects > properties_->maxRedirects)) {
throw ConstraintException("Too many redirects.");
Expand All @@ -135,9 +142,9 @@ class RequestImpl : public Request {
<< ex.code
<< ") '" << url_
<< "' --> '"
<< ex.url
<< url
<< "') ";
url_ = move(ex.url);
url_ = move(url);
parsed_url_ = url_.c_str();
add_url_args_ = false; // Use whatever arguments we got in the redirect
}
Expand Down

0 comments on commit 8e93037

Please sign in to comment.