wabbajack/Wabbajack.Common/HttpExtensions.cs
Luca 0c11f11839
Changes for an official Version 3.3.1.0 Release (#2439)
* added more visible error messages to avoid user confusion

added hard drive free space detection, added red error message text, removed overwrite checkbox, added wiki button link

extended the error text for starting wabbajack in protected location

removed debug code

shortened error message to fit in text box

* restored warning removed in error, updated changelog, removed debug includes

* Update InstallerVM.cs

* Update InstallerVM.cs

* Update MainWindowViewModel.cs

* added json optional flag to only show version number over modlist image in installer view, if the modlist image already contains the title

removed debug code

change to pascal case and match existing code style

update changelog

* Fix manual downloads sometimes launching in browser

* Fix manual downloads from secure servers

* Remove duplicate user agent code

* Create configuration project and performance settings

* Bind new performance settings to UI

* Use performance settings to limit maximum memory per download

* Remove unused settings and related classes

* Updated CHANGELOG.md

* update CHANGELOG.md

* moved the existing files popup to an error message , heralding the return of the overwrite install checkbox

* added newline

* reverted erroneous edit

* gogID for fallout4 added

* update CHANGELOG.md

* Fix deadlock when loading new settings

* change folder/directory check logic

* update CHANGELOG.md

* revert unnecessary change

* update CHANGELOG.md

* Bump Wabbajack to .NET 7

* Bump ReactiveUI packages & deps

* Update GameFinder to 4.0.0

* Update CHANGELOG.md

* Update CHANGELOG.md

* Don't try to add cookies if array is empty

* Start download from scratch if .download_package can't be parsed

* Log when application is shutting down

* update CHANGELOG.md

* exclude the "SP Consent Message" on nexus from the cleared iframes

* Actually use the --outputPath compile option if the user provides one

It was just not being used, defaulting to the parent of installPath. it
still does, if the user does not specify a directory using --outputPath

* update Wabbajack.Compression.BSA dependencies

* improve log message to include storage space reference

* clearing temp folder when the app closes

* update logging message

* update CHANGELOG.md

* update CHANGELOG.md

* update logging for possible exceptions thrown when clearing temp folder

* fix cloudflare captcha

---------

Co-authored-by: JanuarySnow <bobfordiscord12@gmail.com>
Co-authored-by: JanuarySnow <85711747+JanuarySnow@users.noreply.github.com>
Co-authored-by: UrbanCMC <UrbanCMC@web.de>
Co-authored-by: trawzified <55751269+tr4wzified@users.noreply.github.com>
Co-authored-by: Marco Antonio Jaguaribe Costa <marco.antonio.costa@gmail.com>
Co-authored-by: Timothy Baldridge <tbaldridge@gmail.com>
2023-11-14 20:19:53 -07:00

66 lines
2.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Wabbajack.DTOs.Interventions;
using Wabbajack.DTOs.Logins;
using Wabbajack.Networking.Http;
using Wabbajack.RateLimiter;
namespace Wabbajack.Common;
public static class HttpExtensions
{
private const string ChromeUserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36";
public static HttpRequestMessage AddCookies(this HttpRequestMessage msg, Cookie[] cookies)
{
if (cookies.Length > 0)
{
msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}")));
}
return msg;
}
public static HttpRequestMessage AddChromeAgent(this HttpRequestMessage msg, string? overrideUserAgent = null)
{
msg.Headers.UserAgent.Clear();
msg.Headers.Add("User-Agent", overrideUserAgent ?? ChromeUserAgent);
return msg;
}
public static HttpRequestMessage AddHeaders(this HttpRequestMessage msg, IEnumerable<(string Key, string Value)> headers)
{
foreach (var header in headers)
{
msg.Headers.Add(header.Key, header.Value);
}
return msg;
}
public static HttpRequestMessage ToHttpRequestMessage(this ManualDownload.BrowserDownloadState browserState)
{
var msg = new HttpRequestMessage(HttpMethod.Get, browserState.Uri);
msg.AddChromeAgent(browserState.UserAgent);
msg.AddCookies(browserState.Cookies);
msg.AddHeaders(browserState.Headers);
return msg;
}
public static async Task<TValue?> GetFromJsonAsync<TValue>(this HttpClient client, IResource<HttpClient> limiter,
HttpRequestMessage msg,
JsonSerializerOptions? options, CancellationToken cancellationToken = default)
{
using var job = await limiter.Begin($"HTTP Get JSON {msg.RequestUri}", 0, cancellationToken);
using var response = await client.SendAsync(msg, cancellationToken);
if (!response.IsSuccessStatusCode)
throw new HttpException(response);
await job.Report((int) response.Content.Headers.ContentLength!, cancellationToken);
return await response.Content.ReadFromJsonAsync<TValue>(options, cancellationToken);
}
}