Пример #1
0
 protected override void OnReceive(WebSocketConnection connection, string message)
 {
     var chars = message.ToCharArray();
     Array.Reverse(chars);
     string s = new string(chars);
     Send(connection, s);
 }
Пример #2
0
        public void DeflateRoundTrip()
        {
            var handler = new TcpHandler();
            var ctx = new NetContext(delegate { }, handler);

            var extn = PerFrameDeflate.Default.CreateExtension("deflate-frame");
            var conn = new WebSocketConnection(new IPEndPoint(IPAddress.Loopback, 20000));

            string[] messages = {
                "This extension uses one reserved bit to indicate whether DEFLATE is applied to the frame or not.  We call this \"COMP\" bit.",
                "Hello",
                "This extension operates only on data frames, and only on the \"Application data\" therein (it does not affect the \"Extension data\" portion of the \"Payload data\").",
                "world", "Hello",
                "To send a frame with DEFLATE applied, an endpoint MUST use the following algorithm.",
                "world",
                "Apply DEFLATE [RFC1951] to all the octets in the \"Application data\" part of the frame.  Multiple blocks MAY be used.  Any type of block MAY be used.  Both block with \"BFINAL\" set to 0 and 1 MAY be used.",
                "If the resulting data does not end with an empty block with no compression (\"BTYPE\" set to 0), append an empty block with no compression to the tail.",
                "Remove 4 octets (that are 0x00 0x00 0xff 0xff) from the tail.",
                "Hello",
                "Build a frame by putting the resulting octets in the \"Application data\" part instead of the original octets.  The payload length field of the frame MUST be the sum of the size of the \"Extension data\" part and these resulting octets.  \"COMP\" bit MUST be set to 1."
            };

            var frames = Array.ConvertAll(messages, CreateFrame);

            int initialSize = frames.Sum(x => x.PayloadLength);
            Assert.IsTrue(frames.All(x => !x.Reserved1), "no COMP initially");
            var munged = frames.SelectMany(f => extn.ApplyOutgoing(ctx, conn, f)).ToArray();
            Assert.AreEqual(frames.Length, munged.Length, "compress: 1 in, 1 out");
            
            Assert.IsTrue(frames.Any(x => x.Reserved1), "some COMP after compress");
            Assert.IsTrue(frames.Any(x => !x.Reserved1), "some non-COMP after compress");

            for (int i = 0; i < munged.Length; i++)
            {
                var ms = new MemoryStream();
                munged[i].Payload.Position = 0;
                munged[i].Payload.CopyTo(ms);
                ms.Position = 0;
                munged[i].Payload = new MemoryStream(ms.ToArray()); // read-only, deliberately
            }
            int mungedSize = frames.Sum(x => x.PayloadLength);

            var unmunged = munged.SelectMany(f => extn.ApplyIncoming(ctx, conn, f)).ToArray();

            Assert.AreEqual(unmunged.Length, unmunged.Length, "inflate: 1 in, 1 out");
            Assert.IsTrue(unmunged.All(x => !x.Reserved1), "no COMP after inflate");

            int unmungedSize = unmunged.Sum(x => x.PayloadLength);

            Console.WriteLine("Uncompressed: {0} bytes; compressed: {1} bytes; inflated {2} bytes", initialSize, mungedSize, unmungedSize);

            string[] finalMessages = Array.ConvertAll(unmunged, ReadFrameMessage);

            Assert.IsTrue(finalMessages.SequenceEqual(messages), "Equal messages");
        }
 protected virtual void OnShutdown(WebSocketConnection connection)
 {
 }
 protected virtual void OnClosed(WebSocketConnection connection)
 {
 }
 protected virtual void OnReceive(WebSocketConnection connection, byte[] message)
 {
 }
 protected virtual void OnFlushed(WebSocketConnection connection)
 {
 }
