Repository navigation
Follow redirections to relative URLs - #2390
Conversation
|
|
||
| if (!String.IsNullOrEmpty (httpConnection.URL.Ref) && String.IsNullOrEmpty (redirectUrl.Fragment)) { | ||
| if (Logger.LogNet) | ||
| Logger.Log (LogLevel.Debug, LOG_APP, $"Appending fragment '{httpConnection.URL.Ref}' to redirect URL '{redirectUrl}'"); |
There was a problem hiding this comment.
"Ditto"; instead of Logger.Log() calls ~every time you manipulate the URL, could we "get away with" just one message, after line 625 (redirectState.NewUrl = ...), which prints the originating URL and the new URL? Or would that one message not provide enough inference information?
Granted, Logger.LogNet will be disabled by default, but if anyone enables it, this looks like it could be quite verbose, depending on how often the app hits a URL...
There was a problem hiding this comment.
The point of the messages is to show steps which are under-defined/problematic and may effect the outcome. The raw location is printed because we instantiate an Uri later which has issues with the way it parses URIs (e.g. it will bomb on URI like g:h or it will turn /location/test into file:///location/test) and this information is actually useful when debugging problems in this code. For those reasons I left the messages in the form, and where, they are.
|
I'm getting mixed results with the new implementation: Am I missing something? Do you get different results with these parameters? Have you considered using the I have found this source code using the same method, for reference: |
Fixes: dotnet#1923 When a Location header contains an URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
|
@dominik-weber please review the latest changes, it should work correctly for your cases now. The only thing that will not work (because of a bug in the |
|
@grendello awesome, thanks for the update! Actually, I think you can even remove the
So just |
The check avoids having to deal with this: It's simply faster and cleaner to do it this way rather than to check for
Yeah, |
Maybe you misunderstood me there. I think if we used Seems like the Uri(baseUri, relativeUri) constructor was made just for http redirects... ;) |
Fixes: #1923 Consider this code: var client = new HttpClient(new AndroidClientHandler()); var res = await client.GetStringAsync("https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get"); Accessing `https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get` results in an HTTP-302 (redirect) to the location `/httpbin/get`: $ curl -D - https://nghttp2.org/httpbin/redirect-to?url=/httpbin/get HTTP/2 302 date: Wed, 28 Nov 2018 20:52:01 GMT content-type: text/html; charset=utf-8 content-length: 0 location: /httpbin/get ... Expected behavior within Xamarin.Android is that this should work: the HTTP-302 should be followed, with `httpClient.GetStringAsync()` returning the contents of `https://nghttp2.org/httpbin/get`. What instead happens is that an exception is thrown: System.InvalidCastException: Unable to convert instance of type 'Java.Net.URLConnectionInvoker' to type 'Java.Net.HttpURLConnection' Oops. Update `AndroidClientHandler.HandleRedirect()` to improve its support for the `Location` header, such that if the `Location` header value contains a URL that is relative or lacks schema/host, we now use the original request URL to construct the destination URL. Additionally, if the original URL contains a fragment but the destination does not, we append the fragment to the destination.
Fixes: #1923
When a Location header contains an URL that is relative or lacks schema/host, we
now use the original request URL to construct the destination URL. Additionally,
if the original URL contains a fragment but the destination does not, we append
the fragment to the destination.