Flurl icon indicating copy to clipboard operation
Flurl copied to clipboard

DownloadFileAsync => GetFileNameFromPath does not respect redirects

Open dmdymov opened this issue 1 year ago • 1 comments

I tried to download a file via Flurl and noticed that file had a gibberish name, but Chrome downloaded the same file with a readable name.

var client = new FlurlClient(baseUrl);
var request = client.Request(link)
    .WithAutoRedirect(true)
    .WithCookie(cookieKey, cookieValue);
var fileName = await request.DownloadFileAsync(".");
// nothing special

After some investigation my suspicion is that DownloadFileAsync (in case of Content-Disposition absence) does not respect redirects and tries to parse file name from the initial request url. But file name can be parsed only from redirected request url in my case and Chrome does exactly that.

I don't know if this behavior is governed by any RFC or something else, so I don't know if it is a bug or not.

Screenshot of the debug values from the breakpoint at DownloadFileAsync: Screenshot 2024-07-10 002928

Versions

<PackageReference Include="Flurl" Version="4.0.0" />
<PackageReference Include="Flurl.Http" Version="4.0.2" />

dmdymov avatar Jul 09 '24 21:07 dmdymov

Thanks for reporting. The combination of redirect + DownloadFileAsync is probably not well covered by tests so I could see this being a legitimate bug. I'll dig deeper when I have some time, but in the mean time you could probably come up with a reasonable work-around that avoids using DownloadFileAsync directly. That implementation might help as a starting point though:

https://github.com/tmenier/Flurl/blob/dev/src/Flurl.Http/DownloadExtensions.cs

tmenier avatar Jul 22 '24 18:07 tmenier