Exemplo n.º 1
0
        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,
                Headers               = new Dictionary <string, string>(request.Headers, System.StringComparer.InvariantCultureIgnoreCase)
            };
            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);
        }
Exemplo n.º 2
0
        public static string GetVersion(WebSocketHttpRequest request)
        {
            string version;

            if (request.Headers.TryGetValue("Sec-WebSocket-Version", out version))
            {
                return(version);
            }

            if (request.Headers.TryGetValue("Sec-WebSocket-Draft", out version))
            {
                return(version);
            }

            if (request.Headers.ContainsKey("Sec-WebSocket-Key1"))
            {
                return("76");
            }

            if ((request.Body != null) && request.Body.ToLower().Contains("policy-file-request"))
            {
                return("policy-file-request");
            }

            return("75");
        }
Exemplo n.º 3
0
        public static WebSocketHttpRequest Parse(byte[] bytes, string scheme)
        {
            // Check for websocket request header
            var   body  = Encoding.UTF8.GetString(bytes);
            Match match = _regex.Match(body);

            if (!match.Success)
            {
                // No websocket request header found, check for a flash socket policy request
                match = _FlashSocketPolicyRequestRegex.Match(body);
                if (match.Success)
                {
                    // It's a flash socket policy request, so return
                    return(new WebSocketHttpRequest
                    {
                        Body = body,
                        Bytes = bytes
                    });
                }
                else
                {
                    return(null);
                }
            }

            var request = new WebSocketHttpRequest
            {
                Method = match.Groups["method"].Value,
                Path   = match.Groups["path"].Value,
                Body   = match.Groups["body"].Value,
                Bytes  = bytes,
                Scheme = scheme
            };

            var fields = match.Groups["field_name"].Captures;
            var values = match.Groups["field_value"].Captures;

            for (var i = 0; i < fields.Count; i++)
            {
                var name  = fields[i].ToString();
                var value = values[i].ToString();
                request.Headers[name] = value;
            }

            return(request);
        }
Exemplo n.º 4
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, Action <string> onMessage, Action onClose, Action <byte[]> onBinary, Action <byte[]> onPing, Action <byte[]> onPong)
        {
            var version = GetVersion(request);

            switch (version)
            {
            case "76":
                return(Draft76Handler.Create(request, onMessage));

            case "7":
            case "8":
            case "13":
                return(Hybi13Handler.Create(request, onMessage, onClose, onBinary, onPing, onPong));

            case "policy-file-request":
                return(FlashSocketPolicyRequestHandler.Create(request));
            }

            throw new WebSocketException(WebSocketStatusCodes.UnsupportedDataType);
        }