public void SetContentFromFile() { TestHttpData test = new TestHttpData("test", Encoding.UTF8, 0); try { byte[] bytes = new byte[4096]; (new Random()).NextBytes(bytes); using (var fs = File.Create(Path.GetTempFileName(), 4096, FileOptions.DeleteOnClose)) { fs.Write(bytes, 0, bytes.Length); fs.Flush(); fs.Position = 0; test.SetContent(fs); } var buf = test.GetByteBuffer(); Assert.Equal(0, buf.ReaderIndex); Assert.Equal(buf.WriterIndex, bytes.Length); Assert.True(bytes.AsSpan().SequenceEqual(test.GetBytes())); Assert.True(bytes.AsSpan().SequenceEqual(ByteBufferUtil.GetBytes(buf))); } finally { //release the ByteBuf test.Delete(); } }
private static void TestAddContentFromByteBuf0(bool composite) { DiskFileUpload f1 = new DiskFileUpload("file3", "file3", "application/json", null, null, 0); try { byte[] bytes = new byte[4096]; (new SafeRandom()).NextBytes(bytes); IByteBuffer buffer; if (composite) { buffer = Unpooled.CompositeBuffer() .AddComponent(true, Unpooled.WrappedBuffer(bytes, 0, bytes.Length / 2)) .AddComponent(true, Unpooled.WrappedBuffer(bytes, bytes.Length / 2, bytes.Length / 2)); } else { buffer = Unpooled.WrappedBuffer(bytes); } f1.AddContent(buffer, true); IByteBuffer buf = f1.GetByteBuffer(); Assert.Equal(0, buf.ReaderIndex); Assert.Equal(buf.WriterIndex, bytes.Length); Assert.True(ByteBufferUtil.GetBytes(buf).SequenceEqual(bytes)); } finally { //release the ByteBuf f1.Delete(); } }
public void Base64() { string base64 = WebSocketUtil.Base64String(EmptyArrays.EmptyBytes); Assert.NotNull(base64); Assert.Equal(0, base64.Length); base64 = WebSocketUtil.Base64String(Encoding.UTF8.GetBytes("foo")); Assert.Equal("Zm9v", base64); base64 = WebSocketUtil.Base64String(Encoding.UTF8.GetBytes("bar")); IByteBuffer src = Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(base64)); try { IByteBuffer dst = DotNetty.Codecs.Base64.Base64.Decode(src); try { Assert.Equal("bar", Encoding.UTF8.GetString(ByteBufferUtil.GetBytes(dst))); } finally { dst.Release(); } } finally { src.Release(); } }
public void IllegalStateWhenDecompressionInProgress() { var selectivityDecompressionFilter = new SelectivityDecompressionFilter1(); EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.NewZlibEncoder(ZlibWrapper.None, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel( new PerMessageDeflateDecoder(false, selectivityDecompressionFilter)); byte[] firstPayload = new byte[200]; _random.NextBytes(firstPayload); byte[] finalPayload = new byte[50]; _random.NextBytes(finalPayload); Assert.True(encoderChannel.WriteOutbound(Unpooled.WrappedBuffer(firstPayload))); Assert.True(encoderChannel.WriteOutbound(Unpooled.WrappedBuffer(finalPayload))); var compressedFirstPayload = encoderChannel.ReadOutbound <IByteBuffer>(); var compressedFinalPayload = encoderChannel.ReadOutbound <IByteBuffer>(); Assert.True(encoderChannel.FinishAndReleaseAll()); BinaryWebSocketFrame firstPart = new BinaryWebSocketFrame(false, WebSocketRsv.Rsv1, compressedFirstPayload); ContinuationWebSocketFrame finalPart = new ContinuationWebSocketFrame(true, WebSocketRsv.Rsv1, compressedFinalPayload); Assert.True(decoderChannel.WriteInbound(firstPart)); var outboundFirstPart = decoderChannel.ReadInbound <BinaryWebSocketFrame>(); //first part is decompressed Assert.Equal(0, outboundFirstPart.Rsv); Assert.Equal(firstPayload, ByteBufferUtil.GetBytes(outboundFirstPart.Content)); Assert.True(outboundFirstPart.Release()); //final part throwing exception try { decoderChannel.WriteInbound(finalPart); Assert.False(true); } catch (Exception exc) { Assert.IsType <DecoderException>(exc); } finally { Assert.True(finalPart.Release()); Assert.False(encoderChannel.FinishAndReleaseAll()); } }
public void CompressionSkipForBinaryFrame() { EmbeddedChannel encoderChannel = new EmbeddedChannel(new PerMessageDeflateEncoder(9, 15, false, AlwaysSkipWebSocketExtensionFilter.Instance)); byte[] payload = new byte[300]; _random.NextBytes(payload); WebSocketFrame binaryFrame = new BinaryWebSocketFrame(Unpooled.WrappedBuffer(payload)); Assert.True(encoderChannel.WriteOutbound(binaryFrame.Copy())); var outboundFrame = encoderChannel.ReadOutbound <WebSocketFrame>(); Assert.Equal(0, outboundFrame.Rsv); Assert.Equal(payload, ByteBufferUtil.GetBytes(outboundFrame.Content)); Assert.True(outboundFrame.Release()); Assert.False(encoderChannel.Finish()); }
public void IllegalStateWhenCompressionInProgress() { var selectivityCompressionFilter = new SelectivityDecompressionFilter1(); EmbeddedChannel encoderChannel = new EmbeddedChannel( new PerMessageDeflateEncoder(9, 15, false, selectivityCompressionFilter)); byte[] firstPayload = new byte[200]; _random.NextBytes(firstPayload); byte[] finalPayload = new byte[90]; _random.NextBytes(finalPayload); BinaryWebSocketFrame firstPart = new BinaryWebSocketFrame(false, 0, Unpooled.WrappedBuffer(firstPayload)); ContinuationWebSocketFrame finalPart = new ContinuationWebSocketFrame(true, 0, Unpooled.WrappedBuffer(finalPayload)); Assert.True(encoderChannel.WriteOutbound(firstPart)); var outboundFirstPart = encoderChannel.ReadOutbound <BinaryWebSocketFrame>(); //first part is compressed Assert.Equal(WebSocketRsv.Rsv1, outboundFirstPart.Rsv); Assert.NotEqual(firstPayload, ByteBufferUtil.GetBytes(outboundFirstPart.Content)); Assert.True(outboundFirstPart.Release()); //final part throwing exception try { encoderChannel.WriteOutbound(finalPart); Assert.False(true); } catch (Exception exc) { var ae = exc as AggregateException; Assert.NotNull(ae); Assert.IsType <EncoderException>(ae.InnerException); } finally { Assert.True(finalPart.Release()); Assert.False(encoderChannel.FinishAndReleaseAll()); } }
public void SelectivityCompressionSkip() { var selectivityCompressionFilter = new SelectivityDecompressionFilter0(); EmbeddedChannel encoderChannel = new EmbeddedChannel( new PerMessageDeflateEncoder(9, 15, false, selectivityCompressionFilter)); EmbeddedChannel decoderChannel = new EmbeddedChannel( ZlibCodecFactory.NewZlibDecoder(ZlibWrapper.None)); string textPayload = "not compressed payload"; byte[] binaryPayload = new byte[101]; _random.NextBytes(binaryPayload); WebSocketFrame textFrame = new TextWebSocketFrame(textPayload); BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame(Unpooled.WrappedBuffer(binaryPayload)); Assert.True(encoderChannel.WriteOutbound(textFrame)); Assert.True(encoderChannel.WriteOutbound(binaryFrame)); var outboundTextFrame = encoderChannel.ReadOutbound <WebSocketFrame>(); //compression skipped for textFrame Assert.Equal(0, outboundTextFrame.Rsv); Assert.Equal(textPayload, outboundTextFrame.Content.ToString(Encoding.UTF8)); Assert.True(outboundTextFrame.Release()); var outboundBinaryFrame = encoderChannel.ReadOutbound <WebSocketFrame>(); //compression not skipped for binaryFrame Assert.Equal(WebSocketRsv.Rsv1, outboundBinaryFrame.Rsv); Assert.True(decoderChannel.WriteInbound(outboundBinaryFrame.Content.Retain())); var uncompressedBinaryPayload = decoderChannel.ReadInbound <IByteBuffer>(); Assert.Equal(binaryPayload, ByteBufferUtil.GetBytes(uncompressedBinaryPayload)); Assert.True(outboundBinaryFrame.Release()); Assert.True(uncompressedBinaryPayload.Release()); Assert.False(encoderChannel.Finish()); Assert.False(decoderChannel.Finish()); }
public void SelectivityDecompressionSkip() { var selectivityDecompressionFilter = new SelectivityDecompressionFilter0(); EmbeddedChannel encoderChannel = new EmbeddedChannel( ZlibCodecFactory.NewZlibEncoder(ZlibWrapper.None, 9, 15, 8)); EmbeddedChannel decoderChannel = new EmbeddedChannel( new PerMessageDeflateDecoder(false, selectivityDecompressionFilter)); string textPayload = "compressed payload"; byte[] binaryPayload = new byte[300]; _random.NextBytes(binaryPayload); Assert.True(encoderChannel.WriteOutbound(Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(textPayload)))); Assert.True(encoderChannel.WriteOutbound(Unpooled.WrappedBuffer(binaryPayload))); var compressedTextPayload = encoderChannel.ReadOutbound <IByteBuffer>(); var compressedBinaryPayload = encoderChannel.ReadOutbound <IByteBuffer>(); TextWebSocketFrame compressedTextFrame = new TextWebSocketFrame(true, WebSocketRsv.Rsv1, compressedTextPayload); BinaryWebSocketFrame compressedBinaryFrame = new BinaryWebSocketFrame(true, WebSocketRsv.Rsv1, compressedBinaryPayload); Assert.True(decoderChannel.WriteInbound(compressedTextFrame)); Assert.True(decoderChannel.WriteInbound(compressedBinaryFrame)); var inboundTextFrame = decoderChannel.ReadInbound <TextWebSocketFrame>(); var inboundBinaryFrame = decoderChannel.ReadInbound <BinaryWebSocketFrame>(); Assert.Equal(WebSocketRsv.Rsv1, inboundTextFrame.Rsv); Assert.Equal(compressedTextPayload, inboundTextFrame.Content); Assert.True(inboundTextFrame.Release()); Assert.Equal(0, inboundBinaryFrame.Rsv); Assert.Equal(binaryPayload, ByteBufferUtil.GetBytes(inboundBinaryFrame.Content)); Assert.True(inboundBinaryFrame.Release()); Assert.True(encoderChannel.FinishAndReleaseAll()); Assert.False(decoderChannel.Finish()); }
private void CancelGoAwayStreams(int lastStreamId, Http2Error errorCode, IByteBuffer debugData) { if (0u >= (uint)_pendingStreams.Count) { return; } var e = new Http2GoAwayException(lastStreamId, errorCode, ByteBufferUtil.GetBytes(debugData)); var keyList = new List <int>(_pendingStreams.Count); foreach (var stream in _pendingStreams.Values) { if (stream._streamId > lastStreamId) { keyList.Add(stream._streamId); stream.Close(e); } } foreach (var item in keyList) { _ = _pendingStreams.Remove(item); } }