From 577b074024ad629846615affe7a27d8f713fbd49 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Wed, 18 Jun 2014 09:11:14 -0700 Subject: [PATCH] WebSockets cleanup. --- .../FeatureContext.cs | 10 +- .../RequestProcessing/RequestContext.cs | 4 +- .../Microsoft.Net.WebSockets.kproj | 2 - .../WebSocketHelpers.cs | 21 +++ .../WebSocketMiddleware.cs | 159 ------------------ src/Microsoft.Net.WebSockets/build.cmd | 3 - 6 files changed, 30 insertions(+), 169 deletions(-) delete mode 100644 src/Microsoft.Net.WebSockets/WebSocketMiddleware.cs delete mode 100644 src/Microsoft.Net.WebSockets/build.cmd diff --git a/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs b/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs index 720ed6e51c..773f35e8ec 100644 --- a/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs +++ b/src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs @@ -26,6 +26,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.HttpFeature; using Microsoft.Net.Server; +using Microsoft.Net.WebSockets; namespace Microsoft.AspNet.Server.WebListener { @@ -94,9 +95,12 @@ namespace Microsoft.AspNet.Server.WebListener _features.Add(typeof(IHttpSendFileFeature), this); _features.Add(typeof(IHttpRequestLifetimeFeature), this); - // TODO: If Win8+ - _features.Add(typeof(IHttpOpaqueUpgradeFeature), this); - _features.Add(typeof(IHttpWebSocketFeature), this); + // Win8+ + if (WebSocketHelpers.AreWebSocketsSupported) + { + _features.Add(typeof(IHttpOpaqueUpgradeFeature), this); + _features.Add(typeof(IHttpWebSocketFeature), this); + } // TODO: // _environment.CallCancelled = _cts.Token; diff --git a/src/Microsoft.Net.Server/RequestProcessing/RequestContext.cs b/src/Microsoft.Net.Server/RequestProcessing/RequestContext.cs index ad3d7fc7e8..07f0df4fab 100644 --- a/src/Microsoft.Net.Server/RequestProcessing/RequestContext.cs +++ b/src/Microsoft.Net.Server/RequestProcessing/RequestContext.cs @@ -255,14 +255,14 @@ namespace Microsoft.Net.Server { return AcceptWebSocketAsync(null, WebSocketHelpers.DefaultReceiveBufferSize, - WebSocket.DefaultKeepAliveInterval); + WebSocketHelpers.DefaultKeepAliveInterval); } public Task AcceptWebSocketAsync(string subProtocol) { return AcceptWebSocketAsync(subProtocol, WebSocketHelpers.DefaultReceiveBufferSize, - WebSocket.DefaultKeepAliveInterval); + WebSocketHelpers.DefaultKeepAliveInterval); } public Task AcceptWebSocketAsync(string subProtocol, TimeSpan keepAliveInterval) diff --git a/src/Microsoft.Net.WebSockets/Microsoft.Net.WebSockets.kproj b/src/Microsoft.Net.WebSockets/Microsoft.Net.WebSockets.kproj index 031b4e912b..0d270aef1d 100644 --- a/src/Microsoft.Net.WebSockets/Microsoft.Net.WebSockets.kproj +++ b/src/Microsoft.Net.WebSockets/Microsoft.Net.WebSockets.kproj @@ -18,7 +18,6 @@ 2.0 - @@ -43,7 +42,6 @@ - diff --git a/src/Microsoft.Net.WebSockets/WebSocketHelpers.cs b/src/Microsoft.Net.WebSockets/WebSocketHelpers.cs index 7d61cff7a1..026f295077 100644 --- a/src/Microsoft.Net.WebSockets/WebSocketHelpers.cs +++ b/src/Microsoft.Net.WebSockets/WebSocketHelpers.cs @@ -61,6 +61,7 @@ namespace Microsoft.Net.WebSockets internal static readonly ArraySegment EmptyPayload = new ArraySegment(new byte[] { }, 0, 0); private static readonly Random KeyGenerator = new Random(); + private static TimeSpan? _defaultKeepAliveInterval; public static bool AreWebSocketsSupported { @@ -70,6 +71,26 @@ namespace Microsoft.Net.WebSockets } } + public static TimeSpan DefaultKeepAliveInterval + { + get + { + if (!_defaultKeepAliveInterval.HasValue) + { + if (AreWebSocketsSupported) + { + _defaultKeepAliveInterval = new TimeSpan?(UnsafeNativeMethods.WebSocketProtocolComponent.WebSocketGetDefaultKeepAliveInterval()); + } + else + { + _defaultKeepAliveInterval = new TimeSpan?(Timeout.InfiniteTimeSpan); + } + } + return _defaultKeepAliveInterval.Value; + } + } + + public static bool IsValidWebSocketKey(string key) { if (string.IsNullOrWhiteSpace(key)) diff --git a/src/Microsoft.Net.WebSockets/WebSocketMiddleware.cs b/src/Microsoft.Net.WebSockets/WebSocketMiddleware.cs deleted file mode 100644 index bac7e7cf32..0000000000 --- a/src/Microsoft.Net.WebSockets/WebSocketMiddleware.cs +++ /dev/null @@ -1,159 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. -// All Rights Reserved -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING -// WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF -// TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR -// NON-INFRINGEMENT. -// See the Apache 2 License for the specific language governing -// permissions and limitations under the License. -/* TODO: -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net.WebSockets; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Http; -using Microsoft.AspNet.HttpFeature; -using Microsoft.Net.WebSockets; - -namespace Microsoft.AspNet.WebSockets -{ - public class WebSocketMiddleware - { - private RequestDelegate _next; - - public WebSocketMiddleware(RequestDelegate next) - { - _next = next; - } - - public Task Invoke(HttpContext context) - { - // Detect if an opaque upgrade is available, and if websocket upgrade headers are present. - // If so, add a websocket upgrade. - var upgradeFeature = context.GetFeature(); - if (upgradeFeature != null) - { - context.SetFeature(new UpgradeHandshake(context, upgradeFeature)); - } - - return _next(context); - } - - private class UpgradeHandshake : IHttpWebSocketFeature - { - private HttpContext _context; - private IHttpOpaqueUpgradeFeature _upgrade; - - private string _subProtocol; - private int _receiveBufferSize = WebSocketHelpers.DefaultReceiveBufferSize; - private TimeSpan _keepAliveInterval = WebSocket.DefaultKeepAliveInterval; - private ArraySegment? _internalBuffer; - - internal UpgradeHandshake(HttpContext context, IHttpOpaqueUpgradeFeature upgrade) - { - _context = context; - _upgrade = upgrade; - } - - public bool IsWebSocketRequest - { - get - { - // Headers and values: - // Connection: Upgrade - // Upgrade: WebSocket - // Sec-WebSocket-Version: (WebSocketProtocolComponent.SupportedVersion) - // Sec-WebSocket-Key: (hash, see WebSocketHelpers.GetSecWebSocketAcceptString) - // Sec-WebSocket-Protocol: (optional, list) - IList connectionHeaders = _context.Request.Headers.GetCommaSeparatedValues(HttpKnownHeaderNames.Connection); // "Upgrade, KeepAlive" - string upgradeHeader = _context.Request.Headers[HttpKnownHeaderNames.Upgrade]; - string versionHeader = _context.Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion]; - string keyHeader = _context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey]; - - if (connectionHeaders != null && connectionHeaders.Count > 0 - && connectionHeaders.Contains(HttpKnownHeaderNames.Upgrade, StringComparer.OrdinalIgnoreCase) - && string.Equals(upgradeHeader, WebSocketHelpers.WebSocketUpgradeToken, StringComparison.OrdinalIgnoreCase) - && string.Equals(versionHeader, UnsafeNativeMethods.WebSocketProtocolComponent.SupportedVersion, StringComparison.OrdinalIgnoreCase) - && !string.IsNullOrWhiteSpace(keyHeader)) - { - return true; - } - return false; - } - } - - public async Task AcceptAsync(IWebSocketAcceptContext acceptContext) - { - // Get options - if (acceptContext != null) - { - _subProtocol = acceptContext.SubProtocol; - } - - var advancedAcceptContext = acceptContext as WebSocketAcceptContext; - if (advancedAcceptContext != null) - { - if (advancedAcceptContext.ReceiveBufferSize.HasValue) - { - _receiveBufferSize = advancedAcceptContext.ReceiveBufferSize.Value; - } - if (advancedAcceptContext.KeepAliveInterval.HasValue) - { - _keepAliveInterval = advancedAcceptContext.KeepAliveInterval.Value; - } - _internalBuffer = advancedAcceptContext.Buffer; - } - - if (!_internalBuffer.HasValue) - { - _internalBuffer = WebSocketBuffer.CreateInternalBufferArraySegment(_receiveBufferSize, WebSocketBuffer.MinSendBufferSize, true); - } - - // Set WebSocket upgrade response headers - - string outgoingSecWebSocketProtocolString; - bool shouldSendSecWebSocketProtocolHeader = - WebSocketHelpers.ProcessWebSocketProtocolHeader( - _context.Request.Headers[HttpKnownHeaderNames.SecWebSocketProtocol], - _subProtocol, - out outgoingSecWebSocketProtocolString); - - if (shouldSendSecWebSocketProtocolHeader) - { - _context.Response.Headers[HttpKnownHeaderNames.SecWebSocketProtocol] = outgoingSecWebSocketProtocolString; - } - - string secWebSocketKey = _context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey]; - string secWebSocketAccept = WebSocketHelpers.GetSecWebSocketAcceptString(secWebSocketKey); - - _context.Response.Headers[HttpKnownHeaderNames.Connection] = HttpKnownHeaderNames.Upgrade; - _context.Response.Headers[HttpKnownHeaderNames.Upgrade] = WebSocketHelpers.WebSocketUpgradeToken; - _context.Response.Headers[HttpKnownHeaderNames.SecWebSocketAccept] = secWebSocketAccept; - - // 101 Switching Protocols; - Stream opaqueTransport = await _upgrade.UpgradeAsync(); - return new ServerWebSocket(opaqueTransport, _subProtocol, _receiveBufferSize, _keepAliveInterval, _internalBuffer.Value); - } - } - - public class WebSocketAcceptContext : IWebSocketAcceptContext - { - public string SubProtocol { get; set; } - public int? ReceiveBufferSize { get; set; } - public TimeSpan? KeepAliveInterval { get; set; } - public ArraySegment? Buffer { get; set; } - } - } -}*/ \ No newline at end of file diff --git a/src/Microsoft.Net.WebSockets/build.cmd b/src/Microsoft.Net.WebSockets/build.cmd deleted file mode 100644 index 110694d575..0000000000 --- a/src/Microsoft.Net.WebSockets/build.cmd +++ /dev/null @@ -1,3 +0,0 @@ -rem set TARGET_FRAMEWORK=k10 -@call ..\..\packages\ProjectK.0.0.1-pre-30121-096\tools\k build -pause \ No newline at end of file