Skip to content

Add reset stream API - #104

Draft
gjcairo wants to merge 1 commit into
swift-server:mainfrom
gjcairo:reset-streams
Draft

Add reset stream API#104
gjcairo wants to merge 1 commit into
swift-server:mainfrom
gjcairo:reset-streams

Conversation

@gjcairo

@gjcairo gjcairo commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Until now, a request handler had no way to abort the stream carrying a request.

This PR adds a reset operation to the response side:

  • ResponseSender.reset(_:): abort before any response head is sent.
  • Writer.reset(_:): abort a response that has already started.

Because a reset is inherently protocol-specific, reset hands the handler a protocol-discriminated NIOHTTPServer.StreamReset to switch over:

  • .http2 → sends an HTTP/2 RST_STREAM with a chosen HTTP2ErrorCode.
  • .http3 → sends an HTTP/3 RESET_STREAM with a chosen QUIC application error code.
  • .unavailable → HTTP/1.1, which has no per-stream reset.

reset consumes the sender/writer, so users are prevented from writing anything further once the stream has been reset.

An example scenario using CONNECT:

  let writer = try await responseSender.send(.init(status: .ok))   // e.g. CONNECT tunnel established
  // … the tunnelled connection fails …
  try writer.reset { streamReset in
      switch consume streamReset {
      case .http2(let h2): 
        h2.reset(code: .connectError)
      case .http3(let h3): 
        h3.reset(code: 0x010f)   // H3_CONNECT_ERROR
      case .unavailable:
        break
      }
  }

@gjcairo
gjcairo requested review from aryan-25 and ehaydenr July 29, 2026 17:40
@gjcairo gjcairo added the 🆕 semver/minor Adds new public API. label Jul 29, 2026
@ehaydenr

Copy link
Copy Markdown
Collaborator

Related apple/swift-nio-http3#14

///
/// - 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`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
// The `HTTP2FramePayloadToHTTPServerCodec` on the stream channel translates this event into an `RST_STREAM`
// The `HTTP2FramePayloadToHTTPServerCodec` on the stream channel translates this event into a `RST_STREAM`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants