Skip to content
This repository has been archived by the owner on Jul 9, 2023. It is now read-only.

ReRequest fails on RequestUri changes #519

Closed
nirdil opened this issue Nov 5, 2018 · 4 comments
Closed

ReRequest fails on RequestUri changes #519

nirdil opened this issue Nov 5, 2018 · 4 comments
Labels

Comments

@nirdil
Copy link
Contributor

nirdil commented Nov 5, 2018

I need to automatically follow redirect responses internally and return just the final response.
So I did something like this:

public async Task OnResponse(object sender, SessionEventArgs e)
{
    if (e.WebSession.Response.StatusCode >= 300 &&
        e.WebSession.Response.StatusCode <= 307 &&
        e.WebSession.Response.Headers.HeaderExists("Location"))
    {
        Uri redirectUri;
        if (!Uri.TryCreate(e.WebSession.Request.RequestUri, e.WebSession.Response.Headers.GetFirstHeader("Location").Value, out redirectUri))
        {
            e.WebSession.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            e.WebSession.Response.StatusDescription = "Location error";
            return;
        }

        if (e.WebSession.Request.RequestUri.AbsoluteUri.ToLower() != redirectUri.AbsoluteUri.ToLower())
            e.WebSession.Request.RequestUri = redirectUri;
            e.WebSession.Request.Host = e.WebSession.Request.RequestUri.Host;
            e.ReRequest = true;
        }
    }
}

But apparently ReRequest only allows sending the request over an existing server connection, so changing the request uri and requesting a ReRequest does not work.

Here's the relevant responseHandler's code:

// if user requested to send request again
// likely after making modifications from User Response Handler
if (args.ReRequest)
{
    // clear current response
    await args.ClearResponse(cancellationToken);
    await handleHttpSessionRequest(args.WebSession.ServerConnection, args);
    return;
}
@nirdil
Copy link
Contributor Author

nirdil commented Nov 12, 2018

Went ahead and patched it like so:

In ResponseHandler.cs, before user callback invocation, save the request uri:

// if user requested call back then do it
var preCallbackReqUri = args.WebSession.Request.RequestUri;
if (!response.Locked)
{
    await invokeBeforeResponse(args);
}

In ResponseHandler.cs, when handling ReRequest, raise a new connection if necessary:

// if user requested to send request again
// likely after making modifications from User Response Handler
if (args.ReRequest)
{
    // clear current response
    await args.ClearResponse(cancellationToken);
    if ((preCallbackReqUri.Host != args.WebSession.Request.RequestUri.Host) ||
        (preCallbackReqUri.Scheme != args.WebSession.Request.RequestUri.Scheme))
    {
        await tcpConnectionFactory.Release(args.WebSession.ServerConnection, true);
        args.WebSession.ServerConnection = await tcpConnectionFactory.GetServerConnection(this, args, isConnect: false, applicationProtocol: args.ProxyClient.ClientConnection.NegotiatedApplicationProtocol, noCache: false, cancellationToken: cancellationToken);
    }

    await handleHttpSessionRequest(args.WebSession.ServerConnection, args);
    return;
}

Seems to be working fine for my requirements...
@justcoding121 - I would love some input if this makes sense or am I missing/messing up anything.

@nirdil nirdil changed the title ReRequest fails on double attempt ReRequest fails on RequestUri changes Nov 13, 2018
@justcoding121
Copy link
Owner

Thanks I will take a look when I get time

@justcoding121
Copy link
Owner

Should be fixed in latest beta.

@nirdil
Copy link
Contributor Author

nirdil commented Nov 29, 2018

Looking good, thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants