diff --git a/src/Kestrel.Core/Internal/Http/HttpRequestStream.cs b/src/Kestrel.Core/Internal/Http/HttpRequestStream.cs index ff9c9a9d47..3451bcdb30 100644 --- a/src/Kestrel.Core/Internal/Http/HttpRequestStream.cs +++ b/src/Kestrel.Core/Internal/Http/HttpRequestStream.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; @@ -192,29 +193,32 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidateState(CancellationToken cancellationToken) { - switch (_state) + var state = _state; + if (state == HttpStreamState.Open) { - case HttpStreamState.Open: - if (cancellationToken.IsCancellationRequested) - { - cancellationToken.ThrowIfCancellationRequested(); - } - break; - case HttpStreamState.Closed: - throw new ObjectDisposedException(nameof(HttpRequestStream)); - case HttpStreamState.Aborted: - if (_error != null) - { - ExceptionDispatchInfo.Capture(_error).Throw(); - } - else - { - throw new TaskCanceledException(); - } - break; + cancellationToken.ThrowIfCancellationRequested(); } + else if (state == HttpStreamState.Closed) + { + ThrowObjectDisposedException(); + } + else + { + if (_error != null) + { + ExceptionDispatchInfo.Capture(_error).Throw(); + } + else + { + ThrowTaskCanceledException(); + } + } + + void ThrowObjectDisposedException() => throw new ObjectDisposedException(nameof(HttpRequestStream)); + void ThrowTaskCanceledException() => throw new TaskCanceledException(); } } } diff --git a/src/Kestrel.Core/Internal/Http/HttpResponseStream.cs b/src/Kestrel.Core/Internal/Http/HttpResponseStream.cs index 0831c74fbf..79d31367c8 100644 --- a/src/Kestrel.Core/Internal/Http/HttpResponseStream.cs +++ b/src/Kestrel.Core/Internal/Http/HttpResponseStream.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure; @@ -148,25 +149,23 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidateState(CancellationToken cancellationToken) { - switch (_state) + var state = _state; + if (state == HttpStreamState.Open || state == HttpStreamState.Aborted) { - case HttpStreamState.Open: - if (cancellationToken.IsCancellationRequested) - { - cancellationToken.ThrowIfCancellationRequested(); - } - break; - case HttpStreamState.Closed: - throw new ObjectDisposedException(nameof(HttpResponseStream), CoreStrings.WritingToResponseBodyAfterResponseCompleted); - case HttpStreamState.Aborted: - if (cancellationToken.IsCancellationRequested) - { - // Aborted state only throws on write if cancellationToken requests it - cancellationToken.ThrowIfCancellationRequested(); - } - break; + // Aborted state only throws on write if cancellationToken requests it + cancellationToken.ThrowIfCancellationRequested(); + } + else + { + ThrowObjectDisposedException(); + } + + void ThrowObjectDisposedException() + { + throw new ObjectDisposedException(nameof(HttpResponseStream), CoreStrings.WritingToResponseBodyAfterResponseCompleted); } } }