Exemple #1
0
        public override bool Handshake(IWebSocketSession session, WebSocketReaderBase previousReader, out ICommandReader <IWebSocketFragment> dataFrameReader)
        {
            if (!VersionTag.Equals(session.SecWebSocketVersion) && NextProcessor != null)
            {
                return(NextProcessor.Handshake(session, previousReader, out dataFrameReader));
            }

            dataFrameReader = null;

            session.ProtocolProcessor = this;

            var secWebSocketKey = session.Items.GetValue <string>(WebSocketConstant.SecWebSocketKey, string.Empty);

            if (string.IsNullOrEmpty(secWebSocketKey))
            {
                return(false);
            }

            var responseBuilder = new StringBuilder();

            string secKeyAccept = string.Empty;

            try
            {
                secKeyAccept = Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(secWebSocketKey + m_Magic)));
            }
            catch (Exception)
            {
                return(false);
            }

            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseHeadLine10);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseUpgradeLine);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseConnectionLine);
            responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseAcceptLine, secKeyAccept);

            var subProtocol = session.GetAvailableSubProtocol(session.Items.GetValue <string>(WebSocketConstant.SecWebSocketProtocol, string.Empty));

            if (!string.IsNullOrEmpty(subProtocol))
            {
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseProtocolLine, subProtocol);
            }

            responseBuilder.AppendWithCrCf();
            session.SocketSession.SendResponse(responseBuilder.ToString());

            dataFrameReader = new WebSocketDataFrameReader(session.AppServer);

            return(true);
        }
Exemple #2
0
        public override bool Handshake(IWebSocketSession session, WebSocketReaderBase previousReader, out ICommandReader <IWebSocketFragment> dataFrameReader)
        {
            var secKey1 = session.Items.GetValue <string>(WebSocketConstant.SecWebSocketKey1, string.Empty);
            var secKey2 = session.Items.GetValue <string>(WebSocketConstant.SecWebSocketKey2, string.Empty);

            if (string.IsNullOrEmpty(secKey1) && string.IsNullOrEmpty(secKey2) && NextProcessor != null)
            {
                return(NextProcessor.Handshake(session, previousReader, out dataFrameReader));
            }

            session.ProtocolProcessor = this;

            var secKey3 = session.Items.GetValue <byte[]>(WebSocketConstant.SecWebSocketKey3, m_ZeroKeyBytes);

            var responseBuilder = new StringBuilder();

            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseHeadLine00);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseUpgradeLine);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseConnectionLine);

            if (!string.IsNullOrEmpty(session.Origin))
            {
                Extensions.AppendFormatWithCrCf(responseBuilder, WebSocketConstant.ResponseOriginLine, session.Origin);
            }

            Extensions.AppendFormatWithCrCf(responseBuilder, WebSocketConstant.ResponseLocationLine, session.UriScheme, session.Host, session.Path);

            var subProtocol = session.GetAvailableSubProtocol(session.Items.GetValue <string>(WebSocketConstant.SecWebSocketProtocol, string.Empty));

            if (!string.IsNullOrEmpty(subProtocol))
            {
                Extensions.AppendFormatWithCrCf(responseBuilder, WebSocketConstant.ResponseProtocolLine, subProtocol);
            }

            responseBuilder.AppendWithCrCf();
            session.SocketSession.SendResponse(responseBuilder.ToString());
            //Encrypt message
            byte[] secret = GetResponseSecurityKey(secKey1, secKey2, secKey3);
            session.SocketSession.SendResponse(secret, 0, secret.Length);

            dataFrameReader = new WebSocketDataReader(previousReader);

            return(true);
        }
 public bool Handshake(IWebSocketSession session, WebSocketReaderBase previousReader, out ICommandReader <IWebSocketFragment> dataFrameReader)
 {
     dataFrameReader = null;
     session.SocketSession.SendResponse(m_SwitchResponse, 0, m_SwitchResponse.Length);
     return(true);
 }
Exemple #4
0
 public WebSocketSecKey3Reader(WebSocketReaderBase prevReader)
     : base(prevReader)
 {
 }
 public abstract bool Handshake(IWebSocketSession session, WebSocketReaderBase previousReader, out ICommandReader <IWebSocketFragment> dataFrameReader);
 public WebSocketReaderBase(WebSocketReaderBase previousCommandReader)
     : base(previousCommandReader)
 {
 }
 public WebSocketDataReader(WebSocketReaderBase prevReader)
     : base(prevReader)
 {
 }