Skip to content
This repository has been archived by the owner on Mar 19, 2019. It is now read-only.

Commit

Permalink
#283 remove direct WebSocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Jan 5, 2017
1 parent d04dc71 commit bd54ec5
Show file tree
Hide file tree
Showing 11 changed files with 0 additions and 2,195 deletions.
44 changes: 0 additions & 44 deletions scripts/UpdateCoreFxCode.ps1

This file was deleted.

17 changes: 0 additions & 17 deletions src/Microsoft.AspNetCore.Server.HttpSys/FeatureContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.WebSockets;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
Expand All @@ -28,7 +27,6 @@ internal class FeatureContext :
// ITlsTokenBindingFeature, TODO: https://github.com/aspnet/WebListener/issues/231
IHttpBufferingFeature,
IHttpRequestLifetimeFeature,
IHttpWebSocketFeature,
IHttpAuthenticationFeature,
IHttpUpgradeFeature,
IHttpRequestIdentifierFeature
Expand Down Expand Up @@ -442,21 +440,6 @@ async Task<Stream> IHttpUpgradeFeature.UpgradeAsync()
return await _requestContext.UpgradeAsync();
}

bool IHttpWebSocketFeature.IsWebSocketRequest => _requestContext.IsWebSocketRequest;

async Task<WebSocket> IHttpWebSocketFeature.AcceptAsync(WebSocketAcceptContext context)
{
// TODO: Advanced params
string subProtocol = null;
if (context != null)
{
subProtocol = context.SubProtocol;
}

await OnStart();
return await _requestContext.AcceptWebSocketAsync(subProtocol);
}

ClaimsPrincipal IHttpAuthenticationFeature.User
{
get { return _user; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.WebSockets;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Claims;
using System.Threading;
Expand Down Expand Up @@ -110,155 +109,6 @@ public Task<Stream> UpgradeAsync()
return Task.FromResult<Stream>(opaqueStream);
}

// Compare ValidateWebSocketRequest
public bool IsWebSocketRequest
{
get
{
if (!WebSocketHelpers.AreWebSocketsSupported)
{
return false;
}

if (!IsUpgradableRequest)
{
return false;
}

if (Request.KnownMethod != HttpApi.HTTP_VERB.HttpVerbGET)
{
return false;
}

// Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
var connection = Request.Headers[HttpKnownHeaderNames.Connection].ToString();
if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
{
return false;
}

// Upgrade: websocket
var upgrade = Request.Headers[HttpKnownHeaderNames.Upgrade];
if (!string.Equals(WebSocketHelpers.WebSocketUpgradeToken, upgrade, StringComparison.OrdinalIgnoreCase))
{
return false;
}

// Sec-WebSocket-Version: 13
var version = Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion];
if (!string.Equals(WebSocketHelpers.SupportedProtocolVersion, version, StringComparison.OrdinalIgnoreCase))
{
return false;
}

// Sec-WebSocket-Key: {base64string}
var key = Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];
if (!WebSocketHelpers.IsValidWebSocketKey(key))
{
return false;
}

return true;
}
}

// Compare IsWebSocketRequest()
private void ValidateWebSocketRequest()
{
if (!WebSocketHelpers.AreWebSocketsSupported)
{
throw new NotSupportedException("WebSockets are not supported on this platform.");
}

if (!IsUpgradableRequest)
{
throw new InvalidOperationException("This request is not a valid upgrade request.");
}

if (Request.KnownMethod != HttpApi.HTTP_VERB.HttpVerbGET)
{
throw new InvalidOperationException("This request is not a valid upgrade request; invalid verb: " + Request.Method);
}

// Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
var connection = Request.Headers[HttpKnownHeaderNames.Connection].ToString();
if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
{
throw new InvalidOperationException("The Connection header is invalid: " + connection);
}

// Upgrade: websocket
var upgrade = Request.Headers[HttpKnownHeaderNames.Upgrade];
if (!string.Equals(WebSocketHelpers.WebSocketUpgradeToken, upgrade, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("The Upgrade header is invalid: " + upgrade);
}

// Sec-WebSocket-Version: 13
var version = Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion];
if (!string.Equals(WebSocketHelpers.SupportedProtocolVersion, version, StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("The Sec-WebSocket-Version header is invalid or not supported: " + version);
}

// Sec-WebSocket-Key: {base64string}
var key = Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];
if (!WebSocketHelpers.IsValidWebSocketKey(key))
{
throw new InvalidOperationException("The Sec-WebSocket-Key header is invalid: " + upgrade);
}
}

public Task<WebSocket> AcceptWebSocketAsync()
{
return AcceptWebSocketAsync(null, WebSocketHelpers.DefaultReceiveBufferSize, WebSocketHelpers.DefaultKeepAliveInterval);
}

public Task<WebSocket> AcceptWebSocketAsync(string subProtocol)
{
return AcceptWebSocketAsync(subProtocol, WebSocketHelpers.DefaultReceiveBufferSize, WebSocketHelpers.DefaultKeepAliveInterval);
}

public Task<WebSocket> AcceptWebSocketAsync(string subProtocol, TimeSpan keepAliveInterval)
{
return AcceptWebSocketAsync(subProtocol, WebSocketHelpers.DefaultReceiveBufferSize, keepAliveInterval);
}

public Task<WebSocket> AcceptWebSocketAsync(string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval)
{
if (!IsUpgradableRequest)
{
throw new InvalidOperationException("This request cannot be upgraded.");
}
WebSocketHelpers.ValidateOptions(subProtocol, keepAliveInterval);

return AcceptWebSocketAsyncCore(subProtocol, receiveBufferSize, keepAliveInterval);
}

private async Task<WebSocket> AcceptWebSocketAsyncCore(string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval)
{
ValidateWebSocketRequest();

var subProtocols = Request.Headers.GetValues(HttpKnownHeaderNames.SecWebSocketProtocol);
var shouldSendSecWebSocketProtocolHeader = WebSocketHelpers.ProcessWebSocketProtocolHeader(subProtocols, subProtocol);
if (shouldSendSecWebSocketProtocolHeader)
{
Response.Headers[HttpKnownHeaderNames.SecWebSocketProtocol] = subProtocol;
}

// negotiate the websocket key return value
var secWebSocketKey = Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];
var secWebSocketAccept = WebSocketHelpers.GetSecWebSocketAcceptString(secWebSocketKey);

Response.Headers.Append(HttpKnownHeaderNames.Connection, HttpKnownHeaderNames.Upgrade);
Response.Headers.Append(HttpKnownHeaderNames.Upgrade, WebSocketHelpers.WebSocketUpgradeToken);
Response.Headers.Append(HttpKnownHeaderNames.SecWebSocketAccept, secWebSocketAccept);

var opaqueStream = await UpgradeAsync();

return WebSocketHelpers.CreateServerWebSocket(opaqueStream, subProtocol, receiveBufferSize, keepAliveInterval);
}

// TODO: Public when needed
internal bool TryGetChannelBinding(ref ChannelBinding value)
{
Expand Down
Loading

0 comments on commit bd54ec5

Please sign in to comment.