Fix manual downloads from secure servers

This commit is contained in:
UrbanCMC 2023-08-09 21:05:20 +02:00
parent ae7402be58
commit 6399ef07d5
3 changed files with 21 additions and 19 deletions

View File

@ -83,6 +83,11 @@ public abstract class BrowserWindowViewModel : ViewModel
public async Task<Cookie[]> GetCookies(string domainEnding, CancellationToken token)
{
// Strip www. before searching for cookies on a domain to handle websites saving their cookies like .example.org
if (domainEnding.StartsWith("www."))
{
domainEnding = domainEnding[4..];
}
var cookies = (await _browser.CoreWebView2.CookieManager.GetCookiesAsync(""))
.Where(c => c.Domain.EndsWith(domainEnding));
return cookies.Select(c => new Cookie
@ -146,10 +151,14 @@ public abstract class BrowserWindowViewModel : ViewModel
}
var cookies = await GetCookies(uri.Host, token);
return new ManualDownload.BrowserDownloadState(uri, cookies, new[]
return new ManualDownload.BrowserDownloadState(
uri,
cookies,
new[]
{
("Referer", referer.ToString())
});
("Referer", referer?.ToString() ?? uri.ToString())
},
_browser.CoreWebView2.Settings.UserAgent);
}
public async Task<Hash> WaitForDownload(AbsolutePath path, CancellationToken token)

View File

@ -29,17 +29,10 @@ public static class HttpExtensions
return msg;
}
public static HttpRequestMessage AddChromeAgent(this HttpRequestMessage msg)
{
msg.Headers.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
return msg;
}
public static HttpRequestMessage ToHttpRequestMessage(this ManualDownload.BrowserDownloadState browserState)
{
var msg = new HttpRequestMessage(HttpMethod.Get, browserState.Uri);
msg.AddChromeAgent();
msg.Headers.Add("User-Agent", browserState.UserAgent);
msg.AddCookies(browserState.Cookies);
msg.AddHeaders(browserState.Headers);
return msg;

View File

@ -16,7 +16,7 @@ public class ManualDownload : AUserIntervention<ManualDownload.BrowserDownloadSt
Archive = archive;
}
public record BrowserDownloadState(Uri Uri, Cookie[] Cookies, (string Key, string Value)[] Headers)
public record BrowserDownloadState(Uri Uri, Cookie[] Cookies, (string Key, string Value)[] Headers, string UserAgent)
{
}