Skip to content

Commit d8e1e9a

Browse files
Annotate platform-specific properties and check the platform when configuring the handler (#2044)
1 parent 809f071 commit d8e1e9a

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

src/RestSharp/Options/RestClientOptions.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
using System.Net.Http.Headers;
1818
using System.Net.Security;
1919
using System.Reflection;
20+
using System.Runtime.Versioning;
2021
using System.Security.Cryptography.X509Certificates;
2122
using System.Text;
2223
using RestSharp.Authenticators;
2324
using RestSharp.Extensions;
2425

26+
// ReSharper disable UnusedAutoPropertyAccessor.Global
27+
// ReSharper disable PropertyCanBeMadeInitOnly.Global
28+
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
29+
2530
namespace RestSharp;
2631

2732
[GenerateImmutable]
@@ -62,13 +67,19 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
6267
/// <summary>
6368
/// Passed to <see cref="HttpMessageHandler"/> <code>Credentials</code> property
6469
/// </summary>
70+
#if NET
71+
[UnsupportedOSPlatform("browser")]
72+
#endif
6573
public ICredentials? Credentials { get; set; }
6674

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

7485
/// <summary>
@@ -80,6 +91,7 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
8091
/// Set the decompression method to use when making requests
8192
/// </summary>
8293
#if NET
94+
[UnsupportedOSPlatform("browser")]
8395
public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.All;
8496
#else
8597
public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip;
@@ -88,16 +100,27 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
88100
/// <summary>
89101
/// Set the maximum number of redirects to follow
90102
/// </summary>
103+
#if NET
104+
[UnsupportedOSPlatform("browser")]
105+
#endif
91106
public int? MaxRedirects { get; set; }
92107

93108
/// <summary>
94109
/// X509CertificateCollection to be sent with request
95110
/// </summary>
111+
#if NET
112+
[UnsupportedOSPlatform("browser")]
113+
#endif
96114
public X509CertificateCollection? ClientCertificates { get; set; }
97115

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

103126
/// <summary>
@@ -123,12 +146,18 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
123146
/// <summary>
124147
/// Passed to <see cref="HttpMessageHandler"/> <see langword="PreAuthenticate"/> property
125148
/// </summary>
149+
#if NET
150+
[UnsupportedOSPlatform("browser")]
151+
#endif
126152
public bool PreAuthenticate { get; set; }
127153

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

134163
/// <summary>

src/RestSharp/RestClient.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,26 +226,38 @@ static void ConfigureHttpClient(HttpClient httpClient, RestClientOptions options
226226
if (options.Expect100Continue != null) httpClient.DefaultRequestHeaders.ExpectContinue = options.Expect100Continue;
227227
}
228228

229+
// ReSharper disable once CognitiveComplexity
229230
static void ConfigureHttpMessageHandler(HttpClientHandler handler, ReadOnlyRestClientOptions options) {
230-
handler.UseCookies = false;
231-
handler.Credentials = options.Credentials;
232-
handler.UseDefaultCredentials = options.UseDefaultCredentials;
233-
handler.AutomaticDecompression = options.AutomaticDecompression;
234-
handler.PreAuthenticate = options.PreAuthenticate;
235-
handler.AllowAutoRedirect = options.FollowRedirects;
236-
237-
if (handler.SupportsProxy) handler.Proxy = options.Proxy;
238-
239-
if (options.RemoteCertificateValidationCallback != null)
240-
handler.ServerCertificateCustomValidationCallback =
241-
(request, cert, chain, errors) => options.RemoteCertificateValidationCallback(request, cert, chain, errors);
242-
243-
if (options.ClientCertificates != null) {
244-
handler.ClientCertificates.AddRange(options.ClientCertificates);
245-
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
231+
#if NET
232+
if (!OperatingSystem.IsBrowser()) {
233+
#endif
234+
handler.UseCookies = false;
235+
handler.Credentials = options.Credentials;
236+
handler.UseDefaultCredentials = options.UseDefaultCredentials;
237+
handler.AutomaticDecompression = options.AutomaticDecompression;
238+
handler.PreAuthenticate = options.PreAuthenticate;
239+
if (options.MaxRedirects.HasValue) handler.MaxAutomaticRedirections = options.MaxRedirects.Value;
240+
241+
if (options.RemoteCertificateValidationCallback != null)
242+
handler.ServerCertificateCustomValidationCallback =
243+
(request, cert, chain, errors) => options.RemoteCertificateValidationCallback(request, cert, chain, errors);
244+
245+
if (options.ClientCertificates != null) {
246+
handler.ClientCertificates.AddRange(options.ClientCertificates);
247+
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
248+
}
249+
#if NET
246250
}
247-
248-
if (options.MaxRedirects.HasValue) handler.MaxAutomaticRedirections = options.MaxRedirects.Value;
251+
#endif
252+
handler.AllowAutoRedirect = options.FollowRedirects;
253+
254+
#if NET
255+
if (!OperatingSystem.IsBrowser() && !OperatingSystem.IsIOS() && !OperatingSystem.IsTvOS()) {
256+
#endif
257+
if (handler.SupportsProxy) handler.Proxy = options.Proxy;
258+
#if NET
259+
}
260+
#endif
249261
}
250262

251263
[MemberNotNull(nameof(Serializers))]

0 commit comments

Comments
 (0)