From 28fe1a5d320b3289aa27b39a12dbf7ab92de4169 Mon Sep 17 00:00:00 2001 From: Geovanni Perez Date: Wed, 29 Nov 2017 10:50:03 -0600 Subject: [PATCH] Issue96 workaround web socket (#117) * Fix Issue96 * move to WebSocketsModule * Add fix reference * Update WebServer.cs * Update WebServer.cs * Update WebSocketsModule.cs * Versioning --- .../Modules/WebSocketsModule.cs | 25 ++++++++++++++----- .../Unosquare.Labs.EmbedIO.csproj | 2 +- src/Unosquare.Labs.EmbedIO/WebServer.cs | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Unosquare.Labs.EmbedIO/Modules/WebSocketsModule.cs b/src/Unosquare.Labs.EmbedIO/Modules/WebSocketsModule.cs index a9021b9f0..12161b118 100644 --- a/src/Unosquare.Labs.EmbedIO/Modules/WebSocketsModule.cs +++ b/src/Unosquare.Labs.EmbedIO/Modules/WebSocketsModule.cs @@ -11,6 +11,7 @@ using Swan; #if NET47 using System.Net.WebSockets; + using System.Text.RegularExpressions; #else using Net; #endif @@ -28,6 +29,10 @@ public class WebSocketsModule : WebModuleBase private readonly Dictionary _serverMap = new Dictionary(StringComparer.OrdinalIgnoreCase); +#if NETSTANDARD2_0 + private readonly Regex splitter = new Regex(@"(\s|[,;])+"); +#endif + /// /// Initializes a new instance of the class. /// @@ -35,6 +40,14 @@ public WebSocketsModule() { AddHandler(ModuleMap.AnyPath, HttpVerbs.Any, async (context, ct) => { +#if NETSTANDARD2_0 + // Support for Firefox https://github.com/dotnet/corefx/issues/24550#issuecomment-338048691 + var connectionValues = context.Request.Headers.GetValues("Connection"); + context.Request.Headers.Remove("Connection"); + var headers = connectionValues.Select(tk => splitter.Split(tk)).First(); + headers.ToList().ForEach(value => context.Request.Headers.Add("Connection", value)); +#endif + // check if it is a WebSocket request (this only works with Win8 and Windows 2012) if (context.Request.IsWebSocketRequest == false) return false; @@ -59,7 +72,7 @@ public WebSocketsModule() /// The name. /// public override string Name => nameof(WebSocketsModule).Humanize(); - + /// /// Registers the web sockets server given a WebSocketsServer Type. /// @@ -93,7 +106,7 @@ public void RegisterWebSocketsServer(Type socketType) nameof(socketType)); } - _serverMap[attribute.Path] = (WebSocketsServer) Activator.CreateInstance(socketType); + _serverMap[attribute.Path] = (WebSocketsServer)Activator.CreateInstance(socketType); } /// @@ -196,7 +209,7 @@ public ReadOnlyCollection WebSockets /// The name of the server. /// public abstract string ServerName { get; } - + /// /// Gets the Encoding used to use the Send method to send a string. The default is UTF8 per the WebSocket specification. /// @@ -258,7 +271,7 @@ await context.AcceptWebSocketAsync( try { #if NET47 -// define a receive buffer + // define a receive buffer var receiveBuffer = new byte[receiveBufferSize]; // define a dynamic buffer that holds multi-part receptions @@ -472,9 +485,9 @@ protected abstract void OnFrameReceived( /// /// The context. /// The local endpoint. - /// /// The remote endpoint. + /// The remote endpoint. protected abstract void OnClientConnected( - WebSocketContext context, + WebSocketContext context, System.Net.IPEndPoint localEndPoint, System.Net.IPEndPoint remoteEndPoint); #else diff --git a/src/Unosquare.Labs.EmbedIO/Unosquare.Labs.EmbedIO.csproj b/src/Unosquare.Labs.EmbedIO/Unosquare.Labs.EmbedIO.csproj index 109dda76c..962316f15 100644 --- a/src/Unosquare.Labs.EmbedIO/Unosquare.Labs.EmbedIO.csproj +++ b/src/Unosquare.Labs.EmbedIO/Unosquare.Labs.EmbedIO.csproj @@ -12,7 +12,7 @@ $(PackageTargetFallback);portable-net45+win8+wpa81+wp8 ..\..\StyleCop.Analyzers.ruleset Full - 1.11.1 + 1.11.3 EmbedIO Unosquare https://raw.githubusercontent.com/unosquare/embedio/master/LICENSE diff --git a/src/Unosquare.Labs.EmbedIO/WebServer.cs b/src/Unosquare.Labs.EmbedIO/WebServer.cs index b5c814d6d..2f846b2bc 100644 --- a/src/Unosquare.Labs.EmbedIO/WebServer.cs +++ b/src/Unosquare.Labs.EmbedIO/WebServer.cs @@ -436,4 +436,4 @@ private async Task HandleClientRequest(HttpListenerContext context, Cancellation } } } -} \ No newline at end of file +}