示例#1
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);
        }
示例#2
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
                           };

            var cookieHeader = request["Cookie"];
            if (cookieHeader != null)
            {
                var cookies = cookieHeader.Split(';');
                foreach (var cookie in cookies)
                {
                    var parts = cookie.Split('=');
                    if (parts.Length == 2)
                        info.Cookies.Add(parts[0], parts[1]);
                }
            }

            return info;
        }
        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);
        }
示例#4
0
文件: RequestParser.cs 项目: t00/seb
        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;
        }
示例#5
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");
        }
        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;
        }
示例#7
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
                           };

            var cookieHeader = request["Cookie"];
            if (cookieHeader != null)
            {
                var cookies = cookieHeader.Split(';');
                foreach (var cookie in cookies)
                {
                    var parts = cookie.Split('=');
                    if (parts.Length == 2)
                        info.Cookies.Add(parts[0], parts[1]);
                }
            }

            return info;
        }
示例#8
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);
        }
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, string clientIp)
        {
            var info = new WebSocketConnectionInfo
                           {
                               Origin = request["Origin"] ?? request["Sec-WebSocket-Origin"],
                               Host = request["Host"],
                               SubProtocol = request["Sec-WebSocket-Protocol"],
                               Path = request.Path,
                               ClientIpAddress = clientIp
                           };
            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;
        }
示例#10
0
        public static WebSocketHttpRequest Parse(ArraySegment <byte> bytes, string scheme)
        {
            if (bytes.Count > 4096)
            {
                return(null);
            }

            // Check for websocket request header
            var   body  = Encoding.UTF8.GetString(bytes.Array, bytes.Offset, bytes.Count);
            Match match = _regex.Match(body);

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

            var request = new WebSocketHttpRequest
            {
                Method = match.Groups["method"].Value,
                Path   = match.Groups["path"].Value,
                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);
        }
示例#11
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);
        }
示例#12
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";
        }
示例#13
0
        public static WebSocketConnectionInfo Create(WebSocketHttpRequest request, IPAddress 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,
            };

            return(info);
        }
示例#14
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, IWebSocketConnection connection)
        {
            var version = GetVersion(request);

            switch (version)
            {
            case "7":
            case "8":
            case "13":
                return(new Hybi13Handler(request, connection));
            }

            throw new WebSocketException(WebSocketStatusCodes.ProtocolError);
        }
示例#15
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, Action<string> onMessage, 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);
        }
示例#16
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, Action<string> onMessage, Action onClose)
        {
            var version = GetVersion(request);

            switch (version)
            {
                case "76":
                    return Draft76Handler.Create(request, onMessage);
                case "7":
                    return Hybi13Handler.Create(request, onMessage, onClose);
                case "8":
                    return Hybi13Handler.Create(request, onMessage, onClose);
            }

            throw new WebSocketException("Unsupported Request");
        }
示例#17
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);
        }
示例#18
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";
        }
示例#19
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;
        }
示例#20
0
        // mjb public static WebSocketHttpRequest Parse(byte[] bytes, string scheme)
        public static WebSocketHttpRequest Parse(byte[] bytes, string scheme, AccessPolicyServer AccessPolicyServer, ISocket clientSocket)
        {
            var body = Encoding.UTF8.GetString(bytes);
            Match match = _regex.Match(body);

            // mjb
            //if (!match.Success)
            //    return null;
            if (!match.Success)
            {
                if (body == "<policy-file-request/>\0")
                {
                    if (AccessPolicyServer != null)
                    {
                        AccessPolicyServer.SendResponse(clientSocket.Socket);
                    }
                }

                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;
        }
示例#21
0
        public static WebSocketHttpRequest Parse(byte[] bytes, string scheme)
        {
            // Check for websocket request header
            var body    = Encoding.UTF8.GetString(bytes);
            var extract = ExtractRequest(body);

            if (extract == null)
            {
                // No websocket request header found, check for a flash socket policy request
                var 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 = extract.Method,
                Path   = extract.Path,
                Body   = extract.Body,
                Bytes  = bytes,
                Scheme = scheme
            };

            for (var i = 0; i < extract.Headers.Count; i++)
            {
                request.Headers[extract.Headers[i].Name] = extract.Headers[i].Value;
            }

            return(request);
        }
示例#22
0
        public static IHandler BuildHandler(WebSocketHttpRequest request, Action <string> onMessage, Action onClose)
        {
            var version = GetVersion(request);

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

            case "7":
                return(Hybi13Handler.Create(request, onMessage, onClose));

            case "8":
                return(Hybi13Handler.Create(request, onMessage, onClose));

            case "13":
                return(Hybi13Handler.Create(request, onMessage, onClose));
            }

            throw new WebSocketException("Unsupported Request");
        }
示例#23
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");
        }
示例#24
0
        public void CreateHandler(IEnumerable <byte> data)
        {
            WebSocketHttpRequest request = _parseRequest(data.ToArray());

            if (request == null)
            {
                return;
            }
            Handler = _handlerFactory(request);
            if (Handler == null)
            {
                return;
            }
            var subProtocol = _negotiateSubProtocol(request.SubProtocols);

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

            _initialize(this);

            var handshake = Handler.CreateHandshake(subProtocol);

            SendBytes(handshake, OnOpen);
        }