protected internal override void CompleteHandshake(NetContext context, Connection connection, System.IO.Stream input, string requestLine, System.Collections.Specialized.StringDictionary headers, byte[] body)
            {
                keys = Tuple.Create(headers["Sec-WebSocket-Key1"], headers["Sec-WebSocket-Key2"]);
                var builder = new StringBuilder(
                    "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
                    + "Upgrade: WebSocket\r\n"
                    + "Connection: Upgrade\r\n");
                var conn = (WebSocketConnection)connection;

                if (!string.IsNullOrEmpty(conn.Host))
                { // we should add the origin/location, but that depends on what we have available to us
                    builder.Append("Sec-WebSocket-Origin: ").Append(conn.Origin).Append("\r\n");
                    if (!string.IsNullOrEmpty(conn.RequestLine))
                    {
                        Match match;
                        if ((match = Regex.Match(conn.RequestLine, @"GET ([^\s]+) HTTP")).Success)
                        {
                            builder.Append("Sec-WebSocket-Location: ").Append("ws://").Append(conn.Host).Append(match.Groups[1].Value).Append("\r\n");
                        }
                    }
                }
                if (!string.IsNullOrEmpty(conn.Protocol))
                {
                    builder.Append("Sec-WebSocket-Protocol: ").Append(conn.Protocol).Append("\r\n");
                }
                builder.Append("\r\n");
                SendControl(context, new StringFrame(builder.ToString(), Encoding.ASCII, false));
                connection.PromptToSend(context);
            }
 protected override void GracefulShutdown(NetContext context, Connection connection)
 {
     connection.DisableRead();
     var ws = (WebSocketConnection) connection;
     if(ws.HasSentClose)
     {
         ws.HasSentClose = true;
         SendData(context, new BinaryFrame(new byte[] {0xFF, 0x00}, true));
     }
     SendShutdown(context);
     connection.PromptToSend(context);
 }
        protected override void GracefulShutdown(NetContext context, Connection connection)
        {
            connection.DisableRead();
            var ws = (WebSocketConnection)connection;

            if (ws.HasSentClose)
            {
                ws.HasSentClose = true;
                SendData(context, new BinaryFrame(new byte[] { 0xFF, 0x00 }, true));
            }
            SendShutdown(context);
            connection.PromptToSend(context);
        }
Example #4
0
        protected internal override void CompleteHandshake(NetContext context, Connection connection, Stream input, string requestLine, StringDictionary headers, byte[] body)
        {
            string key = headers["Sec-WebSocket-Key"];

            if (key != null)
            {
                key = key.Trim();
            }
            string response = ComputeReply(context, key);

            var builder = new StringBuilder(
                "HTTP/1.1 101 Switching Protocols\r\n"
                + "Upgrade: websocket\r\n"
                + "Connection: Upgrade\r\n"
                + "Sec-WebSocket-Accept: ").Append(response).Append("\r\n");

            var wc   = connection as WebSocketConnection;
            var extn = wc == null ? null : wc.Extensions;

            if (extn != null)
            {
                List <string> extnHeaders = null;
                for (int i = 0; i < extn.Length; i++)
                {
                    string extnHeader = extn[i].GetResponseHeader();
                    if (!string.IsNullOrEmpty(extnHeader))
                    {
                        if (extnHeaders == null)
                        {
                            extnHeaders = new List <string>();
                        }
                        extnHeaders.Add(extnHeader);
                    }
                }
                if (extnHeaders != null)
                {
                    builder.Append("Sec-WebSocket-Extensions: ").Append(extnHeaders[0]);
                    for (int i = 1; i < extnHeaders.Count; i++)
                    {
                        builder.Append(", ").Append(extnHeaders[i]);
                    }
                    builder.Append("\r\n");
                }
            }

            builder.Append("\r\n");
            SendControl(context, new StringFrame(builder.ToString(), Encoding.ASCII));
            connection.PromptToSend(context);
        }
            protected override int ProcessIncoming(NetContext context, Connection connection, Stream incoming)
            {
                if (keys != null) // handshake is not yet complete
                {
                    if (incoming.Length < 8)
                    {
                        return(0);                     // need 8 bytes to finish the handshake
                    }
                    var key3 = new byte[8];
                    NetContext.Fill(incoming, key3, 8);

                    // now that we're on a different protocol, send the server's response to the client's challenge
                    var securityCheck = ComputeKey(keys.Item1, keys.Item2, key3);
                    keys = null; // wipe
                    SendControl(context, new BinaryFrame(securityCheck));
                    connection.PromptToSend(context);
                    return(8);
                }
                return(base.ProcessIncoming(context, connection, incoming));
            }
