// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.IO; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure; namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http { class FrameRequestStream : Stream { private MessageBody _body; private FrameStreamState _state; private Exception _error; public FrameRequestStream() { _state = FrameStreamState.Closed; } public override bool CanRead => true; public override bool CanSeek => false; public override bool CanWrite => false; public override long Length { get { throw new NotSupportedException(); } } public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } } public override void Flush() { // No-op. } public override Task FlushAsync(CancellationToken cancellationToken) { // No-op. return TaskUtilities.CompletedTask; } public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } public override void SetLength(long value) { throw new NotSupportedException(); } public override int Read(byte[] buffer, int offset, int count) { // ValueTask uses .GetAwaiter().GetResult() if necessary return ReadAsync(buffer, offset, count).Result; } #if NET451 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { ValidateState(default(CancellationToken)); var task = ReadAsync(buffer, offset, count, default(CancellationToken), state); if (callback != null) { task.ContinueWith(t => callback.Invoke(t)); } return task; } public override int EndRead(IAsyncResult asyncResult) { return ((Task)asyncResult).GetAwaiter().GetResult(); } private Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state) { ValidateState(cancellationToken); var tcs = new TaskCompletionSource(state); var task = _body.ReadAsync(new ArraySegment(buffer, offset, count), cancellationToken); task.AsTask().ContinueWith((task2, state2) => { var tcs2 = (TaskCompletionSource)state2; if (task2.IsCanceled) { tcs2.SetCanceled(); } else if (task2.IsFaulted) { tcs2.SetException(task2.Exception); } else { tcs2.SetResult(task2.Result); } }, tcs, cancellationToken); return tcs.Task; } #endif public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { var task = ValidateState(cancellationToken); if (task == null) { // Needs .AsTask to match Stream's Async method return types return _body.ReadAsync(new ArraySegment(buffer, offset, count), cancellationToken).AsTask(); } return task; } public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } public void StartAcceptingReads(MessageBody body) { // Only start if not aborted if (_state == FrameStreamState.Closed) { _state = FrameStreamState.Open; _body = body; } } public void PauseAcceptingReads() { _state = FrameStreamState.Closed; } public void ResumeAcceptingReads() { if (_state == FrameStreamState.Closed) { _state = FrameStreamState.Open; } } public void StopAcceptingReads() { // Can't use dispose (or close) as can be disposed too early by user code // As exampled in EngineTests.ZeroContentLengthNotSetAutomaticallyForCertainStatusCodes _state = FrameStreamState.Closed; _body = null; } public void Abort(Exception error = null) { // We don't want to throw an ODE until the app func actually completes. // If the request is aborted, we throw an TaskCanceledException instead, // unless error is not null, in which case we throw it. if (_state != FrameStreamState.Closed) { _state = FrameStreamState.Aborted; _error = error; } } private Task ValidateState(CancellationToken cancellationToken) { switch (_state) { case FrameStreamState.Open: if (cancellationToken.IsCancellationRequested) { return TaskUtilities.GetCancelledZeroTask(cancellationToken); } break; case FrameStreamState.Closed: throw new ObjectDisposedException(nameof(FrameRequestStream)); case FrameStreamState.Aborted: return _error != null ? TaskUtilities.GetFaultedTask(_error) : TaskUtilities.GetCancelledZeroTask(); } return null; } } }