Skip to content

Commit

Permalink
Added some more context to redirect callback #85
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaa committed Mar 26, 2019
1 parent 8e93037 commit f5c62fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/restc-cpp/restc-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class Request {
class Properties {
public:
using ptr_t = std::shared_ptr<Properties>;
using redirect_fn_t = std::function<void (std::string& url)>;
using redirect_fn_t = std::function<void (int code, std::string& url,
const Reply& reply)>;

int maxRedirects = 3;
int connectTimeoutMs = (1000 * 12);
Expand Down
14 changes: 8 additions & 6 deletions src/RequestImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class RequestImpl : public Request {
RedirectException(const RedirectException&) = default;
RedirectException(RedirectException &&) = default;

RedirectException(int redirCode, string redirUrl)
: code{redirCode}, url{move(redirUrl)} {}
RedirectException(int redirCode, string redirUrl, std::unique_ptr<Reply> reply)
: code{redirCode}, url{move(redirUrl)}, redirectReply{move(reply)}
{}

const int code;
std::string url;
std::unique_ptr<Reply> redirectReply;
};

RequestImpl(const std::string& url,
Expand Down Expand Up @@ -125,12 +127,12 @@ class RequestImpl : public Request {
while(true) {
try {
return DoExecute((ctx));
} catch(RedirectException& ex) {
} catch(const RedirectException& ex) {

auto url = move(ex.url);
auto url = ex.url;

if (properties_->redirectFn) {
properties_->redirectFn(url);
properties_->redirectFn(ex.code, url, *ex.redirectReply);
}

if ((properties_->maxRedirects >= 0)
Expand Down Expand Up @@ -446,7 +448,7 @@ class RequestImpl : public Request {
throw ProtocolException(
"No Location header in redirect reply");
}
throw RedirectException(http_code, *redirect_location);
throw RedirectException(http_code, *redirect_location, move(reply));
}

ValidateReply(*reply);
Expand Down

0 comments on commit f5c62fa

Please sign in to comment.