-
Notifications
You must be signed in to change notification settings - Fork 10
Add reset stream API #104
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
Draft
gjcairo
wants to merge
1
commit into
swift-server:main
Choose a base branch
from
gjcairo:reset-streams
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add reset stream API #104
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift HTTP Server open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift HTTP Server project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift HTTP Server project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import NIOCore | ||
| public import NIOHTTP2 | ||
| import NIOHTTPTypes | ||
| import NIOHTTPTypesHTTP2 | ||
|
|
||
| #if HTTP3 | ||
| import NIOQUICHelpers | ||
| #endif // HTTP3 | ||
|
|
||
| @available(anyAppleOS 26.0, *) | ||
| extension NIOHTTPServer { | ||
| /// The protocol-specific surface for resetting the stream carrying a request. | ||
| /// | ||
| /// Stream resets only exists over HTTP/2 and HTTP/3. The only abrupt tear-down mechanism available | ||
| /// for HTTP/1.1 is closing the connection. | ||
| @nonexhaustive | ||
| public enum StreamReset: ~Copyable { | ||
| /// The protocol has no per-stream coded reset (for example HTTP/1.1). | ||
| /// | ||
| /// There is nothing to reset with a code here. Returning from the handler without concluding the response | ||
| /// aborts the exchange and the connection is closed. | ||
| case unavailable | ||
|
|
||
| /// The connection is HTTP/2; ``HTTP2StreamReset`` sends a `RST_STREAM`. | ||
| case http2(HTTP2StreamReset) | ||
|
|
||
| #if HTTP3 | ||
| /// The connection is HTTP/3; ``HTTP3StreamReset`` sends a QUIC `RESET_STREAM`. | ||
| case http3(HTTP3StreamReset) | ||
| #endif // HTTP3 | ||
| } | ||
|
|
||
| /// Resets an HTTP/2 stream by sending a `RST_STREAM` frame with a chosen error code. | ||
| public struct HTTP2StreamReset: ~Copyable { | ||
| private let channel: any Channel | ||
|
|
||
| init(channel: any Channel) { | ||
| self.channel = channel | ||
| } | ||
|
|
||
| /// Sends a `RST_STREAM` frame for this stream with the provided error code. | ||
| /// | ||
| /// - Parameter code: The `RST_STREAM` error code to send. | ||
| public consuming func reset(code: HTTP2ErrorCode) { | ||
| // The `HTTP2FramePayloadToHTTPServerCodec` on the stream channel translates this event into an `RST_STREAM` | ||
| // frame. | ||
| self.channel.triggerUserOutboundEvent( | ||
| NIOHTTP2FramePayloadToHTTPEvent.reset(code: code), | ||
| promise: nil | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| #if HTTP3 | ||
| /// Resets an HTTP/3 stream by sending a QUIC `RESET_STREAM` frame with a chosen error code. | ||
| public struct HTTP3StreamReset: ~Copyable { | ||
| private let channel: any Channel | ||
|
|
||
| init(channel: any Channel) { | ||
| self.channel = channel | ||
| } | ||
|
|
||
| /// Sends a QUIC `RESET_STREAM` frame for this stream with the provided error code. | ||
| /// | ||
| /// - Parameter code: The QUIC application error code to send. It must be a valid application error code that is | ||
| /// less than 2^62 (the maximum QUIC variable-length integer value). If the code is out of range, the stream | ||
| /// is not reset. | ||
| public consuming func reset(code: UInt64) { | ||
| guard let resetCode = QUICApplicationErrorCode(code) else { return } | ||
|
|
||
| self.channel.triggerUserOutboundEvent(QUICResetStreamEvent(code: resetCode), promise: nil) | ||
| } | ||
| } | ||
| #endif // HTTP3 | ||
| } | ||
|
|
||
| @available(*, unavailable) | ||
| extension NIOHTTPServer.StreamReset: Sendable {} | ||
|
|
||
| @available(*, unavailable) | ||
| extension NIOHTTPServer.HTTP2StreamReset: Sendable {} | ||
|
|
||
| #if HTTP3 | ||
| @available(*, unavailable) | ||
| extension NIOHTTPServer.HTTP3StreamReset: Sendable {} | ||
| #endif // HTTP3 | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: