Skip to content

Commit

Permalink
Add more custom headers
Browse files Browse the repository at this point in the history
GetStreamAsync() internal request object use configured http version
  • Loading branch information
maxisoft committed Jun 15, 2024
1 parent 2c319ca commit 1db9d01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 10 additions & 2 deletions ASFFreeGames/HttpClientSimple/SimpleHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -31,18 +32,25 @@ public SimpleHttpClient(IWebProxy? proxy = null, long timeout = 25_000) {
}

HttpClient = new HttpClient(HttpClientHandler, false) { Timeout = TimeSpan.FromMilliseconds(timeout) };
HttpClient.DefaultRequestVersion = HttpVersion.Version30;
HttpClient.DefaultRequestHeaders.ExpectContinue = false;

HttpClient.DefaultRequestHeaders.Add("User-Agent", "Lynx/2.8.8dev.9 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.12.14");
HttpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
HttpClient.DefaultRequestHeaders.Add("DNT", "1");
HttpClient.DefaultRequestHeaders.Add("Sec-GPC", "1");

HttpClient.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US"));
HttpClient.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en", 0.8));
}

public async Task<HttpStreamResponse> GetStreamAsync(Uri uri, IEnumerable<KeyValuePair<string, string>>? additionalHeaders = null, CancellationToken cancellationToken = default) {
using HttpRequestMessage request = new(HttpMethod.Get, uri);
request.Version = HttpClient.DefaultRequestVersion;

// Add additional headers if provided
if (additionalHeaders != null) {
foreach (KeyValuePair<string, string> header in additionalHeaders) {
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
request.Headers.Add(header.Key, header.Value);
}
}

Expand Down
8 changes: 5 additions & 3 deletions ASFFreeGames/Reddit/RedditHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ private static async ValueTask<JsonNode> GetPayload(SimpleHttpClient httpClient,
{ "Accept", "application/json" },
{ "Sec-Fetch-Site", "none" },
{ "Sec-Fetch-Mode", "no-cors" },
{ "Sec-Fetch-Dest", "empty" }
{ "Sec-Fetch-Dest", "empty" },
{ "x-sec-fetch-dest", "empty" },
{ "x-sec-fetch-mode", "no-cors" },
{ "x-sec-fetch-site", "none" },
};

for (int t = 0; t < retry; t++) {
Expand Down Expand Up @@ -233,7 +236,7 @@ private static async ValueTask<JsonNode> GetPayload(SimpleHttpClient httpClient,
/// <param name="maxTimeToWait"></param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>True if the request was handled & awaited, false otherwise.</returns>
private static async ValueTask<bool> HandleTooManyRequest(HttpStreamResponse response, int maxTimeToWait = 60, CancellationToken cancellationToken = default) {
private static async ValueTask<bool> HandleTooManyRequest(HttpStreamResponse response, int maxTimeToWait = 45, CancellationToken cancellationToken = default) {
if (response.StatusCode is HttpStatusCode.Forbidden or HttpStatusCode.TooManyRequests) {
if (response.Response.Headers.TryGetValues("x-ratelimit-remaining", out IEnumerable<string>? rateLimitRemaining)) {
if (int.TryParse(rateLimitRemaining.FirstOrDefault(), out int remaining) && (remaining <= 0)) {
Expand Down Expand Up @@ -269,7 +272,6 @@ private static async ValueTask<bool> HandleTooManyRequest(HttpStreamResponse res
/// <returns>The parsed JSON object, or null if parsing fails.</returns>
private static async Task<JsonNode?> ParseJsonNode(HttpStreamResponse stream, CancellationToken cancellationToken) {
string data = await stream.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericDebug($"Response: {data}");

return JsonNode.Parse(data);
}
Expand Down

0 comments on commit 1db9d01

Please sign in to comment.