public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIp, int clientPort, string negotiatedSubprotocol)
        {
            var info = new WebSocketConnectionInfo
            {
                Origin                = request["Origin"] ?? request["Sec-WebSocket-Origin"],
                Host                  = request["Host"],
                SubProtocol           = request["Sec-WebSocket-Protocol"],
                Path                  = request.Path,
                ClientIpAddress       = clientIp,
                ClientPort            = clientPort,
                NegotiatedSubProtocol = negotiatedSubprotocol
            };
            var cookieHeader = request["Cookie"];

            if (cookieHeader != null)
            {
                var match  = CookieRegex.Match(cookieHeader);
                var fields = match.Groups["cookie_name"].Captures;
                var values = match.Groups["cookie_value"].Captures;
                for (var i = 0; i < fields.Count; i++)
                {
                    var name  = fields[i].ToString();
                    var value = values[i].ToString();
                    info.Cookies[name] = value;
                }
            }

            return(info);
        }
Ejemplo n.º 2
0
        public void CreateHandler(IEnumerable <byte> data)
        {
            byte[] bytes   = data.ToArray();
            var    request = RequestParser.Parse(bytes, owner.Schema);

            if (request == null)
            {
                return;
            }
            handler = WebSocketHandlerFactory.BuildHandler(request, SockectConnection.OnMessage, SockectConnection.OnClose, SockectConnection.OnBinary);
            if (handler == null)
            {
                return;
            }
            var subProtocol = SubProtocolNegotiator.Negotiate(owner.SupportedSubProtocols, request.SubProtocols);

            ConnectionInfo = WebSocketConnectionInfo.Create(request, SockectConnection.Socket.RemoteIpAddress, SockectConnection.Socket.RemotePort, subProtocol);

            if (!string.IsNullOrEmpty(ConnectionInfo.Path))
            {
                SockectConnection.Session.Add("WebSocket_Path", ConnectionInfo.Path);
            }

            foreach (string item in ConnectionInfo.Cookies.Keys)
            {
                SockectConnection.Session.Add(item, ConnectionInfo.Cookies[item]);
            }

            var handshake = handler.CreateHandshake(subProtocol);

            SockectConnection.RawSend(handshake);
        }