Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor WASM - Set Fetch redirect to manual #44740

Closed
LukeTOBrien opened this issue Nov 15, 2020 · 5 comments
Closed

Blazor WASM - Set Fetch redirect to manual #44740

LukeTOBrien opened this issue Nov 15, 2020 · 5 comments

Comments

@LukeTOBrien
Copy link

Woah this is long, I did not want this... I feel guilty adding another issue when you have 2.4K 😲

Problem/what I am trying to do

Many sites use shortlinks to allow users to share links to a blog page or media etc. The case I am working with is Sketchfab.
A shortlink like https://skfb.ly/WVBN points to a 3D model.
I noticed that the shortlink is resolved by performing another GET request to https://sketchfab.com/s/WVBN, which inturn redirects to the full URL of the 3D model.

The request to https://sketchfab.com/s/WVBN returns a 301 status code with the URL of the 3D model caintained in the location header.
So what I would like to do is to make a request to resolve to shortlink and read the location header and not auto-follow the redirect.

The JavaScript fetch API takes a parameter for redirect, the options are:

  • follow
  • error
  • manual

The following code gives a not supported on platform exception:

new HttpClientHandler
{
    AllowAutoRedirect = false
}

After upgrading my project to .Net5 the following code seems to prevent the rediect via way of a canceled request:
(This did not work before updating)

request.SetBrowserRequestOption("redirect", "manual");

Make the request.

var response = await http.GetAsync("https://skfb.ly/WVBN");

301-cancelled

In my code I can also log the status code, but the status code is 0.
Console.WriteLine("STATUS CODE " + response.StatusCode.ToString());

What I would expect is the canceled request not to be made at all and for the response status code to be 301 with the location header value set.

As an aside I also note the HttpResponseMessage.IsSuccessStatusCode Property checks for ranges 200-299, but for me a 3xx redirect is also a success status code

Describe the solution you'd like

HttpClientHandler.AllowAutoRedirect Property should work on the Blazor WASM platform and the following code should set the redirect parameter to manual on the Fetch Api:
And the response should return with status code 301 with the location header set as it does in the F12 dev tools.

new HttpClientHandler
{
    AllowAutoRedirect = false
}

Additional context

I have read #41394 (resposible for request.SetBrowserRequestOption("redirect", "manual");?)
And I have read #39365

@campersau
Copy link
Contributor

Not sure if it is possible to get that information in the browser (not everything in the dev tools is available from JavaScript). FYI: #41394 is only in the 6.0.0 milestone and not in the current 5.0.0 release.

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/aspnetcore Nov 16, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Net.Http untriaged New issue has not been triaged by the area owner labels Nov 16, 2020
@ghost
Copy link

ghost commented Nov 16, 2020

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details
Description:

Woah this is long, I did not want this... I feel guilty adding another issue when you have 2.4K 😲

Problem/what I am trying to do

Many sites use shortlinks to allow users to share links to a blog page or media etc. The case I am working with is Sketchfab.
A shortlink like https://skfb.ly/WVBN points to a 3D model.
I noticed that the shortlink is resolved by performing another GET request to https://sketchfab.com/s/WVBN, which inturn redirects to the full URL of the 3D model.

The request to https://sketchfab.com/s/WVBN returns a 301 status code with the URL of the 3D model caintained in the location header.
So what I would like to do is to make a request to resolve to shortlink and read the location header and not auto-follow the redirect.

The JavaScript fetch API takes a parameter for redirect, the options are:

  • follow
  • error
  • manual

The following code gives a not supported on platform exception:

new HttpClientHandler
{
    AllowAutoRedirect = false
}

After upgrading my project to .Net5 the following code seems to prevent the rediect via way of a canceled request:
(This did not work before updating)

request.SetBrowserRequestOption("redirect", "manual");

Make the request.

var response = await http.GetAsync("https://skfb.ly/WVBN");

301-cancelled

In my code I can also log the status code, but the status code is 0.
Console.WriteLine("STATUS CODE " + response.StatusCode.ToString());

What I would expect is the canceled request not to be made at all and for the response status code to be 301 with the location header value set.

As an aside I also note the HttpResponseMessage.IsSuccessStatusCode Property checks for ranges 200-299, but for me a 3xx redirect is also a success status code

Describe the solution you'd like

HttpClientHandler.AllowAutoRedirect Property should work on the Blazor WASM platform and the following code should set the redirect parameter to manual on the Fetch Api:
And the response should return with status code 301 with the location header set as it does in the F12 dev tools.

new HttpClientHandler
{
    AllowAutoRedirect = false
}

Additional context

I have read #41394 (resposible for request.SetBrowserRequestOption("redirect", "manual");?)
And I have read #39365

Author: LukeTOBrien
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@mkArtakMSFT
Copy link
Member

I believe this is also a blocker for the Octokit.Net

@LukeTOBrien
Copy link
Author

LukeTOBrien commented Nov 16, 2020

I have done some more investigation and it seems this is how redirect: 'manual' works.
Redirect manual works by cancelling the request, the Response.type is set to opaqueredirect:

The Response's status is 0, headers are empty, body is null and trailer is empty.

So it doesn't work the way I thought it would do.... It fact with the headers empty there's no way to do the redirect manually, which in my mind defeats the meaning of 'manual'.
I did find the issue whatwg/fetch#601 so there are some developers looking for the same... But it is a browser issue not a .Net issue, until/if browsers do adopt this feature.

I did also look into FetchEvents, so maybe an event will be fired when a redirect occurs?
However this is Service Workers which is over-complicating things.

So I think you can close this issue for now.
Basically redirect: 'manual' cannot be used due to opaqueredirect.

@kjpou1
Copy link
Contributor

kjpou1 commented Nov 17, 2020

@LukeTOBrien thank you for looking into this more. As you confirm the redirect from the fetch works differently than one would expect.

Here is the original PR that introduced the ability: #41394

Some more references on how the browser handles the AllowAutoRedirect and why certain information is not allowed to be returned per browser security rules.


References:

@kjpou1 kjpou1 closed this as completed Nov 17, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 17, 2020
@karelz karelz added this to the 6.0.0 milestone Jan 26, 2021
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants