Skip to content
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

support h2 stream resets through user events #241

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Sources/NIOHTTPTypesHTTP2/HTTP2ToHTTPCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public final class HTTP2FramePayloadToHTTPClientCodec: ChannelDuplexHandler, Rem
context.fireErrorCaught(error)
}
}

public func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
if let ev = event as? HTTP2FramePayloadToHTTPEvent, case .reset(let code) = ev.kind {
context.writeAndFlush(self.wrapOutboundOut(.rstStream(code)), promise: promise)
return
}
context.triggerUserOutboundEvent(event, promise: promise)
}
}

// MARK: - Server
Expand Down Expand Up @@ -262,4 +270,34 @@ public final class HTTP2FramePayloadToHTTPServerCodec: ChannelDuplexHandler, Rem
let transformedPayload = self.baseCodec.processOutboundData(responsePart, allocator: context.channel.allocator)
context.write(self.wrapOutboundOut(transformedPayload), promise: promise)
}

public func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
if let ev = event as? HTTP2FramePayloadToHTTPEvent, case .reset(let code) = ev.kind {
context.writeAndFlush(self.wrapOutboundOut(.rstStream(code)), promise: promise)
return
}
context.triggerUserOutboundEvent(event, promise: promise)
}
}

/// Events that can be sent by the application to be handled by the `HTTP2StreamChannel`
public struct HTTP2FramePayloadToHTTPEvent {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

It should also be Hashable and Sendable, it's generally useful to add these to value types.

fileprivate enum Kind {
case reset(HTTP2ErrorCode)
}

fileprivate var kind: Kind
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make these private and use the public API in the handlers above?


/// Send a `RST_STREAM` with the specified code
public static func reset(code: HTTP2ErrorCode) -> Self {
.init(kind: .reset(code))
}

/// Returns reset code if the event is a reset
public func reset() -> HTTP2ErrorCode? {
Copy link
Contributor

Choose a reason for hiding this comment

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

A computed property would be more idiomatic here

if case let .reset(code) = self.kind {
return code
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: convention is NIO is to exhaustively switch over enums

}
}
14 changes: 14 additions & 0 deletions Tests/NIOHTTPTypesHTTP2Tests/NIOHTTPTypesHTTP2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ final class NIOHTTPTypesHTTP2Tests: XCTestCase {

try self.channel.writeOutbound(HTTPRequestPart.head(Self.request))
try self.channel.writeOutbound(HTTPRequestPart.end(Self.trailers))
try self.channel.triggerUserOutboundEvent(HTTP2FramePayloadToHTTPEvent.reset(code: .enhanceYourCalm)).wait()

XCTAssertEqual(try self.channel.readOutbound(as: HTTP2Frame.FramePayload.self)?.headers, Self.oldRequest)
XCTAssertEqual(try self.channel.readOutbound(as: HTTP2Frame.FramePayload.self)?.headers, Self.oldTrailers)
switch try self.channel.readOutbound(as: HTTP2Frame.FramePayload.self) {
case .rstStream(.enhanceYourCalm):
break
default:
XCTFail("expected reset")
}

try self.channel.writeInbound(HTTP2Frame.FramePayload(headers: Self.oldResponse))
try self.channel.writeInbound(HTTP2Frame.FramePayload(headers: Self.oldTrailers))
Expand All @@ -142,9 +149,16 @@ final class NIOHTTPTypesHTTP2Tests: XCTestCase {

try self.channel.writeOutbound(HTTPResponsePart.head(Self.response))
try self.channel.writeOutbound(HTTPResponsePart.end(Self.trailers))
try self.channel.triggerUserOutboundEvent(HTTP2FramePayloadToHTTPEvent.reset(code: .enhanceYourCalm)).wait()

XCTAssertEqual(try self.channel.readOutbound(as: HTTP2Frame.FramePayload.self)?.headers, Self.oldResponse)
XCTAssertEqual(try self.channel.readOutbound(as: HTTP2Frame.FramePayload.self)?.headers, Self.oldTrailers)
switch try self.channel.readOutbound(as: HTTP2Frame.FramePayload.self) {
case .rstStream(.enhanceYourCalm):
break
default:
XCTFail("expected reset")
}

XCTAssertTrue(try self.channel.finish().isClean)
}
Expand Down
Loading