diff --git a/include/restc-cpp/restc-cpp.h b/include/restc-cpp/restc-cpp.h index 8527e7a..8380701 100644 --- a/include/restc-cpp/restc-cpp.h +++ b/include/restc-cpp/restc-cpp.h @@ -142,6 +142,7 @@ class Request { class Properties { public: using ptr_t = std::shared_ptr; + using redirect_fn_t = std::function; int maxRedirects = 3; int connectTimeoutMs = (1000 * 12); @@ -155,6 +156,7 @@ class Request { headers_t headers; args_t args; Proxy proxy; + redirect_fn_t redirectFn; }; virtual const Properties& GetProperties() const = 0; diff --git a/src/RequestImpl.cpp b/src/RequestImpl.cpp index bf7c862..8d04020 100644 --- a/src/RequestImpl.cpp +++ b/src/RequestImpl.cpp @@ -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."); @@ -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 }