Skip to content

Commit

Permalink
Fix the http client timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed May 25, 2024
1 parent 63daee1 commit dcb11c0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1,647 deletions.
19 changes: 0 additions & 19 deletions SECURITY.md

This file was deleted.

9 changes: 0 additions & 9 deletions package.json

This file was deleted.

51 changes: 27 additions & 24 deletions src/RestSharp/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using System.Runtime.CompilerServices;
using RestSharp.Serializers;

// ReSharper disable InvertIf

// ReSharper disable VirtualMemberCallInConstructor
#pragma warning disable 618

Expand Down Expand Up @@ -72,7 +74,7 @@ public RestClient(
}

ConfigureSerializers(configureSerialization);
Options = new ReadOnlyRestClientOptions(options);
Options = new ReadOnlyRestClientOptions(options);
DefaultParameters = new DefaultParameters(Options);

if (useClientFactory) {
Expand All @@ -90,8 +92,12 @@ HttpClient GetClient() {
var handler = new HttpClientHandler();
ConfigureHttpMessageHandler(handler, options);
var finalHandler = options.ConfigureMessageHandler?.Invoke(handler) ?? handler;
var httpClient = new HttpClient(finalHandler);
var httpClient = new HttpClient(finalHandler);
ConfigureHttpClient(httpClient, options);

// We will use Options.Timeout in ExecuteAsInternalAsync method
httpClient.Timeout = Timeout.InfiniteTimeSpan;

ConfigureDefaultParameters(options);
configureDefaultHeaders?.Invoke(httpClient.DefaultRequestHeaders);
return httpClient;
Expand Down Expand Up @@ -220,9 +226,6 @@ public RestClient(
: this(new HttpClient(handler, disposeHandler), true, configureRestClient, configureSerialization) { }

static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options) {
// We will use Options.Timeout in ExecuteAsInternalAsync method
httpClient.Timeout = Timeout.InfiniteTimeSpan;

if (options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = options.Expect100Continue;
}

Expand All @@ -231,21 +234,21 @@ static void ConfigureHttpMessageHandler(HttpClientHandler handler, RestClientOpt
#if NET
if (!OperatingSystem.IsBrowser()) {
#endif
handler.UseCookies = false;
handler.Credentials = options.Credentials;
handler.UseDefaultCredentials = options.UseDefaultCredentials;
handler.AutomaticDecompression = options.AutomaticDecompression;
handler.PreAuthenticate = options.PreAuthenticate;
if (options.MaxRedirects.HasValue) handler.MaxAutomaticRedirections = options.MaxRedirects.Value;

if (options.RemoteCertificateValidationCallback != null)
handler.ServerCertificateCustomValidationCallback =
(request, cert, chain, errors) => options.RemoteCertificateValidationCallback(request, cert, chain, errors);

if (options.ClientCertificates != null) {
handler.ClientCertificates.AddRange(options.ClientCertificates);
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
}
handler.UseCookies = false;
handler.Credentials = options.Credentials;
handler.UseDefaultCredentials = options.UseDefaultCredentials;
handler.AutomaticDecompression = options.AutomaticDecompression;
handler.PreAuthenticate = options.PreAuthenticate;
if (options.MaxRedirects.HasValue) handler.MaxAutomaticRedirections = options.MaxRedirects.Value;

if (options.RemoteCertificateValidationCallback != null)
handler.ServerCertificateCustomValidationCallback =
(request, cert, chain, errors) => options.RemoteCertificateValidationCallback(request, cert, chain, errors);

if (options.ClientCertificates != null) {
handler.ClientCertificates.AddRange(options.ClientCertificates);
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
}
#if NET
}
#endif
Expand All @@ -255,7 +258,7 @@ static void ConfigureHttpMessageHandler(HttpClientHandler handler, RestClientOpt
// ReSharper disable once InvertIf
if (!OperatingSystem.IsBrowser() && !OperatingSystem.IsIOS() && !OperatingSystem.IsTvOS()) {
#endif
if (handler.SupportsProxy) handler.Proxy = options.Proxy;
if (handler.SupportsProxy) handler.Proxy = options.Proxy;
#if NET
}
#endif
Expand All @@ -274,8 +277,8 @@ void ConfigureSerializers(ConfigureSerialization? configureSerialization) {
void ConfigureDefaultParameters(RestClientOptions options) {
if (options.UserAgent == null) return;

if (!options.AllowMultipleDefaultParametersWithSameName
&& DefaultParameters.Any(parameter => parameter.Type == ParameterType.HttpHeader && parameter.Name == KnownHeaders.UserAgent))
if (!options.AllowMultipleDefaultParametersWithSameName &&
DefaultParameters.Any(parameter => parameter.Type == ParameterType.HttpHeader && parameter.Name == KnownHeaders.UserAgent))
DefaultParameters.RemoveParameter(KnownHeaders.UserAgent, ParameterType.HttpHeader);
DefaultParameters.AddParameter(Parameter.CreateParameter(KnownHeaders.UserAgent, options.UserAgent, ParameterType.HttpHeader));
}
Expand All @@ -294,4 +297,4 @@ public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
Loading

0 comments on commit dcb11c0

Please sign in to comment.