Example #6
0
        protected override void GracefulShutdown(NetContext context, Connection connection)
        {
            connection.DisableRead();
            var ws = (WebSocketConnection)connection;

            if (!ws.HasSentClose)
            {
                ws.HasSentClose = true;
                var close = new WebSocketsFrame();
                if (IsClient)
                {
                    close.Mask = CreateMask();
                }
                close.OpCode        = WebSocketsFrame.OpCodes.Close;
                close.PayloadLength = 0;
                SendControl(context, close);
            }
            SendShutdown(context);
            connection.PromptToSend(context);
        }
        protected internal override void CompleteHandshake(NetContext context, Connection connection, Stream input, string requestLine, StringDictionary headers, byte[] body)
        {


            string key = headers["Sec-WebSocket-Key"];
            if(key != null) key = key.Trim();
            string response = ComputeReply(context, key);

            var builder = new StringBuilder(
                              "HTTP/1.1 101 Switching Protocols\r\n"
                            + "Upgrade: websocket\r\n"
                            + "Connection: Upgrade\r\n"
                            + "Sec-WebSocket-Accept: ").Append(response).Append("\r\n");

            var wc = connection as WebSocketConnection;
            var extn = wc == null ? null : wc.Extensions;
            if (extn != null)
            {
                List<string> extnHeaders = null;
                for (int i = 0; i < extn.Length; i++)
                {
                    string extnHeader = extn[i].GetResponseHeader();
                    if (!string.IsNullOrEmpty(extnHeader))
                    {
                        if (extnHeaders == null) extnHeaders = new List<string>();
                        extnHeaders.Add(extnHeader);
                    }
                }
                if (extnHeaders != null)
                {
                    builder.Append("Sec-WebSocket-Extensions: ").Append(extnHeaders[0]);
                    for (int i = 1; i < extnHeaders.Count; i++)
                        builder.Append(", ").Append(extnHeaders[i]);
                    builder.Append("\r\n");
                }
            }

            builder.Append("\r\n");
            SendControl(context, new StringFrame(builder.ToString(), Encoding.ASCII));
            connection.PromptToSend(context);
        }
        private void ProcessFrame(NetContext context, Connection connection, WebSocketsFrame frame)
        {
            if(IsClient)
            {
                if(frame.Mask.HasValue) throw new InvalidOperationException("Clients only expect unmasked frames");
            } else
            {
                if (!frame.Mask.HasValue) throw new InvalidOperationException("Servers only expect masked frames");
            }
            var ws = (WebSocketConnection) connection;
            if(frame.IsControlFrame)
            {
                if(!frame.IsFinal) throw new InvalidOperationException("control frames cannot be fragmented");
                switch(frame.OpCode)
                {
                    case WebSocketsFrame.OpCodes.Close:
                        // respond with a closing handshake
                        GracefulShutdown(context, connection);
                        break;
                    case WebSocketsFrame.OpCodes.Pong:
                        // could add some reaction here
                        break;
                    case WebSocketsFrame.OpCodes.Ping:
                        // send a "pong" with the same payload
                        var pong = new WebSocketsFrame();
                        if (IsClient) pong.Mask = CreateMask();
                        pong.OpCode = WebSocketsFrame.OpCodes.Binary;
                        pong.PayloadLength = frame.PayloadLength;
                        if(frame.Payload != null)
                        {
                            var ms = new MemoryStream((int)frame.Payload.Length);
                            byte[] buffer = null;
                            try
                            {
                                buffer = context.GetBuffer();
                                Copy(frame.Payload, ms, buffer, null, frame.PayloadLength);
                            } finally
                            {
                                context.Recycle(buffer);
                            }
                            pong.Payload = ms;
                        }
                        SendControl(context, pong);
                        connection.PromptToSend(context);
                        break;
                    default:
                        throw new InvalidOperationException();
                }
            }
            else
            {
                if (!ws.AllowIncomingDataFrames) throw new InvalidOperationException("Data frames disabled");

                int pendingCount = FrameCount;
                if (frame.OpCode == WebSocketsFrame.OpCodes.Continuation)
                {
                    if(pendingCount == 0) throw new InvalidOperationException("Continuation of nothing");
                }
                else  if (pendingCount != 0)
                {
                    throw new InvalidOperationException("Should be a continuation");
                }
                if (!frame.IsFinal)
                {
                    Push(context, frame);
                }
                else
                {
                    if(pendingCount == 0)
                    {
                        Process(context, connection, frame.Payload, frame.OpCode);
                    } else
                    {
                        throw new NotImplementedException();
                    }
                }

            }
            if (frame.OpCode == WebSocketsFrame.OpCodes.Close)
            {
                Debug.WriteLine("Sent close frame: " + connection);
            }
        }
 protected override void GracefulShutdown(NetContext context, Connection connection)
 {
     connection.DisableRead();
     var ws = (WebSocketConnection) connection;
     if(!ws.HasSentClose)
     {
         ws.HasSentClose = true;
         var close = new WebSocketsFrame();
         if (IsClient) close.Mask = CreateMask();
         close.OpCode = WebSocketsFrame.OpCodes.Close;
         close.PayloadLength = 0;
         SendControl(context, close);
     }
     SendShutdown(context);
     connection.PromptToSend(context);
 }
 protected internal override void CompleteHandshake(NetContext context, Connection connection, System.IO.Stream input, string requestLine, System.Collections.Specialized.StringDictionary headers, byte[] body)
 {
     keys = Tuple.Create(headers["Sec-WebSocket-Key1"], headers["Sec-WebSocket-Key2"]);
     var builder = new StringBuilder(
         "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
         + "Upgrade: WebSocket\r\n"
         + "Connection: Upgrade\r\n");
     var conn = (WebSocketConnection)connection;
     if (!string.IsNullOrEmpty(conn.Host))
     { // we should add the origin/location, but that depends on what we have available to us
         builder.Append("Sec-WebSocket-Origin: ").Append(conn.Origin).Append("\r\n");
         if (!string.IsNullOrEmpty(conn.RequestLine))
         {
             Match match;
             if ((match = Regex.Match(conn.RequestLine, @"GET ([^\s]+) HTTP")).Success)
             {
                 builder.Append("Sec-WebSocket-Location: ").Append("ws://").Append(conn.Host).Append(match.Groups[1].Value).Append("\r\n");
             }
         }
     }
     if (!string.IsNullOrEmpty(conn.Protocol))
         builder.Append("Sec-WebSocket-Protocol: ").Append(conn.Protocol).Append("\r\n");
     builder.Append("\r\n");
     SendControl(context, new StringFrame(builder.ToString(), Encoding.ASCII, false));
     connection.PromptToSend(context);
 }
            protected override int ProcessIncoming(NetContext context, Connection connection, Stream incoming)
            {
                if (keys != null) // handshake is not yet complete
                {
                    if (incoming.Length < 8) return 0; // need 8 bytes to finish the handshake

                    var key3 = new byte[8];
                    NetContext.Fill(incoming, key3, 8);

                    // now that we're on a different protocol, send the server's response to the client's challenge
                    var securityCheck = ComputeKey(keys.Item1, keys.Item2, key3);
                    keys = null; // wipe
                    SendControl(context, new BinaryFrame(securityCheck));
                    connection.PromptToSend(context);
                    return 8;
                }
                return base.ProcessIncoming(context, connection, incoming);
            }
