From e7d442e6a2cae6d214db8e17071a5696df1f7d1f Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Mon, 3 Apr 2023 14:34:18 +0200 Subject: [PATCH] Annotate platform-specific properties and check the platform when configuring the handler --- src/RestSharp/Options/RestClientOptions.cs | 29 +++++++++++++ src/RestSharp/RestClient.cs | 48 ++++++++++++++-------- 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/RestSharp/Options/RestClientOptions.cs b/src/RestSharp/Options/RestClientOptions.cs index a07daf55d..49c197ffc 100644 --- a/src/RestSharp/Options/RestClientOptions.cs +++ b/src/RestSharp/Options/RestClientOptions.cs @@ -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] @@ -62,6 +67,9 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// /// Passed to Credentials property /// +#if NET + [UnsupportedOSPlatform("browser")] +#endif public ICredentials? Credentials { get; set; } /// @@ -69,6 +77,9 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// running) will be sent along to the server. The default is false. /// Passed to UseDefaultCredentials property /// +#if NET + [UnsupportedOSPlatform("browser")] +#endif public bool UseDefaultCredentials { get; set; } /// @@ -80,6 +91,7 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// Set the decompression method to use when making requests /// #if NET + [UnsupportedOSPlatform("browser")] public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.All; #else public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip; @@ -88,16 +100,27 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// /// Set the maximum number of redirects to follow /// +#if NET + [UnsupportedOSPlatform("browser")] +#endif public int? MaxRedirects { get; set; } /// /// X509CertificateCollection to be sent with request /// +#if NET + [UnsupportedOSPlatform("browser")] +#endif public X509CertificateCollection? ClientCertificates { get; set; } /// /// Set the proxy to use when making requests. Default is null, which will use the default system proxy if one is set. /// +#if NET + [UnsupportedOSPlatform("browser")] + [UnsupportedOSPlatform("ios")] + [UnsupportedOSPlatform("tvos")] +#endif public IWebProxy? Proxy { get; set; } /// @@ -123,12 +146,18 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba /// /// Passed to property /// +#if NET + [UnsupportedOSPlatform("browser")] +#endif public bool PreAuthenticate { get; set; } /// /// Callback function for handling the validation of remote certificates. Useful for certificate pinning and /// overriding certificate errors in the scope of a request. /// +#if NET + [UnsupportedOSPlatform("browser")] +#endif public RemoteCertificateValidationCallback? RemoteCertificateValidationCallback { get; set; } /// diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index ec1c3f375..d4ae91867 100644 --- a/src/RestSharp/RestClient.cs +++ b/src/RestSharp/RestClient.cs @@ -226,26 +226,38 @@ static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options 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))]