Esempio n. 1
0
        /// <summary>
        /// Check for an upgrade to a web socket connection.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        private IWebSocketRequestHandler UpgradeToWebsocket(HttpRequest request, HttpResponse response)
        {
            // Check for required headers
            if (!(request.Headers.ContainsKey(HttpHeaders.Connection) && request.Headers[HttpHeaders.Connection].ToLower().Contains("upgrade")))
            {
                return(null);
            }
            if (!(request.Headers.ContainsKey(HttpHeaders.Upgrade) && request.Headers[HttpHeaders.Upgrade].ToLower().Contains("websocket")))
            {
                return(null);
            }
            if (!request.Headers.ContainsKey(SecWebSocketVersion))
            {
                return(null);
            }
            int version;

            if (!(int.TryParse(request.Headers[SecWebSocketVersion], out version) && (version == 13)))
            {
                return(null);
            }
            if (!request.Headers.ContainsKey(SecWebSocketKey))
            {
                return(null);
            }
            // Make sure we have a handler for the URI
            string partial;
            IWebSocketRequestHandler handler = m_server.GetHandlerForWebSocket(request.URI, out partial);

            if (handler == null)
            {
                return(null);
            }
            // Do we support the protocols requested?
            string protocol = null;

            if (request.Headers.ContainsKey(SecWebSocketProtocol))
            {
                foreach (string proto in request.Headers[SecWebSocketProtocol].Split(WebSocketProtocolSeparator))
                {
                    if (handler.WillAcceptRequest(partial, proto.Trim()))
                    {
                        protocol = proto.Trim();
                        break;
                    }
                }
            }
            else if (handler.WillAcceptRequest(partial, ""))
            {
                protocol = "";
            }
            if (protocol == null)
            {
                return(null);
            }
            // Finish the handshake
            byte[] security = Encoding.UTF8.GetBytes(request.Headers[SecWebSocketKey].Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();

            sha1.Initialize();
            sha1.HashCore(security, 0, security.Length);
            security = sha1.HashFinal();
            response.Headers[SecWebSocketAccept]     = Convert.ToBase64String(security);
            response.Headers[HttpHeaders.Upgrade]    = "websocket";
            response.Headers[HttpHeaders.Connection] = "Upgrade";
            response.ResponseCode = HttpResponseCode.SwitchingProtocols;
            if (protocol.Length > 0)
            {
                response.Headers[SecWebSocketProtocol] = protocol;
            }
            // And we are done
            return(handler);
        }