|
| 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.Net.WebSockets; |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.WebSockets; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Used in <see cref="WebSocketMiddleware"/> to wrap the <see cref="HttpContext"/>.Request.Body stream |
| 11 | +/// so that we can call <see cref="HttpContext.Abort"/> when the stream is disposed and the WebSocket is in the <see cref="WebSocketState.Aborted"/> state. |
| 12 | +/// The Stream provided by Kestrel (and maybe other servers) noops in Dispose as it doesn't know whether it's a graceful close or not |
| 13 | +/// and can result in truncated responses if in the graceful case. |
| 14 | +/// |
| 15 | +/// This handles explicit <see cref="WebSocket.Abort"/> calls as well as the Keep-Alive timeout setting <see cref="WebSocketState.Aborted"/> and disposing the stream. |
| 16 | +/// </summary> |
| 17 | +/// <remarks> |
| 18 | +/// Workaround for https://github.com/dotnet/runtime/issues/44272 |
| 19 | +/// </remarks> |
| 20 | +internal sealed class AbortStream : Stream |
| 21 | +{ |
| 22 | + private readonly Stream _innerStream; |
| 23 | + private readonly HttpContext _httpContext; |
| 24 | + |
| 25 | + public WebSocket? WebSocket { get; set; } |
| 26 | + |
| 27 | + public AbortStream(HttpContext httpContext, Stream innerStream) |
| 28 | + { |
| 29 | + _innerStream = innerStream; |
| 30 | + _httpContext = httpContext; |
| 31 | + } |
| 32 | + |
| 33 | + public override bool CanRead => _innerStream.CanRead; |
| 34 | + |
| 35 | + public override bool CanSeek => _innerStream.CanSeek; |
| 36 | + |
| 37 | + public override bool CanWrite => _innerStream.CanWrite; |
| 38 | + |
| 39 | + public override bool CanTimeout => _innerStream.CanTimeout; |
| 40 | + |
| 41 | + public override long Length => _innerStream.Length; |
| 42 | + |
| 43 | + public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; } |
| 44 | + |
| 45 | + public override void Flush() |
| 46 | + { |
| 47 | + _innerStream.Flush(); |
| 48 | + } |
| 49 | + |
| 50 | + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 51 | + { |
| 52 | + return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); |
| 53 | + } |
| 54 | + |
| 55 | + public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| 56 | + { |
| 57 | + return _innerStream.ReadAsync(buffer, cancellationToken); |
| 58 | + } |
| 59 | + |
| 60 | + public override int Read(byte[] buffer, int offset, int count) |
| 61 | + { |
| 62 | + return _innerStream.Read(buffer, offset, count); |
| 63 | + } |
| 64 | + |
| 65 | + public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) |
| 66 | + { |
| 67 | + return _innerStream.BeginRead(buffer, offset, count, callback, state); |
| 68 | + } |
| 69 | + |
| 70 | + public override int EndRead(IAsyncResult asyncResult) |
| 71 | + { |
| 72 | + return _innerStream.EndRead(asyncResult); |
| 73 | + } |
| 74 | + |
| 75 | + public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) |
| 76 | + { |
| 77 | + return _innerStream.BeginWrite(buffer, offset, count, callback, state); |
| 78 | + } |
| 79 | + |
| 80 | + public override void EndWrite(IAsyncResult asyncResult) |
| 81 | + { |
| 82 | + _innerStream.EndWrite(asyncResult); |
| 83 | + } |
| 84 | + |
| 85 | + public override long Seek(long offset, SeekOrigin origin) |
| 86 | + { |
| 87 | + return _innerStream.Seek(offset, origin); |
| 88 | + } |
| 89 | + |
| 90 | + public override void SetLength(long value) |
| 91 | + { |
| 92 | + _innerStream.SetLength(value); |
| 93 | + } |
| 94 | + |
| 95 | + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 96 | + { |
| 97 | + return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); |
| 98 | + } |
| 99 | + |
| 100 | + public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| 101 | + { |
| 102 | + return _innerStream.WriteAsync(buffer, cancellationToken); |
| 103 | + } |
| 104 | + |
| 105 | + public override void Write(byte[] buffer, int offset, int count) |
| 106 | + { |
| 107 | + _innerStream.Write(buffer, offset, count); |
| 108 | + } |
| 109 | + |
| 110 | + public override Task FlushAsync(CancellationToken cancellationToken) |
| 111 | + { |
| 112 | + return _innerStream.FlushAsync(cancellationToken); |
| 113 | + } |
| 114 | + |
| 115 | + public override ValueTask DisposeAsync() |
| 116 | + { |
| 117 | + return _innerStream.DisposeAsync(); |
| 118 | + } |
| 119 | + |
| 120 | + protected override void Dispose(bool disposing) |
| 121 | + { |
| 122 | + // Currently, if ManagedWebSocket sets the Aborted state it calls Stream.Dispose after |
| 123 | + if (WebSocket?.State == WebSocketState.Aborted) |
| 124 | + { |
| 125 | + _httpContext.Abort(); |
| 126 | + } |
| 127 | + _innerStream.Dispose(); |
| 128 | + } |
| 129 | +} |
0 commit comments