|
8 | 8 | using System.Linq;
|
9 | 9 | using System.Net.Http;
|
10 | 10 | using System.Net.WebSockets;
|
| 11 | +using System.Runtime.InteropServices; |
11 | 12 | using System.Threading;
|
12 | 13 | using System.Threading.Tasks;
|
13 | 14 | using Microsoft.AspNetCore.Connections;
|
@@ -38,7 +39,6 @@ public partial class HttpConnection : ConnectionContext, IConnectionInherentKeep
|
38 | 39 | private bool _started;
|
39 | 40 | private bool _disposed;
|
40 | 41 | private bool _hasInherentKeepAlive;
|
41 |
| - private bool _isRunningInBrowser; |
42 | 42 |
|
43 | 43 | private readonly HttpClient _httpClient;
|
44 | 44 | private readonly HttpConnectionOptions _httpConnectionOptions;
|
@@ -152,9 +152,7 @@ public HttpConnection(HttpConnectionOptions httpConnectionOptions, ILoggerFactor
|
152 | 152 | _httpClient = CreateHttpClient();
|
153 | 153 | }
|
154 | 154 |
|
155 |
| - _isRunningInBrowser = Utils.IsRunningInBrowser(); |
156 |
| - |
157 |
| - if (httpConnectionOptions.Transports == HttpTransportType.ServerSentEvents && _isRunningInBrowser) |
| 155 | + if (httpConnectionOptions.Transports == HttpTransportType.ServerSentEvents && OperatingSystem.IsBrowser()) |
158 | 156 | {
|
159 | 157 | throw new ArgumentException("ServerSentEvents can not be the only transport specified when running in the browser.", nameof(httpConnectionOptions));
|
160 | 158 | }
|
@@ -376,7 +374,7 @@ private async Task SelectAndStartTransport(TransferFormat transferFormat, Cancel
|
376 | 374 | continue;
|
377 | 375 | }
|
378 | 376 |
|
379 |
| - if (transportType == HttpTransportType.ServerSentEvents && _isRunningInBrowser) |
| 377 | + if (transportType == HttpTransportType.ServerSentEvents && OperatingSystem.IsBrowser()) |
380 | 378 | {
|
381 | 379 | Log.ServerSentEventsNotSupportedByBrowser(_logger);
|
382 | 380 | transportExceptions.Add(new TransportFailedException("ServerSentEvents", "The transport is not supported in the browser."));
|
@@ -529,42 +527,49 @@ private HttpClient CreateHttpClient()
|
529 | 527 | var httpClientHandler = new HttpClientHandler();
|
530 | 528 | HttpMessageHandler httpMessageHandler = httpClientHandler;
|
531 | 529 |
|
| 530 | + var isBrowser = OperatingSystem.IsBrowser(); |
| 531 | + |
532 | 532 | if (_httpConnectionOptions != null)
|
533 | 533 | {
|
534 |
| - if (_httpConnectionOptions.Proxy != null) |
| 534 | + if (!isBrowser) |
535 | 535 | {
|
536 |
| - httpClientHandler.Proxy = _httpConnectionOptions.Proxy; |
537 |
| - } |
| 536 | + // Configure options that do not work in the browser inside this if-block |
| 537 | + if (_httpConnectionOptions.Proxy != null) |
| 538 | + { |
| 539 | + httpClientHandler.Proxy = _httpConnectionOptions.Proxy; |
| 540 | + } |
538 | 541 |
|
539 |
| - try |
540 |
| - { |
541 |
| - // On supported platforms, we need to pass the cookie container to the http client |
542 |
| - // so that we can capture any cookies from the negotiate response and give them to WebSockets. |
543 |
| - httpClientHandler.CookieContainer = _httpConnectionOptions.Cookies; |
544 |
| - } |
545 |
| - // Some variants of Mono do not support client certs or cookies and will throw NotImplementedException or NotSupportedException |
546 |
| - // Also WASM doesn't support some settings in the browser |
547 |
| - catch (Exception ex) when (ex is NotSupportedException || ex is NotImplementedException) |
548 |
| - { |
549 |
| - Log.CookiesNotSupported(_logger); |
550 |
| - } |
| 542 | + try |
| 543 | + { |
| 544 | + // On supported platforms, we need to pass the cookie container to the http client |
| 545 | + // so that we can capture any cookies from the negotiate response and give them to WebSockets. |
| 546 | + httpClientHandler.CookieContainer = _httpConnectionOptions.Cookies; |
| 547 | + } |
| 548 | + catch (Exception ex) when (ex is NotSupportedException || ex is NotImplementedException) |
| 549 | + { |
| 550 | + // Some variants of Mono do not support client certs or cookies and will throw NotImplementedException or NotSupportedException |
| 551 | + Log.CookiesNotSupported(_logger); |
| 552 | + } |
551 | 553 |
|
552 |
| - // Only access HttpClientHandler.ClientCertificates |
553 |
| - // if the user has configured those options |
554 |
| - // https://github.com/aspnet/SignalR/issues/2232 |
555 |
| - var clientCertificates = _httpConnectionOptions.ClientCertificates; |
556 |
| - if (clientCertificates?.Count > 0) |
557 |
| - { |
558 |
| - httpClientHandler.ClientCertificates.AddRange(clientCertificates); |
559 |
| - } |
| 554 | + // Only access HttpClientHandler.ClientCertificates |
| 555 | + // if the user has configured those options |
| 556 | + // https://github.com/aspnet/SignalR/issues/2232 |
560 | 557 |
|
561 |
| - if (_httpConnectionOptions.UseDefaultCredentials != null) |
562 |
| - { |
563 |
| - httpClientHandler.UseDefaultCredentials = _httpConnectionOptions.UseDefaultCredentials.Value; |
564 |
| - } |
565 |
| - if (_httpConnectionOptions.Credentials != null) |
566 |
| - { |
567 |
| - httpClientHandler.Credentials = _httpConnectionOptions.Credentials; |
| 558 | + var clientCertificates = _httpConnectionOptions.ClientCertificates; |
| 559 | + if (clientCertificates?.Count > 0) |
| 560 | + { |
| 561 | + httpClientHandler.ClientCertificates.AddRange(clientCertificates); |
| 562 | + } |
| 563 | + |
| 564 | + if (_httpConnectionOptions.UseDefaultCredentials != null) |
| 565 | + { |
| 566 | + httpClientHandler.UseDefaultCredentials = _httpConnectionOptions.UseDefaultCredentials.Value; |
| 567 | + } |
| 568 | + |
| 569 | + if (_httpConnectionOptions.Credentials != null) |
| 570 | + { |
| 571 | + httpClientHandler.Credentials = _httpConnectionOptions.Credentials; |
| 572 | + } |
568 | 573 | }
|
569 | 574 |
|
570 | 575 | httpMessageHandler = httpClientHandler;
|
@@ -645,7 +650,7 @@ private void CheckDisposed()
|
645 | 650 |
|
646 | 651 | private static bool IsWebSocketsSupported()
|
647 | 652 | {
|
648 |
| -#if NETSTANDARD2_1 |
| 653 | +#if NETSTANDARD2_1 || NETCOREAPP |
649 | 654 | // .NET Core 2.1 and above has a managed implementation
|
650 | 655 | return true;
|
651 | 656 | #else
|
|
0 commit comments