Пример #1
0
        public async Task <WebSocketHandshake> HandshakeAsync(Stream clientStream)
        {
            WebSocketHandshake handshake = new WebSocketHandshake();

            try
            {
                ReadHttpRequest(clientStream, handshake);
                if (!(handshake.Request.Headers.HeaderNames.Contains("Host") &&
                      handshake.Request.Headers.HeaderNames.Contains("Upgrade") && "websocket".Equals(handshake.Request.Headers["Upgrade"], StringComparison.OrdinalIgnoreCase) &&
                      handshake.Request.Headers.HeaderNames.Contains("Connection") &&
                      handshake.Request.Headers.HeaderNames.Contains("Sec-WebSocket-Key") && !String.IsNullOrWhiteSpace(handshake.Request.Headers["Sec-WebSocket-Key"]) &&
                      handshake.Request.Headers.HeaderNames.Contains("Sec-WebSocket-Version")))
                {
                    await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);

                    return(handshake);
                }

                handshake.IsWebSocketRequest = true;

                handshake.Factory = _factories.GetWebSocketFactory(handshake.Request);
                if (handshake.Factory == null)
                {
                    await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);

                    return(handshake);
                }

                handshake.IsVersionSupported = true;

                ConsolidateObjectModel(handshake);

                SelectExtensions(handshake);

                RunHttpNegotiationHandler(handshake);

                await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                handshake.Error   = ExceptionDispatchInfo.Capture(ex);
                handshake.IsValid = false;
                if (!handshake.IsResponseSent)
                {
                    try { WriteHttpResponse(handshake, clientStream); }
                    catch (Exception ex2)
                    {
                        DebugLog.Fail("HttpNegotiationQueue.WorkAsync (Writting error esponse)", ex2);
                    };
                }
            }
            return(handshake);
        }
Пример #2
0
        public async Task <WebSocketHandshake> HandshakeAsync(Stream clientStream)
        {
            WebSocketHandshake handshake = new WebSocketHandshake();

            try
            {
                ReadHttpRequest(clientStream, handshake);
                if (!IsHttpHeadersValid(handshake))
                {
                    await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);

                    return(handshake);
                }

                handshake.IsWebSocketRequest = true;

                handshake.Factory = _factories.GetWebSocketFactory(handshake.Request);
                if (handshake.Factory == null)
                {
                    await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);

                    return(handshake);
                }

                handshake.IsVersionSupported = true;

                ConsolidateObjectModel(handshake);

                SelectExtensions(handshake);

                RunHttpNegotiationHandler(handshake);

                await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                handshake.Error   = ExceptionDispatchInfo.Capture(ex);
                handshake.IsValid = false;
                if (!handshake.IsResponseSent)
                {
                    try { WriteHttpResponse(handshake, clientStream); }
                    catch (Exception ex2)
                    {
                        DebugLog.Fail("HttpNegotiationQueue.WorkAsync (Writting error esponse)", ex2);
                    };
                }
            }
            return(handshake);
        }
        public async Task <WebSocketHandshake> HandshakeAsync(Stream clientStream)
        {
            WebSocketHandshake handshake = new WebSocketHandshake();

            try
            {
                ReadHttpRequest(clientStream, handshake);
                if (!(handshake.Request.Headers.AllKeys.Contains("Host") &&
                      handshake.Request.Headers.AllKeys.Contains("Upgrade") && "websocket".Equals(handshake.Request.Headers["Upgrade"], StringComparison.InvariantCultureIgnoreCase) &&
                      handshake.Request.Headers.AllKeys.Contains("Connection") &&
                      handshake.Request.Headers.AllKeys.Contains("Sec-WebSocket-Key") && !String.IsNullOrWhiteSpace(handshake.Request.Headers["Sec-WebSocket-Key"]) &&
                      handshake.Request.Headers.AllKeys.Contains("Sec-WebSocket-Version")))
                {
                    await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);

                    return(handshake);
                }

                handshake.IsWebSocketRequest = true;

                handshake.Factory = _factories.GetWebSocketFactory(handshake.Request);
                if (handshake.Factory == null)
                {
                    await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);

                    return(handshake);
                }

                handshake.IsVersionSupported = true;

                ConsolidateObjectModel(handshake);

                SelectExtensions(handshake);

                if (_options.OnHttpNegotiation != null)
                {
                    try
                    {
                        _options.OnHttpNegotiation(handshake.Request, handshake.Response);
                    }
                    catch (Exception onNegotiationHandlerError)
                    {
                        handshake.Response.Status = HttpStatusCode.InternalServerError;
                        handshake.Error           = ExceptionDispatchInfo.Capture(onNegotiationHandlerError);
                    }
                }

                await WriteHttpResponseAsync(handshake, clientStream).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                handshake.Error   = ExceptionDispatchInfo.Capture(ex);
                handshake.IsValid = false;
                if (!handshake.IsResponseSent)
                {
                    try { WriteHttpResponse(handshake, clientStream); }
                    catch { };
                }
            }
            return(handshake);
        }