public static byte[] Handshake(WebSocketHttpRequest request, string subProtocol)
        {
            Logger.Debug("Building Draft76 Response");

            var builder = new StringBuilder();

            builder.Append("HTTP/1.1 101 WebSocket Protocol Handshake\r\n");
            builder.Append("Upgrade: WebSocket\r\n");
            builder.Append("Connection: Upgrade\r\n");
            builder.AppendFormat("Sec-WebSocket-Origin: {0}\r\n", request["Origin"]);
            builder.AppendFormat("Sec-WebSocket-Location: {0}://{1}{2}\r\n", request.Scheme, request["Host"], request.Path);

            if (subProtocol != null)
            {
                builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", subProtocol);
            }

            builder.Append("\r\n");

            var key1      = request["Sec-WebSocket-Key1"];
            var key2      = request["Sec-WebSocket-Key2"];
            var challenge = new ArraySegment <byte>(request.Bytes, request.Bytes.Length - 8, 8);

            var answerBytes = CalculateAnswerBytes(key1, key2, challenge);

            byte[] byteResponse       = Encoding.ASCII.GetBytes(builder.ToString());
            int    byteResponseLength = byteResponse.Length;

            Array.Resize(ref byteResponse, byteResponseLength + answerBytes.Length);
            Array.Copy(answerBytes, 0, byteResponse, byteResponseLength, answerBytes.Length);

            return(byteResponse);
        }
        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);
        }
Example #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 IWebSocketHandler Create(WebSocketHttpRequest request, Action <string> onMessage)
 {
     return(new ComposableHandler
     {
         TextFrame = Draft76Handler.FrameText,
         Handshake = sub => Draft76Handler.Handshake(request, sub),
         ReceiveData = data => ReceiveData(onMessage, data)
     });
 }
Example #5
0
        public static IWebSocketHandler Create(WebSocketHttpRequest request, Action <string> onMessage, Action onClose, Action <byte[]> onBinary)
        {
            var readState = new ReadState();

            return(new ComposableHandler
            {
                Handshake = sub => Hybi13Handler.BuildHandshake(request, sub),
                TextFrame = s => Hybi13Handler.FrameData(Encoding.UTF8.GetBytes(s), FrameType.Text),
                BinaryFrame = s => Hybi13Handler.FrameData(s, FrameType.Binary),
                CloseFrame = i => Hybi13Handler.FrameData(i.ToBigEndianBytes <ushort>(), FrameType.Close),
                ReceiveData = d => Hybi13Handler.ReceiveData(d, readState, (op, data) => Hybi13Handler.ProcessFrame(op, data, onMessage, onClose, onBinary))
            });
        }
        public static IWebSocketHandler 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);
        }
        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");
        }
Example #8
0
        public static byte[] BuildHandshake(WebSocketHttpRequest request, string subProtocol)
        {
            Logger.Debug("Building Hybi-14 Response");

            var builder = new StringBuilder();

            builder.Append("HTTP/1.1 101 Switching Protocols\r\n");
            builder.Append("Upgrade: websocket\r\n");
            builder.Append("Connection: Upgrade\r\n");
            if (subProtocol != null)
            {
                builder.AppendFormat("Sec-WebSocket-Protocol: {0}\r\n", subProtocol);
            }

            var responseKey = CreateResponseKey(request["Sec-WebSocket-Key"]);

            builder.AppendFormat("Sec-WebSocket-Accept: {0}\r\n", responseKey);
            builder.Append("\r\n");

            return(Encoding.ASCII.GetBytes(builder.ToString()));
        }