Skip to content

Commit

Permalink
Annotate platform-specific properties and check the platform when con…
Browse files Browse the repository at this point in the history
…figuring the handler (#2044)
  • Loading branch information
alexeyzimarev committed Apr 4, 2023
1 parent 809f071 commit d8e1e9a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
29 changes: 29 additions & 0 deletions src/RestSharp/Options/RestClientOptions.cs
Expand Up @@ -17,11 +17,16 @@
using System.Net.Http.Headers;
using System.Net.Security;
using System.Reflection;
using System.Runtime.Versioning;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using RestSharp.Authenticators;
using RestSharp.Extensions;

// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable PropertyCanBeMadeInitOnly.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global

namespace RestSharp;

[GenerateImmutable]
Expand Down Expand Up @@ -62,13 +67,19 @@ public class RestClientOptions {
/// <summary>
/// Passed to <see cref="HttpMessageHandler"/> <code>Credentials</code> property
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public ICredentials? Credentials { get; set; }

/// <summary>
/// Determine whether or not the "default credentials" (e.g. the user account under which the current process is
/// running) will be sent along to the server. The default is false.
/// Passed to <see cref="HttpMessageHandler"/> <code>UseDefaultCredentials</code> property
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public bool UseDefaultCredentials { get; set; }

/// <summary>
Expand All @@ -80,6 +91,7 @@ public class RestClientOptions {
/// Set the decompression method to use when making requests
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.All;
#else
public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip;
Expand All @@ -88,16 +100,27 @@ public class RestClientOptions {
/// <summary>
/// Set the maximum number of redirects to follow
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public int? MaxRedirects { get; set; }

/// <summary>
/// X509CertificateCollection to be sent with request
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public X509CertificateCollection? ClientCertificates { get; set; }

/// <summary>
/// Set the proxy to use when making requests. Default is null, which will use the default system proxy if one is set.
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
#endif
public IWebProxy? Proxy { get; set; }

/// <summary>
Expand All @@ -123,12 +146,18 @@ public class RestClientOptions {
/// <summary>
/// Passed to <see cref="HttpMessageHandler"/> <see langword="PreAuthenticate"/> property
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public bool PreAuthenticate { get; set; }

/// <summary>
/// Callback function for handling the validation of remote certificates. Useful for certificate pinning and
/// overriding certificate errors in the scope of a request.
/// </summary>
#if NET
[UnsupportedOSPlatform("browser")]
#endif
public RemoteCertificateValidationCallback? RemoteCertificateValidationCallback { get; set; }

/// <summary>
Expand Down
48 changes: 30 additions & 18 deletions src/RestSharp/RestClient.cs
Expand Up @@ -226,26 +226,38 @@ public partial class RestClient : IRestClient {
if (options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = options.Expect100Continue;
}

// ReSharper disable once CognitiveComplexity
static void ConfigureHttpMessageHandler(HttpClientHandler handler, ReadOnlyRestClientOptions options) {
handler.UseCookies = false;
handler.Credentials = options.Credentials;
handler.UseDefaultCredentials = options.UseDefaultCredentials;
handler.AutomaticDecompression = options.AutomaticDecompression;
handler.PreAuthenticate = options.PreAuthenticate;
handler.AllowAutoRedirect = options.FollowRedirects;

if (handler.SupportsProxy) handler.Proxy = options.Proxy;

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
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;
}
#if NET
}

if (options.MaxRedirects.HasValue) handler.MaxAutomaticRedirections = options.MaxRedirects.Value;
#endif
handler.AllowAutoRedirect = options.FollowRedirects;

#if NET
if (!OperatingSystem.IsBrowser() && !OperatingSystem.IsIOS() && !OperatingSystem.IsTvOS()) {
#endif
if (handler.SupportsProxy) handler.Proxy = options.Proxy;
#if NET
}
#endif
}

[MemberNotNull(nameof(Serializers))]
Expand Down

0 comments on commit d8e1e9a

Please sign in to comment.