/// <summary>
        /// Creates a <see cref="WebSocketConnectionInfo"/> for a given request and IP address.
        /// </summary>
        /// <param name="request">The <see cref="WebSocketHttpRequest"/> to get the connection information for.</param>
        /// <param name="clientIPAddress">The IP address of the client connection.</param>
        /// <returns>The created <see cref="WebSocketConnectionInfo"/>.</returns>
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIPAddress)
        {
            var info = new WebSocketConnectionInfo
            {
                Origin = request["Origin"] ?? request["Sec-WebSocket-Origin"],
                Host = request["Host"],
                SubProtocol = request["Sec-WebSocket-Protocol"],
                Path = request.Path,
                ClientIPAddress = clientIPAddress
            };

            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;
        }
        /// <summary>
        /// Creates a <see cref="WebSocketConnectionInfo"/> for a given request and IP address.
        /// </summary>
        /// <param name="request">The <see cref="WebSocketHttpRequest"/> to get the connection information for.</param>
        /// <param name="clientIPAddress">The IP address of the client connection.</param>
        /// <returns>The created <see cref="WebSocketConnectionInfo"/>.</returns>
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIPAddress)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request", "Request to create WebSocket must not be null");
            }

            var info = new WebSocketConnectionInfo
            {
                Origin          = request["Origin"] ?? request["Sec-WebSocket-Origin"],
                Host            = request["Host"],
                SubProtocol     = request["Sec-WebSocket-Protocol"],
                Path            = request.Path,
                ClientIPAddress = clientIPAddress
            };

            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.º 3
0
        /// <summary>
        /// Parses the specified data into a <see cref="WebSocketHttpRequest"/> for the given scheme.
        /// </summary>
        /// <param name="requestData">The data to be parsed.</param>
        /// <param name="scheme">The scheme to use in parsing the data.</param>
        /// <returns>The parsed <see cref="WebSocketHttpRequest"/>.</returns>
        public WebSocketHttpRequest Parse(byte[] requestData, string scheme)
        {
            var body = Encoding.UTF8.GetString(requestData);
            Match match = ParserRegex.Match(body);

            if (!match.Success)
            {
                return null;
            }

            var request = new WebSocketHttpRequest
            {
                Method = match.Groups["method"].Value,
                Path = match.Groups["path"].Value,
                Body = match.Groups["body"].Value,
                Payload = requestData,
                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
        /// <summary>
        /// Parses the specified data into a <see cref="WebSocketHttpRequest"/> for the given scheme.
        /// </summary>
        /// <param name="requestData">The data to be parsed.</param>
        /// <param name="scheme">The scheme to use in parsing the data.</param>
        /// <returns>The parsed <see cref="WebSocketHttpRequest"/>.</returns>
        public WebSocketHttpRequest Parse(byte[] requestData, string scheme)
        {
            var   body  = Encoding.UTF8.GetString(requestData);
            Match match = ParserRegex.Match(body);

            if (!match.Success)
            {
                return(null);
            }

            var request = new WebSocketHttpRequest
            {
                Method  = match.Groups["method"].Value,
                Path    = match.Groups["path"].Value,
                Body    = match.Groups["body"].Value,
                Payload = requestData,
                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.º 5
0
        /// <summary>
        /// Creates a handler to handle a <see cref="WebSocketHttpRequest"/>.
        /// </summary>
        /// <param name="request">The <see cref="WebSocketHttpRequest"/> to create the handler for.</param>
        /// <returns>An <see cref="IHandler"/> object that can handle the specific protocol version
        /// of the request.</returns>
        public static IHandler BuildHandler(WebSocketHttpRequest request)
        {
            var version = GetVersion(request);

            switch (version)
            {
                case "76":
                    return Hybi00Handler.Create(request);
                case "7":
                case "8":
                case "13":
                    return Rfc6455Handler.Create(request);
            }

            throw new WebSocketException(WebSocketStatusCodes.UnsupportedDataType, "Unsupported protocol version.");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a handler to handle a <see cref="WebSocketHttpRequest"/>.
        /// </summary>
        /// <param name="request">The <see cref="WebSocketHttpRequest"/> to create the handler for.</param>
        /// <returns>An <see cref="IHandler"/> object that can handle the specific protocol version
        /// of the request.</returns>
        public static IHandler BuildHandler(WebSocketHttpRequest request)
        {
            var version = GetVersion(request);

            switch (version)
            {
            case "76":
                return(Hybi00Handler.Create(request));

            case "7":
            case "8":
            case "13":
                return(Rfc6455Handler.Create(request));
            }

            throw new WebSocketException(WebSocketStatusCodes.UnsupportedDataType, "Unsupported protocol version.");
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the version of an <see cref="WebSocketHttpRequest"/>.
        /// </summary>
        /// <param name="request">The <see cref="WebSocketHttpRequest"/> to get the protocol version of.</param>
        /// <returns>A string containing the version of the protocol of the request.</returns>
        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";
            }

            return "75";
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the version of an <see cref="WebSocketHttpRequest"/>.
        /// </summary>
        /// <param name="request">The <see cref="WebSocketHttpRequest"/> to get the protocol version of.</param>
        /// <returns>A string containing the version of the protocol of the request.</returns>
        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");
            }

            return("75");
        }