diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index a4adab1395..8da1ee6330 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -317,4 +317,4 @@ fbae122a63ebbf1c7176e36fb6d3b26d681be8ab - + \ No newline at end of file diff --git a/eng/Versions.props b/eng/Versions.props index ceefa035c5..7e9c319a3a 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -249,4 +249,4 @@ https://dotnetcli.blob.core.windows.net/dotnet/ https://dotnetclimsrc.blob.core.windows.net/dotnet/ - + \ No newline at end of file diff --git a/eng/common/tools.sh b/eng/common/tools.sh index caae1dbdb2..5e4429c64f 100755 --- a/eng/common/tools.sh +++ b/eng/common/tools.sh @@ -201,7 +201,14 @@ function InstallDotNet { local runtimeSourceFeedKey='' if [[ -n "${7:-}" ]]; then - decodedFeedKey=`echo $7 | base64 --decode` + # The 'base64' binary on alpine uses '-d' and doesn't support '--decode' + # '-d'. To work around this, do a simple detection and switch the parameter + # accordingly. + decodeArg="--decode" + if base64 --help 2>&1 | grep -q "BusyBox"; then + decodeArg="-d" + fi + decodedFeedKey=`echo $7 | base64 $decodeArg` runtimeSourceFeedKey="--feed-credential $decodedFeedKey" fi diff --git a/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs b/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs index 341c7c6fa4..08e3832d7e 100644 --- a/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs +++ b/src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs @@ -374,7 +374,6 @@ namespace Microsoft.AspNetCore.Hosting.Tests Assert.Contains(Activity.Current.Baggage, pair => pair.Key == "Key1" && pair.Value == "value1/1"); } - [Fact] public void ActivityTraceParentAndTraceStateFromHeaders() { diff --git a/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.Log.cs b/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.Log.cs index 0836ea8708..74b3de2e71 100644 --- a/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.Log.cs +++ b/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.Log.cs @@ -66,6 +66,9 @@ namespace Microsoft.AspNetCore.Http.Connections.Client private static readonly Action _transportStarted = LoggerMessage.Define(LogLevel.Debug, new EventId(18, "TransportStarted"), "Transport '{Transport}' started."); + private static readonly Action _serverSentEventsNotSupportedByBrowser = + LoggerMessage.Define(LogLevel.Debug, new EventId(19, "ServerSentEventsNotSupportedByBrowser"), "Skipping ServerSentEvents because they are not supported by the browser."); + public static void Starting(ILogger logger) { _starting(logger, null); @@ -167,6 +170,11 @@ namespace Microsoft.AspNetCore.Http.Connections.Client { _transportStarted(logger, transportType, null); } + + public static void ServerSentEventsNotSupportedByBrowser(ILogger logger) + { + _serverSentEventsNotSupportedByBrowser(logger, null); + } } } } diff --git a/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs b/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs index 1c2daa9cdf..71fd3d0f2a 100644 --- a/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs +++ b/src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs @@ -37,6 +37,7 @@ namespace Microsoft.AspNetCore.Http.Connections.Client private bool _started; private bool _disposed; private bool _hasInherentKeepAlive; + private bool _isRunningInBrowser; private readonly HttpClient _httpClient; private readonly HttpConnectionOptions _httpConnectionOptions; @@ -150,6 +151,14 @@ namespace Microsoft.AspNetCore.Http.Connections.Client _httpClient = CreateHttpClient(); } + _isRunningInBrowser = Utils.IsRunningInBrowser(); + + + if (httpConnectionOptions.Transports == HttpTransportType.ServerSentEvents && _isRunningInBrowser) + { + throw new ArgumentException("ServerSentEvents can not be the only transport specified when running in the browser.", nameof(httpConnectionOptions)); + } + _transportFactory = new DefaultTransportFactory(httpConnectionOptions.Transports, _loggerFactory, _httpClient, httpConnectionOptions, GetAccessTokenAsync); _logScope = new ConnectionLogScope(); @@ -365,6 +374,13 @@ namespace Microsoft.AspNetCore.Http.Connections.Client continue; } + if (transportType == HttpTransportType.ServerSentEvents && _isRunningInBrowser) + { + Log.ServerSentEventsNotSupportedByBrowser(_logger); + transportExceptions.Add(new TransportFailedException("ServerSentEvents", "The transport is not supported in the browser.")); + continue; + } + try { if ((transportType & _httpConnectionOptions.Transports) == 0) diff --git a/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/Utils.cs b/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/Utils.cs index f92f89a3a0..8071a054a3 100644 --- a/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/Utils.cs +++ b/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/Utils.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Runtime.InteropServices; namespace Microsoft.AspNetCore.Http.Connections.Client.Internal { @@ -41,5 +42,10 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal builder.Query = newQueryString; return builder.Uri; } + + internal static bool IsRunningInBrowser() + { + return RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")); + } } } diff --git a/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs b/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs index e41dc35022..f3cda1c3ad 100644 --- a/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs +++ b/src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO.Pipelines; using System.Net.WebSockets; using System.Runtime.InteropServices; +using System.Text.Encodings.Web; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Connections; @@ -23,6 +24,7 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal private readonly ILogger _logger; private readonly TimeSpan _closeTimeout; private volatile bool _aborted; + private bool _isRunningInBrowser; private IDuplexPipe _transport; @@ -93,6 +95,8 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal // Ignore the HttpConnectionOptions access token provider. We were given an updated delegate from the HttpConnection. _accessTokenProvider = accessTokenProvider; + + _isRunningInBrowser = Utils.IsRunningInBrowser(); } public async Task StartAsync(Uri url, TransferFormat transferFormat, CancellationToken cancellationToken = default) @@ -119,7 +123,17 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal var accessToken = await _accessTokenProvider(); if (!string.IsNullOrEmpty(accessToken)) { - _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {accessToken}"); + // We can't use request headers in the browser, so instead append the token as a query string in that case + if (_isRunningInBrowser) + { + var accessTokenEncoded = UrlEncoder.Default.Encode(accessToken); + accessTokenEncoded = "access_token=" + accessTokenEncoded; + resolvedUrl = Utils.AppendQueryString(resolvedUrl, accessTokenEncoded); + } + else + { + _webSocket.Options.SetRequestHeader("Authorization", $"Bearer {accessToken}"); + } } }