-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add KeepAliveTimeout support to WebSocketMiddleware #57293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Http.Features.IHttpMetricsTagsFeature.MetricsDisabled.get -> bool | ||
Microsoft.AspNetCore.Http.Features.IHttpMetricsTagsFeature.MetricsDisabled.set -> void | ||
Microsoft.AspNetCore.Http.WebSocketAcceptContext.KeepAliveTimeout.get -> System.TimeSpan? | ||
Microsoft.AspNetCore.Http.WebSocketAcceptContext.KeepAliveTimeout.set -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net.WebSockets; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.WebSockets; | ||
|
||
/// <summary> | ||
/// Used in WebSocketMiddleware to wrap the HttpContext.Request.Body stream | ||
BrennanConroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
/// so that we can call HttpContext.Abort when the stream is disposed and the WebSocket is in the Aborted state. | ||
/// 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 | ||
/// and can result in truncated responses if in the graceful case. | ||
/// | ||
/// This handles explicit WebSocket.Abort calls as well as the Keep-Alive timeout setting Aborted and disposing the stream. | ||
/// </summary> | ||
/// <remarks> | ||
/// Workaround for https://github.com/dotnet/runtime/issues/44272 | ||
/// </remarks> | ||
internal sealed class AbortStream : Stream | ||
{ | ||
private readonly Stream _innerStream; | ||
private readonly HttpContext _httpContext; | ||
|
||
public WebSocket? WebSocket { get; set; } | ||
|
||
public AbortStream(HttpContext httpContext, Stream innerStream) | ||
{ | ||
_innerStream = innerStream; | ||
_httpContext = httpContext; | ||
} | ||
|
||
public override bool CanRead => _innerStream.CanRead; | ||
|
||
public override bool CanSeek => _innerStream.CanSeek; | ||
|
||
public override bool CanWrite => _innerStream.CanWrite; | ||
|
||
public override bool CanTimeout => _innerStream.CanTimeout; | ||
|
||
public override long Length => _innerStream.Length; | ||
|
||
public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; } | ||
|
||
public override void Flush() | ||
{ | ||
_innerStream.Flush(); | ||
} | ||
|
||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); | ||
} | ||
|
||
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
return _innerStream.ReadAsync(buffer, cancellationToken); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
return _innerStream.Read(buffer, offset, count); | ||
} | ||
|
||
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) | ||
{ | ||
return _innerStream.BeginRead(buffer, offset, count, callback, state); | ||
} | ||
|
||
public override int EndRead(IAsyncResult asyncResult) | ||
{ | ||
return _innerStream.EndRead(asyncResult); | ||
} | ||
|
||
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) | ||
{ | ||
return _innerStream.BeginWrite(buffer, offset, count, callback, state); | ||
} | ||
|
||
public override void EndWrite(IAsyncResult asyncResult) | ||
{ | ||
_innerStream.EndWrite(asyncResult); | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
return _innerStream.Seek(offset, origin); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
_innerStream.SetLength(value); | ||
} | ||
|
||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); | ||
} | ||
|
||
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) | ||
{ | ||
return _innerStream.WriteAsync(buffer, cancellationToken); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_innerStream.Write(buffer, offset, count); | ||
} | ||
|
||
public override Task FlushAsync(CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.FlushAsync(cancellationToken); | ||
} | ||
|
||
public override ValueTask DisposeAsync() | ||
{ | ||
return _innerStream.DisposeAsync(); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
// Currently, if ManagedWebSocket sets the Aborted state it calls Stream.Dispose after | ||
if (WebSocket?.State == WebSocketState.Aborted) | ||
{ | ||
_httpContext.Abort(); | ||
} | ||
_innerStream.Dispose(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Builder.WebSocketOptions.KeepAliveTimeout.get -> System.TimeSpan | ||
Microsoft.AspNetCore.Builder.WebSocketOptions.KeepAliveTimeout.set -> void |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.