restc-cpp icon indicating copy to clipboard operation
restc-cpp copied to clipboard

Add functor to deal with redirect

Open Geramy opened this issue 6 years ago • 5 comments

We can call SendRequest and GetReply, but we cannot use the RedirectException, because its not in the header, its in the Implementation .cpp file. It should be in the header so we can handle the exception and get the data. @jgaa

Geramy avatar Mar 19 '19 15:03 Geramy

I propose putting it in error.h

struct RedirectException : public RestcCppException {
    RedirectException(const RedirectException&) = default;
    RedirectException(RedirectException &&) = default;

    RedirectException(int redirCode, std::string redirUrl)
    : code{redirCode}, url{std::move(redirUrl)}, RestcCppException("Redirect Exception") {}

    const int code;
    std::string url;
};

Geramy avatar Mar 19 '19 15:03 Geramy

For some reason I still can't actually catch the exception, I just get SIGABRT.

Geramy avatar Mar 19 '19 16:03 Geramy

This is an internal exception where the client either retries the request with the redirect-url or gives up, based on the redirect settings.

At the time the server requests a redirect, the client have built up state for the request, and it tries to re-use as much state as possible for the redirected request.

Aborting the request and then building a new one would, under normal circumstances, waste resources. However, if you want to filter or inspect the redirect request before it's executed (or rejected), a predicate functor could accomplish that.

So what I need to know is what your target is? What do you want to do when the server sends a redirect request?

jgaa avatar Mar 20 '19 05:03 jgaa

I would like to catch the redirect myself and figure out if I want to continue through the redirection or not. But I would also like to read the Location of the redirect and any other header information.

Geramy avatar Mar 20 '19 15:03 Geramy

I have added a callback that can look at redirects in "staging" branch. I'll have to write some tests and document it before I can release it in the master branch.

The callback can be set in the Properties, and you can inspect and modify the redirect url. You can throw an exception to abort the redirect. You also have access to the server-reply with all the headers.

jgaa avatar Mar 26 '19 17:03 jgaa