Пример #7
0
        static string DecompressFrame(IExtension extn, params byte[] data)
        {
            var handler = new TcpHandler();
            var ctx = new NetContext(delegate { }, handler);
            
            if (extn == null) extn = PerFrameDeflate.Default.CreateExtension("deflate-frame");
            var frame = new WebSocketsFrame {
                OpCode = WebSocketsFrame.OpCodes.Text,
                Payload = new MemoryStream(data),
                PayloadLength = data.Length,
                Reserved1 = true
            };
            var connection = new WebSocketConnection(new IPEndPoint(IPAddress.Loopback, 20000));
            var decoded = extn.ApplyIncoming(ctx, connection, frame).Single();

            return ReadFrameMessage(decoded);
        }
Пример #8
0
 protected virtual void OnFlushed(WebSocketConnection connection)
 { }
Пример #9
0
 protected virtual void OnReceive(WebSocketConnection connection, string message)
 { }
 protected virtual void OnAfterAuthenticate(WebSocketConnection connection)
 {
 }
Пример #11
0
 protected virtual void OnAuthenticate(WebSocketConnection connection, StringDictionary claims)
 { }
Пример #12
0
 protected virtual void OnAfterAuthenticate(WebSocketConnection connection)
 { }
Пример #13
0
 protected virtual void OnOpened(WebSocketConnection connection)
 { }
Пример #14
0
 protected virtual void OnClosed(WebSocketConnection connection)
 { }
Пример #15
0
 protected virtual void OnShutdown(WebSocketConnection connection)
 {
     
 }
 protected virtual void OnOpened(WebSocketConnection connection)
 {
 }
Пример #17
0
 protected virtual void OnReceive(WebSocketConnection connection, byte[] message)
 { }
 protected virtual void OnAuthenticate(WebSocketConnection connection, StringDictionary claims)
 {
 }
Пример #19
0
            IEnumerable<WebSocketsFrame> IExtension.ApplyOutgoing(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
            {
                if (!frame.IsControlFrame && frame.PayloadLength > parent.compressMessagesLargerThanBytes)
                {
                    if (frame.Reserved1)
                    {
                        throw new InvalidOperationException("Reserved1 flag is already set; extension conflict?");
                    }

                    int headerBytes = 0;
                    if (outbound == null)
                    {
                        outbound = new ZStream();
                        const int BITS = 12; // 4096 byte outbound buffer (instead of full 32k=15)
                        outbound.deflateInit(zlibConst.Z_BEST_COMPRESSION, BITS);
                        headerBytes = 2;
                    }
                    BufferStream tmp = null;
                    var payload = frame.Payload;
                    payload.Position = 0;
                    byte[] inBuffer = null, outBuffer = null;
                    try
                    {
                        inBuffer = context.GetBuffer();
                        outBuffer = context.GetBuffer();
                        tmp = new BufferStream(context, 0);

                        outbound.next_out = outBuffer;
                        outbound.next_in = inBuffer;

                        int remaining = frame.PayloadLength;
                        while (remaining > 0)
                        {
                            int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
                            if (readCount <= 0) break;
                            remaining -= readCount;

                            outbound.next_in_index = 0;
                            outbound.avail_in = readCount;

                            do
                            {
                                outbound.next_out_index = 0;
                                outbound.avail_out = outBuffer.Length;
                                long priorOut = outbound.total_out;
                                int err = outbound.deflate(remaining == 0 ? zlibConst.Z_SYNC_FLUSH : zlibConst.Z_NO_FLUSH);
                                if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                                    throw new ZStreamException("deflating: " + outbound.msg);

                                int outCount = (int)(outbound.total_out - priorOut);
                                if (outCount > 0)
                                {
                                    if (headerBytes == 0)
                                    {
                                        tmp.Write(outBuffer, 0, outCount);
                                    }
                                    else
                                    {
                                        if (outCount < headerBytes)
                                        {
                                            throw new InvalidOperationException("Failed to write entire header");
                                        }
                                        // check the generated header meets our expectations
                                        // CMF is very specific - CM must be 8, and CINFO must be <=7 (for 32k window)
                                        if ((outBuffer[0] & 15) != 8)
                                        {
                                            throw new InvalidOperationException("Zlib CM header was incorrect");
                                        }
                                        if ((outBuffer[0] & 128) != 0) // if msb set, is > 7 - invalid
                                        {
                                            throw new InvalidOperationException("Zlib CINFO header was incorrect");
                                        }

                                        // FLG is less important; FCHECK is irrelevent, FLEVEL doesn't matter; but
                                        // FDICT must be zero, to ensure that we aren't expecting a an initialization dictionary
                                        if ((outBuffer[1] & 32) != 0)
                                        {
                                            throw new InvalidOperationException("Zlib FLG.FDICT header was set (must not be)");
                                        }

                                        // skip the header, and write anything else
                                        outCount -= headerBytes;
                                        if (outCount > 0)
                                        {
                                            tmp.Write(outBuffer, headerBytes, outCount);
                                        }
                                        headerBytes = 0; // all written now
                                    }
                                }
                            } while (outbound.avail_in > 0 || outbound.avail_out == 0);
                        }
                        if (remaining != 0) throw new EndOfStreamException();
                        if (headerBytes != 0) throw new InvalidOperationException("Zlib header was not written");

                        // verify the last 4 bytes, then drop them
                        tmp.Position = tmp.Length - 4;
                        NetContext.Fill(tmp, outBuffer, 4);
                        if (!(outBuffer[0] == 0x00 && outBuffer[1] == 0x00 && outBuffer[2] == 0xFF && outBuffer[3] == 0xFF))
                        {
                            throw new InvalidOperationException("expectation failed: 0000FFFF in the tail");
                        }

                        if (parent.disableContextTakeover && tmp.Length >= frame.PayloadLength)
                        { // compressing it didn't do anything useful; since we're going to discard
                          // the compression context, we might as well stick with the original data
                            payload.Position = 0;
                            payload = null; // so that it doesn't get disposed
                        }
                        else
                        {
                            // set our final output
                            tmp.Position = 0;
                            tmp.SetLength(tmp.Length - 4);
                            long bytesSaved = frame.PayloadLength - tmp.Length;
                            frame.Payload = tmp;
                            frame.PayloadLength = (int)tmp.Length;
                            frame.Reserved1 = true;
                            tmp = payload as BufferStream;
                            parent.RegisterOutboundBytesSaved(bytesSaved);
                        }
                    }
#if DEBUG
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                        throw;
                    }
#endif
                    finally
                    {
                        if (outbound != null)
                        {
                            outbound.next_out = null;
                            outbound.next_in = null;
                        }
                        if (tmp != null) tmp.Dispose();
                        if (inBuffer != null) context.Recycle(inBuffer);
                        if (outBuffer != null) context.Recycle(outBuffer);
                        if (parent.disableContextTakeover) ClearContext(false, true);
                    }
                }
                yield return frame;
            }
Пример #20
0
        public void CompressFrame()
        {
            var handler = new TcpHandler();
            var ctx = new NetContext(delegate { }, handler);

            IExtensionFactory factory = new PerFrameDeflate(0, false);
            var extn = factory.CreateExtension("deflate-frame");
            var data = Encoding.UTF8.GetBytes("Hello");
            var frame = new WebSocketsFrame
            {
                OpCode = WebSocketsFrame.OpCodes.Text,
                Payload = new MemoryStream(data),
                PayloadLength = data.Length,
                Reserved1 = false
            };
            var connection = new WebSocketConnection(new IPEndPoint(IPAddress.Loopback, 20000));
            var encoded = extn.ApplyOutgoing(ctx, connection, frame).Single();
            var ms = new MemoryStream();
            encoded.Payload.CopyTo(ms);
            string hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);
            Assert.AreEqual("F2-48-CD-C9-C9-07-00", hex);

            // unrelated decoder
            extn = PerFrameDeflate.Default.CreateExtension("deflate-frame");
            var decoded = extn.ApplyIncoming(ctx, connection, frame).Single();

            ms = new MemoryStream();
            decoded.Payload.Position = 0;
            decoded.Payload.CopyTo(ms);
            string s = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
            Assert.AreEqual("Hello", s);
        }
