| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.IO.Pipelines; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using CoreWCF.Runtime; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels.Framing |
| | | 13 | | { |
| | | 14 | | internal class DuplexPipeStream : Stream, IAsyncDisposable |
| | | 15 | | { |
| | | 16 | | private readonly PipeReader _input; |
| | | 17 | | private readonly PipeWriter _output; |
| | | 18 | | private IInputLengthDecider _inputLengthDecider; |
| | | 19 | | |
| | 0 | 20 | | public DuplexPipeStream(PipeReader input, PipeWriter output) |
| | | 21 | | { |
| | 0 | 22 | | _input = input; |
| | 0 | 23 | | _output = output; |
| | 0 | 24 | | _inputLengthDecider = NoopInputLengthDecider.Instance; |
| | 0 | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | public override bool CanRead => true; |
| | | 28 | | |
| | 0 | 29 | | public override bool CanSeek => false; |
| | | 30 | | |
| | 0 | 31 | | public override bool CanWrite => true; |
| | | 32 | | |
| | 0 | 33 | | public override long Length => throw new NotSupportedException(); |
| | | 34 | | |
| | | 35 | | public override long Position |
| | | 36 | | { |
| | | 37 | | get |
| | | 38 | | { |
| | 0 | 39 | | throw new NotSupportedException(); |
| | | 40 | | } |
| | | 41 | | set |
| | | 42 | | { |
| | 0 | 43 | | throw new NotSupportedException(); |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public override long Seek(long offset, SeekOrigin origin) |
| | | 48 | | { |
| | 0 | 49 | | throw new NotSupportedException(); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public override void SetLength(long value) |
| | | 53 | | { |
| | 0 | 54 | | throw new NotSupportedException(); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 58 | | { |
| | 0 | 59 | | ValueTask<int> vt = ReadAsyncInternal(new Memory<byte>(buffer, offset, count), default); |
| | 0 | 60 | | return vt.IsCompleted ? |
| | 0 | 61 | | vt.Result : |
| | 0 | 62 | | vt.AsTask().GetAwaiter().GetResult(); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = |
| | | 66 | | { |
| | 0 | 67 | | return ReadAsyncInternal(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask(); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | // TODO: Enable code when moving to .NET 5 |
| | | 71 | | //public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = defau |
| | | 72 | | //{ |
| | | 73 | | // return ReadAsyncInternal(destination, cancellationToken); |
| | | 74 | | //} |
| | | 75 | | |
| | | 76 | | public override void Write(byte[] buffer, int offset, int count) |
| | | 77 | | { |
| | 0 | 78 | | WriteAsync(buffer, offset, count).GetAwaiter().GetResult(); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | | 82 | | { |
| | 0 | 83 | | return _output.WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // TODO: Enable code when moving to .NET 5 |
| | | 87 | | //public override ValueTask WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = defaul |
| | | 88 | | //{ |
| | | 89 | | // return _output.WriteAsync(source, cancellationToken).GetAsValueTask(); |
| | | 90 | | //} |
| | | 91 | | |
| | | 92 | | public override void Flush() |
| | | 93 | | { |
| | 0 | 94 | | FlushAsync(CancellationToken.None).GetAwaiter().GetResult(); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | | 98 | | { |
| | 0 | 99 | | return _output.FlushAsync(cancellationToken).AsTask(); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, CancellationToken cancellationToken) |
| | | 103 | | { |
| | | 104 | | while (true) |
| | | 105 | | { |
| | 0 | 106 | | var result = await _input.ReadAsync(cancellationToken); |
| | 0 | 107 | | var readableBuffer = result.Buffer; |
| | | 108 | | try |
| | | 109 | | { |
| | 0 | 110 | | if (!readableBuffer.IsEmpty) |
| | | 111 | | { |
| | | 112 | | // buffer.Count is int |
| | 0 | 113 | | var count = _inputLengthDecider.LengthToConsume(readableBuffer, destination.Length); |
| | 0 | 114 | | readableBuffer = readableBuffer.Slice(0, count); |
| | 0 | 115 | | readableBuffer.CopyTo(destination.Span); |
| | 0 | 116 | | return count; |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (result.IsCompleted) |
| | | 120 | | { |
| | 0 | 121 | | return 0; |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | finally |
| | | 125 | | { |
| | 0 | 126 | | _input.AdvanceTo(readableBuffer.End, readableBuffer.End); |
| | | 127 | | } |
| | | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object stat |
| | | 132 | | { |
| | 0 | 133 | | return ReadAsync(buffer, offset, count).ToApm(callback, state); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public override int EndRead(IAsyncResult asyncResult) |
| | | 137 | | { |
| | 0 | 138 | | return asyncResult.ToApmEnd<int>(); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object sta |
| | | 142 | | { |
| | 0 | 143 | | return WriteAsync(buffer, offset, count).ToApm(callback, state); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public override void EndWrite(IAsyncResult asyncResult) |
| | | 147 | | { |
| | 0 | 148 | | asyncResult.ToApmEnd(); |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | protected override void Dispose(bool disposing) |
| | | 152 | | { |
| | 0 | 153 | | _input.Complete(); |
| | 0 | 154 | | _output.Complete(); |
| | 0 | 155 | | base.Dispose(disposing); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | public async ValueTask DisposeAsync() |
| | | 159 | | { |
| | 0 | 160 | | await _input.CompleteAsync(); |
| | 0 | 161 | | await _output.CompleteAsync(); |
| | 0 | 162 | | base.Dispose(true); |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | internal void SetContentType(string contentType) |
| | | 166 | | { |
| | 0 | 167 | | if (contentType == "application/ssl-tls" /*FramingUpgradeString.SslOrTls*/) |
| | 0 | 168 | | _inputLengthDecider = new TlsInputLengthDecider(); |
| | 0 | 169 | | else _inputLengthDecider = NoopInputLengthDecider.Instance; |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | internal interface IInputLengthDecider |
| | | 173 | | { |
| | | 174 | | int LengthToConsume(ReadOnlySequence<byte> buffer, int destinationLength); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | internal class TlsInputLengthDecider : IInputLengthDecider |
| | | 178 | | { |
| | | 179 | | // buffer[0] is TLS Frame content type, eg 23 = ApplicationData |
| | | 180 | | // buffer[1] and buffer[2] are the TLS version, eg 0x0303 == TLS 1.2 |
| | | 181 | | // buffer[3] and buffer[4] and big endian integer for length |
| | | 182 | | |
| | | 183 | | private int _frameSize; |
| | | 184 | | |
| | | 185 | | public int LengthToConsume(ReadOnlySequence<byte> buffer, int destinationLength) |
| | | 186 | | { |
| | | 187 | | // Maximum number of bytes that can be read from incoming buffer. This is either the entire buffer if th |
| | | 188 | | // space in the destination, or however much space is available in the destination buffer |
| | 0 | 189 | | int maxRead = (int)Math.Min(buffer.Length, destinationLength); |
| | | 190 | | int bytesToRead; |
| | | 191 | | |
| | | 192 | | // If there's still bytes left to be read from the current frame, we don't need to read the frame header |
| | | 193 | | // read the frame size if we've finished reading the previous frame. |
| | 0 | 194 | | if (_frameSize == 0) |
| | | 195 | | { |
| | 0 | 196 | | if (buffer.Length < 5) |
| | | 197 | | { |
| | | 198 | | // Need at least 5 bytes to read size from frame header so presuming this isn't a TLS frame |
| | | 199 | | // so indicating to consume everything that's possible. |
| | 0 | 200 | | return maxRead; |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | if (buffer.IsSingleSegment || buffer.First.Length >= 5) |
| | | 204 | | { |
| | | 205 | | // We have enough bytes to read the frame size from the first segment |
| | 0 | 206 | | var span = buffer.First.Span; |
| | | 207 | | Fx.Assert(span[1] == 3, "Invalid TLS header"); |
| | | 208 | | // The length in the TLS header doesn't include the header itself so that needs to be added |
| | 0 | 209 | | _frameSize = ((span[3] << 8) | span[4]) + 5; |
| | | 210 | | } |
| | | 211 | | else |
| | | 212 | | { |
| | | 213 | | // We have at least 5 bytes, but the first 5 bytes aren't in the first segment so get the 5 byte |
| | 0 | 214 | | var frameHeaderBytes = buffer.Slice(0, 5).ToArray(); |
| | | 215 | | // The length in the TLS header doesn't include the header itself so that needs to be added |
| | 0 | 216 | | _frameSize = ((frameHeaderBytes[3] << 8) | frameHeaderBytes[4]) + 5; |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | // If the frame size is smaller than the number of bytes read from the input, then only read frame size |
| | 0 | 221 | | bytesToRead = Math.Min(maxRead, _frameSize); |
| | | 222 | | // Decrement _frameSize by bytesToRead to store number of bytes still pending to be read in future reads |
| | 0 | 223 | | _frameSize -= bytesToRead; |
| | 0 | 224 | | return bytesToRead; |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | internal class NoopInputLengthDecider : IInputLengthDecider |
| | | 229 | | { |
| | 0 | 230 | | internal static IInputLengthDecider Instance = new NoopInputLengthDecider(); |
| | | 231 | | public int LengthToConsume(ReadOnlySequence<byte> buffer, int destinationLength) |
| | | 232 | | { |
| | | 233 | | // Can't read more than there's space in the destination buffer |
| | 0 | 234 | | return (int)Math.Min(buffer.Length, destinationLength); |
| | | 235 | | } |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | } |