Пример #1
0
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIp, int clientPort)
        {
            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
                           };
            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;
        }
Пример #2
0
        public static WebSocketHttpRequest Parse(byte[] bytes, string scheme)
        {
            var   body  = Encoding.UTF8.GetString(bytes);
            Match match = Regex.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,
                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);
        }
Пример #3
0
        public static WebSocketHttpRequest Parse(byte[] bytes, string scheme)
        {
            var body = Encoding.UTF8.GetString(bytes);
            Match match = Regex.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,
                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;
        }
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIp, int clientPort)
        {
            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
            };
            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);
        }
Пример #5
0
        public void ShouldReturnHandlerForValidHeaders()
        {
            var request = new WebSocketHttpRequest {Headers = {{"Sec-WebSocket-Key1", "BLAH"}}};
            var handler = HandlerFactory.BuildHandler(request, x => { }, () => { }, x => { });

            Assert.IsNotNull(handler);
        }
Пример #6
0
        public void Setup()
        {
            _request = new WebSocketHttpRequest();
            _onMessage = delegate { };

            _handler = Draft76Handler.Create(_request, s => _onMessage(s));
        }
        public void ShouldReadHeadersFromRequest()
        {
            const string origin = "http://blah.com/path/to/page";
            const string host = "blah.com";
            const string subprotocol = "Submarine!";
            const string path = "/path/to/page";
            const string clientIp = "127.0.0.1";
            const int clientPort = 0;

            var request =
                new WebSocketHttpRequest
                    {
                        Headers =
                            {
                                {"Origin", origin},
                                {"Host", host},
                                {"Sec-WebSocket-Protocol", subprotocol}
                            },
                        Path = path
                    };
            var info = WebSocketConnectionInfo.Create(request, clientIp, clientPort);

            Assert.AreEqual(origin, info.Origin);
            Assert.AreEqual(host, info.Host);
            Assert.AreEqual(subprotocol, info.SubProtocol);
            Assert.AreEqual(path, info.Path);
            Assert.AreEqual(clientIp, info.ClientIpAddress);
        }
Пример #8
0
        public void Setup()
        {
            _request = new WebSocketHttpRequest();
            _onClose = delegate { };
            _onMessage = delegate { };
            _onBinary = delegate { };

            _handler = Hybi13Handler.Create(_request, s => _onMessage(s), () => _onClose(), b => _onBinary(b));
        }
        public void ShouldReadSecWebSocketOrigin()
        {
            const string origin = "http://example.com/myPath";
            var request =
                new WebSocketHttpRequest
                    {
                        Headers = { {"Sec-WebSocket-Origin", origin} }
                    };
            var info = WebSocketConnectionInfo.Create(request, null, 1);

            Assert.AreEqual(origin, info.Origin);
        }
        public void ShouldParseCookies()
        {
            const string cookie = "chocolate=tasty; cabbage=not so much";
            var request =
                new WebSocketHttpRequest
                    {
                        Headers = { {"Cookie", cookie} }
                    };

            var info = WebSocketConnectionInfo.Create(request, null, 1);
            Assert.AreEqual(info.Cookies["chocolate"], "tasty");
            Assert.AreEqual(info.Cookies["cabbage"], "not so much");
        }
Пример #11
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";

            return "75";
        }
Пример #12
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, Action<string> onMessage, Fleck2Extensions.Action onClose, Action<byte[]> onBinary)
        {
            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);
            }

            throw new WebSocketException(WebSocketStatusCodes.UnsupportedDataType);
        }
Пример #13
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, Action <string> onMessage, Fleck2Extensions.Action onClose, Action <byte[]> onBinary)
        {
            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));
            }

            throw new WebSocketException(WebSocketStatusCodes.UnsupportedDataType);
        }
Пример #14
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");
            }

            return("75");
        }
Пример #15
0
 public void ShouldThrowWhenUnsupportedType()
 {
     var request = new WebSocketHttpRequest {Headers = {{"Bad", "Request"}}};
     Assert.Throws<WebSocketException>(() => HandlerFactory.BuildHandler(request, x => {}, () => {}, x => { }));
 }
 public void ShouldHaveId()
 {
     var request = new WebSocketHttpRequest();
     var info = WebSocketConnectionInfo.Create(request, null, 1);
     Assert.AreNotEqual(default(Guid), info.Id);
 }