예제 #1
0
        public static bool IsWebSocketRequest(this RequestContext context)
        {
            if (!WebSocketHelpers.AreWebSocketsSupported)
            {
                return(false);
            }

            if (!context.IsUpgradableRequest)
            {
                return(false);
            }

            if (!string.Equals("GET", context.Request.Method, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
            string connection = context.Request.Headers[HttpKnownHeaderNames.Connection];

            if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
            {
                return(false);
            }

            // Upgrade: websocket
            string upgrade = context.Request.Headers[HttpKnownHeaderNames.Upgrade];

            if (!string.Equals(WebSocketHelpers.WebSocketUpgradeToken, upgrade, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Sec-WebSocket-Version: 13
            string version = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion];

            if (!string.Equals(WebSocketConstants.SupportedProtocolVersion, version, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // Sec-WebSocket-Key: {base64string}
            string key = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];

            if (!WebSocketHelpers.IsValidWebSocketKey(key))
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        // Compare IsWebSocketRequest()
        private static void ValidateWebSocketRequest(RequestContext context)
        {
            if (!WebSocketHelpers.AreWebSocketsSupported)
            {
                throw new NotSupportedException("WebSockets are not supported on this platform.");
            }

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

            if (!string.Equals("GET", context.Request.Method, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("This request is not a valid upgrade request; invalid verb: " + context.Request.Method);
            }

            // Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
            string connection = context.Request.Headers[HttpKnownHeaderNames.Connection];

            if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
            {
                throw new InvalidOperationException("The Connection header is invalid: " + connection);
            }

            // Upgrade: websocket
            string upgrade = context.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
            string version = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketVersion];

            if (!string.Equals(WebSocketConstants.SupportedProtocolVersion, version, StringComparison.OrdinalIgnoreCase))
            {
                throw new InvalidOperationException("The Sec-WebSocket-Version header is invalid or not supported: " + version);
            }

            // Sec-WebSocket-Key: {base64string}
            string key = context.Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];

            if (!WebSocketHelpers.IsValidWebSocketKey(key))
            {
                throw new InvalidOperationException("The Sec-WebSocket-Key header is invalid: " + upgrade);
            }
        }