2022-05-19 03:21:38 +00:00
|
|
|
using System.Collections.Generic;
|
2021-09-27 12:42:46 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
2021-10-12 05:19:13 +00:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
using System.Text.Json;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2022-05-19 03:21:38 +00:00
|
|
|
using Wabbajack.DTOs.Interventions;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.DTOs.Logins;
|
2021-10-12 05:19:13 +00:00
|
|
|
using Wabbajack.Networking.Http;
|
|
|
|
using Wabbajack.RateLimiter;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Common;
|
|
|
|
|
|
|
|
public static class HttpExtensions
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2023-10-12 18:33:06 +00:00
|
|
|
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";
|
2021-10-23 16:51:17 +00:00
|
|
|
public static HttpRequestMessage AddCookies(this HttpRequestMessage msg, Cookie[] cookies)
|
2021-09-27 12:42:46 +00:00
|
|
|
{
|
2023-11-15 03:19:53 +00:00
|
|
|
if (cookies.Length > 0)
|
|
|
|
{
|
|
|
|
msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}")));
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
return msg;
|
|
|
|
}
|
2023-10-12 18:33:06 +00:00
|
|
|
|
|
|
|
public static HttpRequestMessage AddChromeAgent(this HttpRequestMessage msg, string? overrideUserAgent = null)
|
|
|
|
{
|
|
|
|
msg.Headers.UserAgent.Clear();
|
|
|
|
msg.Headers.Add("User-Agent", overrideUserAgent ?? ChromeUserAgent);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2022-05-19 03:21:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
2022-05-19 03:21:38 +00:00
|
|
|
public static HttpRequestMessage ToHttpRequestMessage(this ManualDownload.BrowserDownloadState browserState)
|
|
|
|
{
|
|
|
|
var msg = new HttpRequestMessage(HttpMethod.Get, browserState.Uri);
|
2023-10-12 18:33:06 +00:00
|
|
|
msg.AddChromeAgent(browserState.UserAgent);
|
2022-05-19 03:21:38 +00:00
|
|
|
msg.AddCookies(browserState.Cookies);
|
|
|
|
msg.AddHeaders(browserState.Headers);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
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);
|
2021-10-12 05:19:13 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
await job.Report((int) response.Content.Headers.ContentLength!, cancellationToken);
|
|
|
|
return await response.Content.ReadFromJsonAsync<TValue>(options, cancellationToken);
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|
|
|
|
}
|