Пример #21
0
            IEnumerable <WebSocketsFrame> IExtension.ApplyIncoming(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
            {
                if (frame.Reserved1 && !frame.IsControlFrame)
                {
                    BufferStream tmp     = null;
                    var          payload = frame.Payload;
                    payload.Position = 0;
                    byte[] inBuffer = null, outBuffer = null;

                    try
                    {
                        outBuffer = context.GetBuffer();
                        inBuffer  = context.GetBuffer();
                        tmp       = new BufferStream(context, 0);

                        if (inbound == null)
                        {
                            inbound = new ZStream();
                            inbound.inflateInit();

                            // fake a zlib header with:
                            // CMF:
                            //   CM = 8 (deflate)
                            //   CINFO = 7 (32k window)
                            // FLG:
                            //   FCHECK: 26 (checksum of other bits)
                            //   FDICT: 0 (no dictionary)
                            //   FLEVEL: 3 (maximum)
                            inBuffer[0]     = 120;
                            inBuffer[1]     = 218;
                            inbound.next_in = inBuffer;
                            int chk = Inflate(tmp, outBuffer, 2);
                            if (chk != 0)
                            {
                                throw new InvalidOperationException("Spoofed zlib header suggested data");
                            }
                        }

                        inbound.next_in = inBuffer;
                        int remaining = frame.PayloadLength;
                        //bool first = true;
                        while (remaining > 0)
                        {
                            int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
                            if (readCount <= 0)
                            {
                                break;
                            }
                            remaining -= readCount;

                            //if (first)
                            //{   // kill the BFINAL flag from the first block, if set; we don't want zlib
                            //    // trying to verify the ADLER checksum; unfortunately, a frame can contain
                            //    // multiple blocks, and a *later* block could have BFINAL set. That sucks.
                            //    inBuffer[0] &= 254;
                            //    first = false;
                            //}
                            Inflate(tmp, outBuffer, readCount);
                        }
                        if (remaining != 0)
                        {
                            throw new EndOfStreamException();
                        }

                        // spoof the missing 4 bytes from the tail
                        inBuffer[0] = inBuffer[1] = 0x00;
                        inBuffer[2] = inBuffer[3] = 0xFF;
                        Inflate(tmp, outBuffer, 4);

                        // set our final output
                        tmp.Position  = 0;
                        frame.Payload = tmp;
                        long bytesSaved = tmp.Length - frame.PayloadLength;
                        frame.PayloadLength = (int)tmp.Length;
                        frame.Reserved1     = false;
                        tmp = payload as BufferStream;
                        parent.RegisterInboundBytesSaved(bytesSaved);
                    }
#if DEBUG
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                        throw;
                    }
#endif
                    finally
                    {
                        if (inbound != null)
                        {
                            inbound.next_out = null;
                            inbound.next_in  = null;
                        }
                        if (tmp != null)
                        {
                            tmp.Dispose();
                        }
                        if (inBuffer != null)
                        {
                            context.Recycle(inBuffer);
                        }
                        if (outBuffer != null)
                        {
                            context.Recycle(outBuffer);
                        }
                        if (parent.disableContextTakeover)
                        {
                            ClearContext(true, false);
                        }
                    }
                }
                yield return(frame);
            }
 protected virtual void OnReceive(WebSocketConnection connection, string message)
 {
 }
Пример #23
0
 public void Send(WebSocketConnection connection, byte[] message)
 {
     if (message == null) throw new ArgumentNullException("message");
     connection.Send(Context, message);
 }
