Skip to content

Commit

Permalink
fix: some header fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 16, 2024
1 parent 508bf52 commit a6ea5dc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
21 changes: 16 additions & 5 deletions DisCatSharp/Clients/BaseDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -230,13 +231,23 @@ protected BaseDiscordClient(DiscordConfiguration config)
this.InternalVoiceRegions = new();
this.VoiceRegionsLazy = new(() => new ReadOnlyDictionary<string, DiscordVoiceRegion>(this.InternalVoiceRegions));

this.RestClient = new();
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Utilities.GetUserAgent());
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-locale", this.Configuration.Locale);
var httphandler = new HttpClientHandler
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
UseProxy = this.Configuration.Proxy != null,
Proxy = this.Configuration.Proxy
};
this.RestClient = new()
{
Timeout = this.Configuration.HttpTimeout
};
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this.Configuration.Locale);
if (!string.IsNullOrWhiteSpace(this.Configuration.Timezone))
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-timezone", this.Configuration.Timezone);
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this.Configuration.Timezone);
if (this.Configuration.Override is not null)
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation("x-super-properties", this.Configuration.Override);
this.RestClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this.Configuration.Override);

var a = typeof(DiscordClient).GetTypeInfo().Assembly;

Expand Down
2 changes: 1 addition & 1 deletion DisCatSharp/Clients/DiscordOAuth2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public DiscordOAuth2Client(
}

this.VersionHeader = $"DiscordBot (https://github.com/Aiko-IT-Systems/DisCatSharp, v{vs})";
this.ApiClient.Rest.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", this.VersionHeader);
this.ApiClient.Rest.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, this.VersionHeader);

this.OAuth2ClientErroredInternal = new("CLIENT_ERRORED", EventExecutionLimit, this.Goof);

Expand Down
10 changes: 5 additions & 5 deletions DisCatSharp/Clients/DiscordShardedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ private async Task<GatewayInfo> GetGatewayInfoAsync()
Timeout = this._configuration.HttpTimeout
};

http.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Utilities.GetUserAgent());
http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", Utilities.GetFormattedToken(this._configuration));
http.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-locale", this._configuration.Locale);
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.AUTHORIZATION, Utilities.GetFormattedToken(this._configuration));
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this._configuration.Locale);
if (!string.IsNullOrWhiteSpace(this._configuration.Timezone))
http.DefaultRequestHeaders.TryAddWithoutValidation("x-discord-timezone", this._configuration.Timezone);
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this._configuration.Timezone);
if (this._configuration.Override != null)
http.DefaultRequestHeaders.TryAddWithoutValidation("x-super-properties", this._configuration.Override);
http.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this._configuration.Override);

this.Logger.LogDebug(LoggerEvents.ShardRest, $"Obtaining gateway information from GET {Endpoints.GATEWAY}{Endpoints.BOT}...");
var resp = await http.GetAsync(url).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion DisCatSharp/Net/Rest/CommonHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static class CommonHeaders
/// <summary>
/// Gets or sets the user agent header.
/// </summary>
public const string USER_AGENT = "UserAgent";
public const string USER_AGENT = "User-Agent";

/// <summary>
/// Gets or sets the authorization header.
Expand Down
18 changes: 15 additions & 3 deletions DisCatSharp/Net/Rest/DiscordApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ internal DiscordApiClient(DiscordOAuth2Client client, IWebProxy proxy, TimeSpan
{
this.OAuth2Client = client;
this.Discord = null!;
this.Rest = new(proxy, timeout, useRelativeRateLimit, logger);
this.Rest = new(new DiscordConfiguration() {
Proxy = proxy,
HttpTimeout = timeout,
UseRelativeRatelimit = useRelativeRateLimit,
ApiChannel = ApiChannel.Stable,
ApiVersion = "10"
}, logger);
}

/// <summary>
Expand All @@ -70,7 +76,13 @@ internal DiscordApiClient(IWebProxy proxy, TimeSpan timeout, bool useRelativeRat
{
this.Discord = null!;
this.OAuth2Client = null!;
this.Rest = new(proxy, timeout, useRelativeRateLimit, logger);
this.Rest = new(new DiscordConfiguration() {
Proxy = proxy,
HttpTimeout = timeout,
UseRelativeRatelimit = useRelativeRateLimit,
ApiChannel = ApiChannel.Stable,
ApiVersion = "10"
}, logger);
}

/// <summary>
Expand Down Expand Up @@ -7089,7 +7101,7 @@ internal async Task<DiscordInteractionCallbackResponse> CreateInteractionRespons
attachments.Add(att);
fileId++;
}

if (pld.Data is not null)
pld.Data.Attachments = attachments;
}
Expand Down
30 changes: 13 additions & 17 deletions DisCatSharp/Net/Rest/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ internal sealed class RestClient : IDisposable
/// </summary>
/// <param name="client">The client.</param>
internal RestClient(BaseDiscordClient client)
: this(client.Configuration.Proxy, client.Configuration.HttpTimeout, client.Configuration.UseRelativeRatelimit, client.Logger)
: this(client.Configuration, client.Logger)
{
this._discord = client;

Expand All @@ -109,14 +109,10 @@ internal RestClient(BaseDiscordClient client)
/// Initializes a new instance of the <see cref="RestClient" /> class.
/// This is for meta-clients, such as the <see cref="DiscordWebhookClient" /> and <see cref="DiscordOAuth2Client" />.
/// </summary>
/// <param name="proxy">The proxy.</param>
/// <param name="timeout">The timeout.</param>
/// <param name="useRelativeRatelimit">Whether to use relative ratelimit.</param>
/// <param name="configuration">The configuration.</param>
/// <param name="logger">The logger.</param>
internal RestClient(
IWebProxy? proxy,
TimeSpan timeout,
bool useRelativeRatelimit,
DiscordConfiguration configuration,
ILogger logger
)
{
Expand All @@ -126,32 +122,32 @@ ILogger logger
{
UseCookies = false,
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
UseProxy = proxy != null,
Proxy = proxy
UseProxy = configuration.Proxy != null,
Proxy = configuration.Proxy
};

this.HttpClient = new(httphandler)
{
BaseAddress = new(Utilities.GetApiBaseUri(this._discord?.Configuration)),
Timeout = timeout
BaseAddress = new(Utilities.GetApiBaseUri(configuration)),
Timeout = configuration.HttpTimeout
};

this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.USER_AGENT, Utilities.GetUserAgent());
if (this._discord is { Configuration: not null })
{
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, this._discord.Configuration.Locale);
if (!string.IsNullOrWhiteSpace(this._discord.Configuration.Timezone))
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, this._discord.Configuration.Timezone);
if (!string.IsNullOrWhiteSpace(this._discord.Configuration.Override))
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, this._discord.Configuration.Override);
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_LOCALE, configuration.Locale);
if (!string.IsNullOrWhiteSpace(configuration.Timezone))
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.DISCORD_TIMEZONE, configuration.Timezone);
if (!string.IsNullOrWhiteSpace(configuration.Override))
this.HttpClient.DefaultRequestHeaders.TryAddWithoutValidation(CommonHeaders.SUPER_PROPERTIES, configuration.Override);
}

this._routesToHashes = new();
this._hashesToBuckets = new();
this._requestQueue = new();

this._globalRateLimitEvent = new(true);
this._useResetAfter = useRelativeRatelimit;
this._useResetAfter = configuration.UseRelativeRatelimit;
}

/// <summary>
Expand Down

0 comments on commit a6ea5dc

Please sign in to comment.