Example #12
0
        private void ProcessFrame(NetContext context, Connection connection, WebSocketsFrame frame)
        {
            if (IsClient)
            {
                if (frame.Mask.HasValue)
                {
                    throw new InvalidOperationException("Clients only expect unmasked frames");
                }
            }
            else
            {
                if (!frame.Mask.HasValue)
                {
                    throw new InvalidOperationException("Servers only expect masked frames");
                }
            }
            var ws = (WebSocketConnection)connection;

            if (frame.IsControlFrame)
            {
                if (!frame.IsFinal)
                {
                    throw new InvalidOperationException("control frames cannot be fragmented");
                }
                switch (frame.OpCode)
                {
                case WebSocketsFrame.OpCodes.Close:
                    // respond with a closing handshake
                    GracefulShutdown(context, connection);
                    break;

                case WebSocketsFrame.OpCodes.Pong:
                    // could add some reaction here
                    break;

                case WebSocketsFrame.OpCodes.Ping:
                    // send a "pong" with the same payload
                    var pong = new WebSocketsFrame();
                    if (IsClient)
                    {
                        pong.Mask = CreateMask();
                    }
                    pong.OpCode        = WebSocketsFrame.OpCodes.Binary;
                    pong.PayloadLength = frame.PayloadLength;
                    if (frame.Payload != null)
                    {
                        var    ms     = new MemoryStream((int)frame.Payload.Length);
                        byte[] buffer = null;
                        try
                        {
                            buffer = context.GetBuffer();
                            Copy(frame.Payload, ms, buffer, null, frame.PayloadLength);
                        } finally
                        {
                            context.Recycle(buffer);
                        }
                        pong.Payload = ms;
                    }
                    SendControl(context, pong);
                    connection.PromptToSend(context);
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }
            else
            {
                if (!ws.AllowIncomingDataFrames)
                {
                    throw new InvalidOperationException("Data frames disabled");
                }

                int pendingCount = FrameCount;
                if (frame.OpCode == WebSocketsFrame.OpCodes.Continuation)
                {
                    if (pendingCount == 0)
                    {
                        throw new InvalidOperationException("Continuation of nothing");
                    }
                }
                else if (pendingCount != 0)
                {
                    throw new InvalidOperationException("Should be a continuation");
                }
                if (!frame.IsFinal)
                {
                    Push(context, frame);
                }
                else
                {
                    if (pendingCount == 0)
                    {
                        Process(context, connection, frame.Payload, frame.OpCode);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
            if (frame.OpCode == WebSocketsFrame.OpCodes.Close)
            {
                Debug.WriteLine("Sent close frame: " + connection);
            }
        }