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