Пример #24
0
            IEnumerable<WebSocketsFrame> IExtension.ApplyIncoming(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
            {
                if (frame.Reserved1 && !frame.IsControlFrame)
                {
                    BufferStream tmp = null;
                    var payload = frame.Payload;
                    payload.Position = 0;
                    byte[] inBuffer = null, outBuffer = null;

                    try
                    {
                        outBuffer = context.GetBuffer();
                        inBuffer = context.GetBuffer();
                        tmp = new BufferStream(context, 0);

                        if (inbound == null)
                        {
                            inbound = new ZStream();
                            inbound.inflateInit();

                            // fake a zlib header with:
                            // CMF:
                            //   CM = 8 (deflate)
                            //   CINFO = 7 (32k window)
                            // FLG:
                            //   FCHECK: 26 (checksum of other bits)
                            //   FDICT: 0 (no dictionary)
                            //   FLEVEL: 3 (maximum)
                            inBuffer[0] = 120;
                            inBuffer[1] = 218;
                            inbound.next_in = inBuffer;
                            int chk = Inflate(tmp, outBuffer, 2);
                            if (chk != 0) throw new InvalidOperationException("Spoofed zlib header suggested data");
                        }   
                        
                        inbound.next_in = inBuffer;
                        int remaining = frame.PayloadLength;
                        //bool first = true;
                        while (remaining > 0)
                        {
                            int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
                            if (readCount <= 0) break;
                            remaining -= readCount;

                            //if (first)
                            //{   // kill the BFINAL flag from the first block, if set; we don't want zlib
                            //    // trying to verify the ADLER checksum; unfortunately, a frame can contain
                            //    // multiple blocks, and a *later* block could have BFINAL set. That sucks.
                            //    inBuffer[0] &= 254;
                            //    first = false;
                            //}
                            Inflate(tmp, outBuffer, readCount);                          
                        }
                        if (remaining != 0) throw new EndOfStreamException();

                        // spoof the missing 4 bytes from the tail
                        inBuffer[0] = inBuffer[1] = 0x00;
                        inBuffer[2] = inBuffer[3] = 0xFF;
                        Inflate(tmp, outBuffer, 4);

                        // set our final output
                        tmp.Position = 0;
                        frame.Payload = tmp;
                        long bytesSaved = tmp.Length - frame.PayloadLength;
                        frame.PayloadLength = (int)tmp.Length;
                        frame.Reserved1 = false;
                        tmp = payload as BufferStream;
                        parent.RegisterInboundBytesSaved(bytesSaved);
                    }
#if DEBUG
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                        throw;
                    }
#endif
                    finally
                    {
                        if (inbound != null)
                        {
                            inbound.next_out = null;
                            inbound.next_in = null;
                        }
                        if (tmp != null) tmp.Dispose();
                        if (inBuffer != null) context.Recycle(inBuffer);
                        if (outBuffer != null) context.Recycle(outBuffer);
                        if (parent.disableContextTakeover) ClearContext(true, false);
                    }

                }
                yield return frame;
            }
Пример #25
0
            IEnumerable <WebSocketsFrame> IExtension.ApplyOutgoing(NetContext context, WebSocketConnection connection, WebSocketsFrame frame)
            {
                if (!frame.IsControlFrame && frame.PayloadLength > parent.compressMessagesLargerThanBytes)
                {
                    if (frame.Reserved1)
                    {
                        throw new InvalidOperationException("Reserved1 flag is already set; extension conflict?");
                    }

                    int headerBytes = 0;
                    if (outbound == null)
                    {
                        outbound = new ZStream();
                        const int BITS = 12; // 4096 byte outbound buffer (instead of full 32k=15)
                        outbound.deflateInit(zlibConst.Z_BEST_COMPRESSION, BITS);
                        headerBytes = 2;
                    }
                    BufferStream tmp     = null;
                    var          payload = frame.Payload;
                    payload.Position = 0;
                    byte[] inBuffer = null, outBuffer = null;
                    try
                    {
                        inBuffer  = context.GetBuffer();
                        outBuffer = context.GetBuffer();
                        tmp       = new BufferStream(context, 0);

                        outbound.next_out = outBuffer;
                        outbound.next_in  = inBuffer;

                        int remaining = frame.PayloadLength;
                        while (remaining > 0)
                        {
                            int readCount = payload.Read(inBuffer, 0, inBuffer.Length);
                            if (readCount <= 0)
                            {
                                break;
                            }
                            remaining -= readCount;

                            outbound.next_in_index = 0;
                            outbound.avail_in      = readCount;

                            do
                            {
                                outbound.next_out_index = 0;
                                outbound.avail_out      = outBuffer.Length;
                                long priorOut = outbound.total_out;
                                int  err      = outbound.deflate(remaining == 0 ? zlibConst.Z_SYNC_FLUSH : zlibConst.Z_NO_FLUSH);
                                if (err != zlibConst.Z_OK && err != zlibConst.Z_STREAM_END)
                                {
                                    throw new ZStreamException("deflating: " + outbound.msg);
                                }

                                int outCount = (int)(outbound.total_out - priorOut);
                                if (outCount > 0)
                                {
                                    if (headerBytes == 0)
                                    {
                                        tmp.Write(outBuffer, 0, outCount);
                                    }
                                    else
                                    {
                                        if (outCount < headerBytes)
                                        {
                                            throw new InvalidOperationException("Failed to write entire header");
                                        }
                                        // check the generated header meets our expectations
                                        // CMF is very specific - CM must be 8, and CINFO must be <=7 (for 32k window)
                                        if ((outBuffer[0] & 15) != 8)
                                        {
                                            throw new InvalidOperationException("Zlib CM header was incorrect");
                                        }
                                        if ((outBuffer[0] & 128) != 0) // if msb set, is > 7 - invalid
                                        {
                                            throw new InvalidOperationException("Zlib CINFO header was incorrect");
                                        }

                                        // FLG is less important; FCHECK is irrelevent, FLEVEL doesn't matter; but
                                        // FDICT must be zero, to ensure that we aren't expecting a an initialization dictionary
                                        if ((outBuffer[1] & 32) != 0)
                                        {
                                            throw new InvalidOperationException("Zlib FLG.FDICT header was set (must not be)");
                                        }

                                        // skip the header, and write anything else
                                        outCount -= headerBytes;
                                        if (outCount > 0)
                                        {
                                            tmp.Write(outBuffer, headerBytes, outCount);
                                        }
                                        headerBytes = 0; // all written now
                                    }
                                }
                            } while (outbound.avail_in > 0 || outbound.avail_out == 0);
                        }
                        if (remaining != 0)
                        {
                            throw new EndOfStreamException();
                        }
                        if (headerBytes != 0)
                        {
                            throw new InvalidOperationException("Zlib header was not written");
                        }

                        // verify the last 4 bytes, then drop them
                        tmp.Position = tmp.Length - 4;
                        NetContext.Fill(tmp, outBuffer, 4);
                        if (!(outBuffer[0] == 0x00 && outBuffer[1] == 0x00 && outBuffer[2] == 0xFF && outBuffer[3] == 0xFF))
                        {
                            throw new InvalidOperationException("expectation failed: 0000FFFF in the tail");
                        }

                        if (parent.disableContextTakeover && tmp.Length >= frame.PayloadLength)
                        { // compressing it didn't do anything useful; since we're going to discard
                          // the compression context, we might as well stick with the original data
                            payload.Position = 0;
                            payload          = null; // so that it doesn't get disposed
                        }
                        else
                        {
                            // set our final output
                            tmp.Position = 0;
                            tmp.SetLength(tmp.Length - 4);
                            long bytesSaved = frame.PayloadLength - tmp.Length;
                            frame.Payload       = tmp;
                            frame.PayloadLength = (int)tmp.Length;
                            frame.Reserved1     = true;
                            tmp = payload as BufferStream;
                            parent.RegisterOutboundBytesSaved(bytesSaved);
                        }
                    }
#if DEBUG
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                        throw;
                    }
#endif
                    finally
                    {
                        if (outbound != null)
                        {
                            outbound.next_out = null;
                            outbound.next_in  = null;
                        }
                        if (tmp != null)
                        {
                            tmp.Dispose();
                        }
                        if (inBuffer != null)
                        {
                            context.Recycle(inBuffer);
                        }
                        if (outBuffer != null)
                        {
                            context.Recycle(outBuffer);
                        }
                        if (parent.disableContextTakeover)
                        {
                            ClearContext(false, true);
                        }
                    }
                }
                yield return(frame);
            }