weidazhao / Hosting

Hosting prototype
170 stars 35 forks source link

302 redirects in the gateway #23

Closed rmja closed 8 years ago

rmja commented 8 years ago

The default behaviour in HttpClient is to auto follow 302 redirects, but I am not sure that it is the correct behaviour for the gateway. Instead the default should be to return the 302 to the caller. At least it should be configurable. Basically what is needed is to configure the message handler for the client, so that this becomes something like this instead:

public static IServiceCollection AddDefaultHttpRequestDispatcherProvider(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            services.AddHttpRequestDispatcherProvider(new HttpRequestDispatcherProvider(() =>
            {
                var handler = new WebRequestHandler();
                handler.AllowAutoRedirect = false;

                return new HttpRequestDispatcher(handler);
            }, null, new[] { new AlwaysTreatedAsNonTransientExceptionHandler() }, null));

            return services;
        }
rmja commented 8 years ago

One more thing. Cookies should also be forwarded. According to this post on StackOverflow, then I believe that the default WebRequestHandler configuration should be:

handler.AllowAutoRedirect = false;
handler.UseCookies = false;

I can confirm that by setting handler.UseCookies = false then the headers are forwarded as expected, instead of being silently removed.

weidazhao commented 8 years ago

Thanks for the suggestions. Agree that AllowAutoRedirect and UseCookies should be set to false. Since the extensibility of HttpClient allows to completely replace HttpMessageHandler, I just changed the settings in the default implementation. If users want to provide their own HttpMessageHandler, they will have to decide whether to change the two flags or not.

https://github.com/weidazhao/Hosting/commit/13cf06c375c7dddb9e3b969a785a5607dbc0e5af

@rmja @cwe1ss