Apply swift-format changes and CI (#472)

Motivation:

Standardised formatting makes the project cleaner to look at and work
on.

Modifications:

Setup swift-format with the same settings as Swift-NIO

Result:

Common formatting throughout
This commit is contained in:
Peter Adams authored and GitHub committed 2024-10-29 13:09:54 +00:00
1 parent 93586110f2
commit 1118370ec4
125 files changed
+21869 -11457

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
+1 -1
View File
@@ -10,7 +10,7 @@ jobs:
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
with:
license_header_check_project_name: "SwiftNIO"
format_check_enabled: false
format_check_enabled: true
shell_check_enabled: false
unit-tests:
+62
View File
@@ -0,0 +1,62 @@
{
"version" : 1,
"indentation" : {
"spaces" : 4
},
"tabWidth" : 4,
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"spacesAroundRangeFormationOperators" : false,
"indentConditionalCompilationBlocks" : false,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : true,
"lineBreakBeforeEachGenericRequirement" : true,
"lineLength" : 120,
"maximumBlankLines" : 1,
"respectsExistingLineBreaks" : true,
"prioritizeKeepingFunctionOutputTogether" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : false,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : false,
"UseLetInEveryBoundCaseVariable" : false,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : false,
"UseSynthesizedInitializer" : false,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
}
}
+1 -1
View File
@@ -30,6 +30,6 @@ let package = Package(
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
]
),
)
]
)
+9 -11
View File
@@ -19,22 +19,20 @@
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
import Foundation
import NIOCore
import NIOEmbedded
import NIOHTTP1
import NIOHTTP2
fileprivate func fuzzInput(_ bytes: UnsafeRawBufferPointer) throws {
private func fuzzInput(_ bytes: UnsafeRawBufferPointer) throws {
let channel = EmbeddedChannel()
defer {
_ = try? channel.finish()
}
_ = try channel.configureHTTP2Pipeline(
mode: .server,
initialLocalSettings: [HTTP2Setting(parameter: .maxConcurrentStreams, value: 1<<23)],
initialLocalSettings: [HTTP2Setting(parameter: .maxConcurrentStreams, value: 1 << 23)],
inboundStreamInitializer: nil
).wait()
try channel.connect(to: SocketAddress(unixDomainSocketPath: "/foo")).wait()
@@ -48,12 +46,12 @@ fileprivate func fuzzInput(_ bytes: UnsafeRawBufferPointer) throws {
@_cdecl("LLVMFuzzerTestOneInput")
public func FuzzServer(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
let bytes = UnsafeRawBufferPointer(start: start, count: count)
do {
let _ = try fuzzInput(bytes)
} catch {
// Errors parsing are to be expected since not all inputs will be well formed.
}
let bytes = UnsafeRawBufferPointer(start: start, count: count)
do {
let _ = try fuzzInput(bytes)
} catch {
// Errors parsing are to be expected since not all inputs will be well formed.
}
return 0
return 0
}
@@ -18,21 +18,27 @@ import NIOHTTP2
/// Test use only. Allows abstracting over the two multiplexer implementations to write common testing code
internal protocol MultiplexerChannelCreator {
func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOHTTP2Handler.StreamInitializer)
func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOHTTP2Handler.StreamInitializer
)
func createStreamChannel(_ initializer: @escaping NIOChannelInitializer) -> EventLoopFuture<Channel>
}
extension HTTP2StreamMultiplexer: MultiplexerChannelCreator { }
extension NIOHTTP2Handler.StreamMultiplexer: MultiplexerChannelCreator { }
extension HTTP2StreamMultiplexer: MultiplexerChannelCreator {}
extension NIOHTTP2Handler.StreamMultiplexer: MultiplexerChannelCreator {}
/// Have two `EmbeddedChannel` objects send and receive data from each other until
/// they make no forward progress.
func interactInMemory(_ first: EmbeddedChannel, _ second: EmbeddedChannel) throws {
precondition(first.eventLoop === second.eventLoop, "interactInMemory assumes both channels are on the same event loop.")
precondition(
first.eventLoop === second.eventLoop,
"interactInMemory assumes both channels are on the same event loop."
)
var operated: Bool
func readBytesFromChannel(_ channel: EmbeddedChannel) throws -> ByteBuffer? {
return try channel.readOutbound(as: ByteBuffer.self)
try channel.readOutbound(as: ByteBuffer.self)
}
repeat {
@@ -52,9 +52,12 @@ struct ServerOnly1KRequestsBenchmark {
headers.add(name: ":authority", value: "localhost", indexing: .nonIndexable)
headers.add(name: ":path", value: "/", indexing: .indexable)
headers.add(name: ":scheme", value: "https", indexing: .indexable)
headers.add(name: "user-agent",
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable)
headers.add(
name: "user-agent",
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable
)
headers.add(name: "accept-encoding", value: "gzip, deflate", indexing: .indexable)
var hpackEncoder = HPACKEncoder(allocator: .init())
@@ -108,7 +111,7 @@ struct ServerOnly1KRequestsBenchmark {
return settingsCopy
}
init(concurrentStreams: Int, pipelineConfigurator: (Channel) throws -> ()) throws {
init(concurrentStreams: Int, pipelineConfigurator: (Channel) throws -> Void) throws {
self.concurrentStreams = concurrentStreams
self.channel = EmbeddedChannel()
@@ -122,7 +125,7 @@ struct ServerOnly1KRequestsBenchmark {
initialBytes.writeImmutableBuffer(self.settingsACK)
try self.channel.writeInbound(initialBytes)
while try self.channel.readOutbound(as: ByteBuffer.self) != nil { }
while try self.channel.readOutbound(as: ByteBuffer.self) != nil {}
}
func tearDown() {
@@ -142,7 +145,7 @@ struct ServerOnly1KRequestsBenchmark {
private mutating func sendInterleavedRequests(_ interleavedRequests: Int) throws -> Int {
var streamID = self.streamID
for _ in 0 ..< interleavedRequests {
for _ in 0..<interleavedRequests {
self.headersFrame.setInteger(UInt32(Int32(streamID)), at: self.headersFrame.readerIndex + 5)
try self.channel.writeInbound(self.headersFrame)
streamID = streamID.advanced(by: 2)
@@ -150,7 +153,7 @@ struct ServerOnly1KRequestsBenchmark {
streamID = self.streamID
for _ in 0 ..< interleavedRequests {
for _ in 0..<interleavedRequests {
self.dataFrame.setInteger(UInt32(Int32(streamID)), at: self.dataFrame.readerIndex + 5)
try self.channel.writeInbound(self.dataFrame)
streamID = streamID.advanced(by: 2)
@@ -169,7 +172,7 @@ struct ServerOnly1KRequestsBenchmark {
}
}
fileprivate class TestServer: ChannelInboundHandler {
private class TestServer: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame.FramePayload
public typealias OutboundOut = HTTP2Frame.FramePayload
@@ -198,24 +201,24 @@ fileprivate class TestServer: ChannelInboundHandler {
func run(identifier: String) {
var interleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 100) { channel in
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(TestServer())
streamChannel.pipeline.addHandler(TestServer())
}.wait()
}
measure(identifier: identifier + "_interleaved") {
return try! interleaved.run()
try! interleaved.run()
}
interleaved.tearDown()
var noninterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 1) { channel in
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(TestServer())
streamChannel.pipeline.addHandler(TestServer())
}.wait()
}
measure(identifier: identifier + "_noninterleaved") {
return try! noninterleaved.run()
try! noninterleaved.run()
}
noninterleaved.tearDown()
@@ -223,25 +226,33 @@ func run(identifier: String) {
//
// MARK: - Inline HTTP2 multiplexer tests
var inlineInterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 100) { channel in
_ = try channel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(TestServer())
_ = try channel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(TestServer())
}.wait()
}
measure(identifier: identifier + "_inline_interleaved") {
return try! inlineInterleaved.run()
try! inlineInterleaved.run()
}
inlineInterleaved.tearDown()
var inlineNoninterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 1) { channel in
_ = try channel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(TestServer())
_ = try channel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(TestServer())
}.wait()
}
measure(identifier: identifier + "_inline_noninterleaved") {
return try! inlineNoninterleaved.run()
try! inlineNoninterleaved.run()
}
inlineNoninterleaved.tearDown()
@@ -39,14 +39,18 @@ final class ServerHandler: ChannelInboundHandler {
}
}
final class ClientHandler: ChannelInboundHandler {
typealias InboundIn = HTTPClientResponsePart
typealias OutboundOut = HTTPClientRequestPart
func channelActive(context: ChannelHandlerContext) {
// Send a request.
let head = HTTPRequestHead(version: .init(major: 2, minor: 0), method: .GET, uri: "/", headers: HTTPHeaders([("host", "localhost")]))
let head = HTTPRequestHead(
version: .init(major: 2, minor: 0),
method: .GET,
uri: "/",
headers: HTTPHeaders([("host", "localhost")])
)
context.write(self.wrapOutboundOut(.head(head)), promise: nil)
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)
}
@@ -54,27 +58,39 @@ final class ClientHandler: ChannelInboundHandler {
func run(identifier: String) {
testRun(identifier: identifier) { clientChannel in
return try! clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait()
try! clientChannel.configureHTTP2Pipeline(mode: .client, inboundStreamInitializer: nil).wait()
} serverPipelineConfigurator: { serverChannel in
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server) { channel in
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
}.wait()
}
//
// MARK: - Inline HTTP2 multiplexer tests
testRun(identifier: identifier + "_inline") { clientChannel in
return try! clientChannel.configureHTTP2Pipeline(mode: .client, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
try! clientChannel.configureHTTP2Pipeline(
mode: .client,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { channel in
channel.eventLoop.makeSucceededVoidFuture()
}.wait()
} serverPipelineConfigurator: { serverChannel in
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
_ = try! serverChannel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { channel in
channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ServerCodec(), ServerHandler()])
}.wait()
}
}
private func testRun(identifier: String, clientPipelineConfigurator: (Channel) throws -> MultiplexerChannelCreator, serverPipelineConfigurator: (Channel) throws -> ()) {
private func testRun(
identifier: String,
clientPipelineConfigurator: (Channel) throws -> MultiplexerChannelCreator,
serverPipelineConfigurator: (Channel) throws -> Void
) {
let loop = EmbeddedEventLoop()
measure(identifier: identifier) {
@@ -91,7 +107,9 @@ private func testRun(identifier: String, clientPipelineConfigurator: (Channel) t
let promise = clientChannel.eventLoop.makePromise(of: Channel.self)
clientMultiplexer.createStreamChannel(promise: promise) { channel in
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https), ClientHandler()])
channel.pipeline.addHandlers([
HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https), ClientHandler(),
])
}
clientChannel.embeddedEventLoop.run()
let child = try! promise.futureResult.wait()
@@ -108,4 +126,3 @@ private func testRun(identifier: String, clientPipelineConfigurator: (Channel) t
return sumOfStreamIDs
}
}
@@ -41,7 +41,6 @@ final class ServerHandler: ChannelInboundHandler {
}
}
final class ClientHandler: ChannelInboundHandler {
typealias InboundIn = HTTP2Frame.FramePayload
typealias OutboundOut = HTTP2Frame.FramePayload
@@ -56,10 +55,12 @@ final class ClientHandler: ChannelInboundHandler {
func channelActive(context: ChannelHandlerContext) {
// Send a request.
let headers = HPACKHeaders([(":path", "/"),
(":authority", "localhost"),
(":method", "GET"),
(":scheme", "https")])
let headers = HPACKHeaders([
(":path", "/"),
(":authority", "localhost"),
(":method", "GET"),
(":scheme", "https"),
])
if self.dataBlockCount > 0 {
let requestFramePayload = HTTP2Frame.FramePayload.headers(.init(headers: headers, endStream: false))
@@ -67,10 +68,18 @@ final class ClientHandler: ChannelInboundHandler {
let buffer = ByteBuffer(repeating: 0, count: self.dataBlockLengthBytes)
for _ in 0 ..< self.dataBlockCount-1 {
context.write(self.wrapOutboundOut(HTTP2Frame.FramePayload.data(.init(data: .byteBuffer(buffer), endStream: false))), promise: nil)
for _ in 0..<self.dataBlockCount - 1 {
context.write(
self.wrapOutboundOut(
HTTP2Frame.FramePayload.data(.init(data: .byteBuffer(buffer), endStream: false))
),
promise: nil
)
}
context.writeAndFlush(self.wrapOutboundOut(HTTP2Frame.FramePayload.data(.init(data: .byteBuffer(buffer), endStream: true))), promise: nil)
context.writeAndFlush(
self.wrapOutboundOut(HTTP2Frame.FramePayload.data(.init(data: .byteBuffer(buffer), endStream: true))),
promise: nil
)
} else {
let requestFramePayload = HTTP2Frame.FramePayload.headers(.init(headers: headers, endStream: true))
context.writeAndFlush(self.wrapOutboundOut(requestFramePayload), promise: nil)
@@ -80,49 +89,71 @@ final class ClientHandler: ChannelInboundHandler {
func run(identifier: String) {
testRun(identifier: identifier) { clientChannel in
return try! clientChannel.configureHTTP2Pipeline(mode: .client) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
try! clientChannel.configureHTTP2Pipeline(mode: .client) { channel in
channel.eventLoop.makeSucceededVoidFuture()
}.wait()
} serverPipelineConfigurator: { serverChannel in
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server) { channel in
return channel.pipeline.addHandler(ServerHandler())
channel.pipeline.addHandler(ServerHandler())
}.wait()
}
testRun(identifier: identifier + "_many", dataBlockCount: 100, dataBlockLengthBytes: 1000) { clientChannel in
return try! clientChannel.configureHTTP2Pipeline(mode: .client) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
try! clientChannel.configureHTTP2Pipeline(mode: .client) { channel in
channel.eventLoop.makeSucceededVoidFuture()
}.wait()
} serverPipelineConfigurator: { serverChannel in
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server) { channel in
return channel.pipeline.addHandler(ServerHandler())
channel.pipeline.addHandler(ServerHandler())
}.wait()
}
//
// MARK: - Inline HTTP2 multiplexer tests
testRun(identifier: identifier + "_inline") { clientChannel in
return try! clientChannel.configureHTTP2Pipeline(mode: .client, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
try! clientChannel.configureHTTP2Pipeline(
mode: .client,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { channel in
channel.eventLoop.makeSucceededVoidFuture()
}.wait()
} serverPipelineConfigurator: { serverChannel in
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
return channel.pipeline.addHandler(ServerHandler())
_ = try! serverChannel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { channel in
channel.pipeline.addHandler(ServerHandler())
}.wait()
}
testRun(identifier: identifier + "_many_inline", dataBlockCount: 100, dataBlockLengthBytes: 1000) { clientChannel in
return try! clientChannel.configureHTTP2Pipeline(mode: .client, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
try! clientChannel.configureHTTP2Pipeline(
mode: .client,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { channel in
channel.eventLoop.makeSucceededVoidFuture()
}.wait()
} serverPipelineConfigurator: { serverChannel in
_ = try! serverChannel.configureHTTP2Pipeline(mode: .server, connectionConfiguration: .init(), streamConfiguration: .init()) { channel in
return channel.pipeline.addHandler(ServerHandler())
_ = try! serverChannel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: .init(),
streamConfiguration: .init()
) { channel in
channel.pipeline.addHandler(ServerHandler())
}.wait()
}
}
private func testRun(identifier: String, dataBlockCount: Int = 0, dataBlockLengthBytes: Int = 0, clientPipelineConfigurator: (Channel) throws -> MultiplexerChannelCreator, serverPipelineConfigurator: (Channel) throws -> ()) {
private func testRun(
identifier: String,
dataBlockCount: Int = 0,
dataBlockLengthBytes: Int = 0,
clientPipelineConfigurator: (Channel) throws -> MultiplexerChannelCreator,
serverPipelineConfigurator: (Channel) throws -> Void
) {
let loop = EmbeddedEventLoop()
measure(identifier: identifier) {
@@ -139,7 +170,9 @@ private func testRun(identifier: String, dataBlockCount: Int = 0, dataBlockLengt
let promise = clientChannel.eventLoop.makePromise(of: Channel.self)
clientMultiplexer.createStreamChannel(promise: promise) { channel in
return channel.pipeline.addHandler(ClientHandler(dataBlockCount: dataBlockCount, dataBlockLengthBytes: dataBlockLengthBytes))
channel.pipeline.addHandler(
ClientHandler(dataBlockCount: dataBlockCount, dataBlockLengthBytes: dataBlockLengthBytes)
)
}
clientChannel.embeddedEventLoop.run()
let child = try! promise.futureResult.wait()
@@ -156,4 +189,3 @@ private func testRun(identifier: String, dataBlockCount: Int = 0, dataBlockLengt
return sumOfStreamIDs
}
}
@@ -18,14 +18,13 @@ import NIOHPACK
import NIOHTTP1
import NIOHTTP2
func run(identifier: String) {
testRun(identifier: identifier, usePromiseBasedAPI: true) { channel in
let http2Handler = NIOHTTP2Handler(mode: .client)
let multiplexer = HTTP2StreamMultiplexer(mode: .client, channel: channel, inboundStreamInitializer: nil)
try! channel.pipeline.addHandlers([
http2Handler,
multiplexer
multiplexer,
]).wait()
return multiplexer
}
@@ -35,7 +34,7 @@ func run(identifier: String) {
let multiplexer = HTTP2StreamMultiplexer(mode: .client, channel: channel, inboundStreamInitializer: nil)
try! channel.pipeline.addHandlers([
http2Handler,
multiplexer
multiplexer,
]).wait()
return multiplexer
}
@@ -43,7 +42,7 @@ func run(identifier: String) {
// MARK: - Inline HTTP2 multiplexer tests
testRun(identifier: identifier + "_inline", usePromiseBasedAPI: true) { channel in
let http2Handler = NIOHTTP2Handler(mode: .client, eventLoop: channel.eventLoop) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
channel.eventLoop.makeSucceededVoidFuture()
}
try! channel.pipeline.addHandler(http2Handler).wait()
return try! http2Handler.multiplexer.wait()
@@ -51,7 +50,7 @@ func run(identifier: String) {
testRun(identifier: identifier + "_inline_no_promise_based_API", usePromiseBasedAPI: false) { channel in
let http2Handler = NIOHTTP2Handler(mode: .client, eventLoop: channel.eventLoop) { channel in
return channel.eventLoop.makeSucceededVoidFuture()
channel.eventLoop.makeSucceededVoidFuture()
}
try! channel.pipeline.addHandler(http2Handler).wait()
return try! http2Handler.multiplexer.wait()
@@ -75,12 +74,12 @@ private func testRun(
if usePromiseBasedAPI {
let promise = channel.eventLoop.makePromise(of: Channel.self)
multiplexer.createStreamChannel(promise: promise) { channel in
return channel.eventLoop.makeSucceededFuture(())
channel.eventLoop.makeSucceededFuture(())
}
channelFuture = promise.futureResult
} else {
channelFuture = multiplexer.createStreamChannel { channel in
return channel.eventLoop.makeSucceededFuture(())
channel.eventLoop.makeSucceededFuture(())
}
}
channel.embeddedEventLoop.run()
@@ -13,18 +13,24 @@
//===----------------------------------------------------------------------===//
import NIOCore
import NIOHTTP2
import NIOHPACK
import NIOHTTP2
func run(identifier: String) {
var buffer = ByteBufferAllocator().buffer(capacity: 128)
buffer.writeBytes([0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d])
buffer.writeBytes([
0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
0x6f, 0x6d,
])
let request1 = buffer
buffer.clear()
buffer.writeBytes([0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65])
let request2 = buffer
buffer.clear()
buffer.writeBytes([0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65])
buffer.writeBytes([
0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63,
0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65,
])
let request3 = buffer
measure(identifier: identifier) {
@@ -12,11 +12,11 @@
//
//===----------------------------------------------------------------------===//
import Atomics
import NIOCore
import NIOEmbedded
import NIOHPACK
import NIOHTTP2
import Atomics
struct StreamTeardownBenchmark {
private let concurrentStreams: Int
@@ -28,9 +28,12 @@ struct StreamTeardownBenchmark {
headers.add(name: ":authority", value: "localhost", indexing: .nonIndexable)
headers.add(name: ":path", value: "/", indexing: .indexable)
headers.add(name: ":scheme", value: "https", indexing: .indexable)
headers.add(name: "user-agent",
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable)
headers.add(
name: "user-agent",
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable
)
headers.add(name: "accept-encoding", value: "gzip, deflate", indexing: .indexable)
var hpackEncoder = HPACKEncoder(allocator: .init())
@@ -88,7 +91,7 @@ struct StreamTeardownBenchmark {
self.concurrentStreams = concurrentStreams
}
private func createChannel(pipelineConfigurator: (Channel, Int) throws -> ()) throws -> EmbeddedChannel {
private func createChannel(pipelineConfigurator: (Channel, Int) throws -> Void) throws -> EmbeddedChannel {
let channel = EmbeddedChannel()
try! pipelineConfigurator(channel, self.concurrentStreams)
@@ -100,7 +103,7 @@ struct StreamTeardownBenchmark {
initialBytes.writeImmutableBuffer(self.settingsACK)
try channel.writeInbound(initialBytes)
while try channel.readOutbound(as: ByteBuffer.self) != nil { }
while try channel.readOutbound(as: ByteBuffer.self) != nil {}
return channel
}
@@ -109,7 +112,7 @@ struct StreamTeardownBenchmark {
_ = try channel.finish()
}
fileprivate mutating func run(pipelineConfigurator: (Channel, Int) throws -> ()) throws -> Int {
fileprivate mutating func run(pipelineConfigurator: (Channel, Int) throws -> Void) throws -> Int {
var bodyByteCount = 0
var completedIterations = 0
while completedIterations < 10_000 {
@@ -121,10 +124,13 @@ struct StreamTeardownBenchmark {
return bodyByteCount
}
private mutating func sendInterleavedRequestsAndTerminate(_ interleavedRequests: Int, _ channel: EmbeddedChannel) throws -> Int {
private mutating func sendInterleavedRequestsAndTerminate(
_ interleavedRequests: Int,
_ channel: EmbeddedChannel
) throws -> Int {
var streamID = HTTP2StreamID(1)
for _ in 0 ..< interleavedRequests {
for _ in 0..<interleavedRequests {
self.headersFrame.setInteger(UInt32(Int32(streamID)), at: self.headersFrame.readerIndex + 5)
try channel.writeInbound(self.headersFrame)
streamID = streamID.advanced(by: 2)
@@ -142,17 +148,18 @@ struct StreamTeardownBenchmark {
}
}
fileprivate class DoNothingServer: ChannelInboundHandler {
private class DoNothingServer: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame.FramePayload
public typealias OutboundOut = HTTP2Frame.FramePayload
}
fileprivate class SendGoawayHandler: ChannelInboundHandler {
private class SendGoawayHandler: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame
public typealias OutboundOut = HTTP2Frame
private static let goawayFrame: HTTP2Frame = HTTP2Frame(
streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .enhanceYourCalm, opaqueData: nil)
streamID: .rootStream,
payload: .goAway(lastStreamID: .rootStream, errorCode: .enhanceYourCalm, opaqueData: nil)
)
private let expectedStreams: Int
@@ -174,9 +181,10 @@ fileprivate class SendGoawayHandler: ChannelInboundHandler {
}
}
fileprivate class SendGoawayDelegate {
private class SendGoawayDelegate {
private static let goawayFrame: HTTP2Frame = HTTP2Frame(
streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .enhanceYourCalm, opaqueData: nil)
streamID: .rootStream,
payload: .goAway(lastStreamID: .rootStream, errorCode: .enhanceYourCalm, opaqueData: nil)
)
private let expectedStreams: Int
@@ -206,25 +214,24 @@ func run(identifier: String) {
var benchmark = StreamTeardownBenchmark(concurrentStreams: 100)
measure(identifier: identifier) {
return try! benchmark.run() { channel, concurrentStreams in
try! benchmark.run { channel, concurrentStreams in
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(DoNothingServer())
streamChannel.pipeline.addHandler(DoNothingServer())
}.wait()
try channel.pipeline.addHandler(SendGoawayHandler(expectedStreams: concurrentStreams)).wait()
}
}
measure(identifier: identifier + "_inline") {
return try! benchmark.run() { channel, concurrentStreams in
try! benchmark.run { channel, concurrentStreams in
_ = try channel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: .init(),
streamConfiguration: .init(),
streamDelegate: SendGoawayDelegate(expectedStreams: concurrentStreams)
) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(DoNothingServer())
streamChannel.pipeline.addHandler(DoNothingServer())
}.wait()
}
}
}
+12 -7
View File
@@ -18,7 +18,7 @@ import PackageDescription
let package = Package(
name: "swift-nio-http2",
products: [
.library(name: "NIOHTTP2", targets: ["NIOHTTP2"]),
.library(name: "NIOHTTP2", targets: ["NIOHTTP2"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.60.0"),
@@ -32,7 +32,8 @@ let package = Package(
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
]),
]
),
.executableTarget(
name: "NIOHTTP2PerformanceTester",
dependencies: [
@@ -40,7 +41,8 @@ let package = Package(
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
]),
]
),
.target(
name: "NIOHTTP2",
dependencies: [
@@ -51,7 +53,8 @@ let package = Package(
.product(name: "NIOTLS", package: "swift-nio"),
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
.product(name: "Atomics", package: "swift-atomics"),
]),
]
),
.target(
name: "NIOHPACK",
dependencies: [
@@ -59,7 +62,8 @@ let package = Package(
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
]),
]
),
.testTarget(
name: "NIOHTTP2Tests",
dependencies: [
@@ -70,7 +74,8 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOFoundationCompat", package: "swift-nio"),
.product(name: "Atomics", package: "swift-atomics"),
]),
]
),
.testTarget(
name: "NIOHPACKTests",
dependencies: [
@@ -82,6 +87,6 @@ let package = Package(
.copy("Fixtures/large_complex_huffman_b64.txt"),
.copy("Fixtures/large_huffman_b64.txt"),
]
)
),
]
)
+19 -18
View File
@@ -19,15 +19,15 @@ import NIOCore
@usableFromInline
struct DynamicHeaderTable: Sendable {
public static let defaultSize = 4096
/// The actual table, with items looked up by index.
private var storage: HeaderTableStorage
/// The length of the contents of the table.
var length: Int {
return self.storage.length
self.storage.length
}
/// The size to which the dynamic table may currently grow. Represents
/// the current maximum length signaled by the peer via a table-resize
/// value at the start of an encoded header block.
@@ -35,13 +35,13 @@ struct DynamicHeaderTable: Sendable {
/// - note: This value cannot exceed `self.maximumTableLength`.
var allowedLength: Int {
get {
return self.storage.maxSize
self.storage.maxSize
}
set {
self.storage.setTableSize(to: newValue)
}
}
/// The maximum permitted size of the dynamic header table as set
/// through a SETTINGS_HEADER_TABLE_SIZE value in a SETTINGS frame.
var maximumTableLength: Int {
@@ -51,33 +51,34 @@ struct DynamicHeaderTable: Sendable {
}
}
}
/// The number of items in the table.
var count: Int {
return self.storage.count
self.storage.count
}
init(maximumLength: Int = DynamicHeaderTable.defaultSize) {
self.storage = HeaderTableStorage(maxSize: maximumLength)
self.maximumTableLength = maximumLength
self.allowedLength = maximumLength // until we're told otherwise, this is what we assume the other side expects.
// until we're told otherwise, this is what we assume the other side expects.
self.allowedLength = maximumLength
}
/// Subscripts into the dynamic table alone, using a zero-based index.
subscript(i: Int) -> HeaderTableEntry {
return self.storage[i]
self.storage[i]
}
// internal for testing
func dumpHeaders() -> String {
return self.storage.dumpHeaders(offsetBy: StaticHeaderTable.count)
self.storage.dumpHeaders(offsetBy: StaticHeaderTable.count)
}
// internal for testing -- clears the dynamic table
mutating func clear() {
self.storage.purge(toRelease: self.storage.length)
}
/// Searches the table for a matching header, optionally with a particular value. If
/// a match is found, returns the index of the item and an indication whether it contained
/// the matching value as well.
@@ -97,7 +98,7 @@ struct DynamicHeaderTable: Sendable {
// no `first` on AnySequence, just `first(where:)`
return self.storage.firstIndex(matching: name).map { ($0, false) }
}
// If we have a value, locate the index of the lowest header which contains that
// value, but if no value matches, return the index of the lowest header with a
// matching name alone.
@@ -110,7 +111,7 @@ struct DynamicHeaderTable: Sendable {
return nil
}
}
/// Appends a header to the table. Note that if this succeeds, the new item's index
/// is always zero.
///
+59 -35
View File
@@ -27,7 +27,7 @@ import NIOCore
/// by a single HTTP/2 stream, operating on a single NIO `EventLoop`.
public struct HPACKDecoder: Sendable {
public static var maxDynamicTableSize: Int {
return DynamicHeaderTable.defaultSize
DynamicHeaderTable.defaultSize
}
/// The default value of the maximum header list size for the decoder.
@@ -35,20 +35,20 @@ public struct HPACKDecoder: Sendable {
/// This value is somewhat arbitrary, but 16kB should be sufficiently large to decode all reasonably
/// sized header lists.
public static var defaultMaxHeaderListSize: Int {
return 1<<14
1 << 14
}
// private but tests
var headerTable: IndexedHeaderTable
var dynamicTableLength: Int {
return headerTable.dynamicTableLength
headerTable.dynamicTableLength
}
/// The current allowed length of the dynamic portion of the header table. May be
/// less than the current protocol-assigned maximum supplied by a SETTINGS frame.
public private(set) var allowedDynamicTableLength: Int {
get { return self.headerTable.dynamicTableAllowedLength }
get { self.headerTable.dynamicTableAllowedLength }
set { self.headerTable.dynamicTableAllowedLength = newValue }
}
@@ -60,7 +60,7 @@ public struct HPACKDecoder: Sendable {
enum HPACKString {
case indexed(index: Int)
case literal
init(fromEncodedInteger value: Int) {
switch value {
case 0:
@@ -70,23 +70,27 @@ public struct HPACKDecoder: Sendable {
}
}
}
/// The maximum size of the dynamic table as set by the enclosing protocol. This is defined in RFC 7541
/// to be the sum of [name-octet-count] + [value-octet-count] + 32 for
/// each header it contains.
public var maxDynamicTableLength: Int {
get { return headerTable.maxDynamicTableLength }
/* private but tests */
get { headerTable.maxDynamicTableLength }
// private but tests
set { headerTable.maxDynamicTableLength = newValue }
}
/// Creates a new decoder
///
/// - Parameters:
/// - allocator: Allocator for headers view byte buffer (which is deprecated)
/// - maxDynamicTableSize: Maximum allowed size of the dynamic header table.
public init(allocator: ByteBufferAllocator, maxDynamicTableSize: Int = HPACKDecoder.maxDynamicTableSize) {
self.init(allocator: allocator, maxDynamicTableSize: maxDynamicTableSize, maxHeaderListSize: HPACKDecoder.defaultMaxHeaderListSize)
self.init(
allocator: allocator,
maxDynamicTableSize: maxDynamicTableSize,
maxHeaderListSize: HPACKDecoder.defaultMaxHeaderListSize
)
}
/// Creates a new decoder
@@ -100,7 +104,7 @@ public struct HPACKDecoder: Sendable {
self.headerTable = IndexedHeaderTable(allocator: allocator, maxDynamicTableSize: maxDynamicTableSize)
self.maxHeaderListSize = maxHeaderListSize
}
/// Reads HPACK encoded header data from a `ByteBuffer`.
///
/// It is expected that the buffer cover only the bytes of the header list, i.e.
@@ -169,59 +173,79 @@ public struct HPACKDecoder: Sendable {
throw NIOHPACKErrors.ZeroHeaderIndex()
}
return try .header(self.decodeIndexedHeader(from: Int(hidx)))
case let x where x & 0b1100_0000 == 0b0100_0000:
// 0b01xxxxxx -- two-bit prefix, six bits of value
// literal header with possibly-indexed name
let hidx = try buffer.readEncodedInteger(withPrefix: 6)
return try .header(self.decodeLiteralHeader(from: &buffer, headerName: HPACKString(fromEncodedInteger: hidx), addToIndex: true))
return try .header(
self.decodeLiteralHeader(
from: &buffer,
headerName: HPACKString(fromEncodedInteger: hidx),
addToIndex: true
)
)
case let x where x & 0b1111_0000 == 0b0000_0000:
// 0b0000xxxx -- four-bit prefix, four bits of value
// literal header with possibly-indexed name, not added to dynamic table
let hidx = try buffer.readEncodedInteger(withPrefix: 4)
var header = try self.decodeLiteralHeader(from: &buffer, headerName: HPACKString(fromEncodedInteger: hidx), addToIndex: false)
var header = try self.decodeLiteralHeader(
from: &buffer,
headerName: HPACKString(fromEncodedInteger: hidx),
addToIndex: false
)
header.indexing = .nonIndexable
return .header(header)
case let x where x & 0b1111_0000 == 0b0001_0000:
// 0b0001xxxx -- four-bit prefix, four bits of value
// literal header with possibly-indexed name, never added to dynamic table nor
// rewritten by proxies
let hidx = try buffer.readEncodedInteger(withPrefix: 4)
var header = try self.decodeLiteralHeader(from: &buffer, headerName: HPACKString(fromEncodedInteger: hidx), addToIndex: false)
var header = try self.decodeLiteralHeader(
from: &buffer,
headerName: HPACKString(fromEncodedInteger: hidx),
addToIndex: false
)
header.indexing = .neverIndexed
return .header(header)
case let x where x & 0b1110_0000 == 0b0010_0000:
// 0b001xxxxx -- three-bit prefix, five bits of value
// dynamic header table size update
let newMaxLength = try Int(buffer.readEncodedInteger(withPrefix: 5))
// RFC 7541 § 4.2 <https://httpwg.org/specs/rfc7541.html#maximum.table.size>:
//
// 1. "the chosen size MUST stay lower than or equal to the maximum set by the protocol."
guard newMaxLength <= self.headerTable.maxDynamicTableLength else {
throw NIOHPACKErrors.InvalidDynamicTableSize(requestedSize: newMaxLength, allowedSize: self.headerTable.maxDynamicTableLength)
throw NIOHPACKErrors.InvalidDynamicTableSize(
requestedSize: newMaxLength,
allowedSize: self.headerTable.maxDynamicTableLength
)
}
self.allowedDynamicTableLength = newMaxLength
return .tableSizeChange
default:
throw NIOHPACKErrors.InvalidHeaderStartByte(byte: initial)
}
}
private mutating func decodeIndexedHeader(from hidx: Int) throws -> HPACKHeader {
let (name, value) = try self.headerTable.header(at: hidx)
return HPACKHeader(name: name, value: value)
}
private mutating func decodeLiteralHeader(from buffer: inout ByteBuffer, headerName: HPACKString,
addToIndex: Bool) throws -> HPACKHeader {
private mutating func decodeLiteralHeader(
from buffer: inout ByteBuffer,
headerName: HPACKString,
addToIndex: Bool
) throws -> HPACKHeader {
let name: String
switch headerName {
case .indexed(let idx):
(name, _) = try self.headerTable.header(at: idx)
@@ -232,23 +256,23 @@ public struct HPACKDecoder: Sendable {
throw NIOHPACKErrors.EmptyLiteralHeaderFieldName()
}
}
let value = try self.readEncodedString(from: &buffer)
if addToIndex {
try headerTable.add(headerNamed: name, value: value)
}
return HPACKHeader(name: name, value: value)
}
private mutating func readEncodedString(from buffer: inout ByteBuffer) throws -> String {
// peek to read the encoding bit
guard let initialByte: UInt8 = buffer.getInteger(at: buffer.readerIndex) else {
throw NIOHPACKErrors.InsufficientInput()
}
let huffmanEncoded = initialByte & 0x80 == 0x80
// read the length; there's a seven-bit prefix here (one-bit encoding flag)
let len = try Int(buffer.readEncodedInteger(withPrefix: 7))
guard len <= buffer.readableBytes else {
@@ -259,7 +283,7 @@ public struct HPACKDecoder: Sendable {
return try buffer.readHuffmanEncodedString(length: len)
} else {
// This force-unwrap is safe as we have checked above that len is less than or equal to the buffer readable bytes.
return buffer.readString(length: len)!
return buffer.readString(length: len)!
}
}
}
+52 -39
View File
@@ -24,53 +24,58 @@ import NIOCore
/// RFC 7541, appending and evicting items as described there.
public struct HPACKEncoder {
/// The default size of the encoder's dynamic header table.
public static var defaultDynamicTableSize: Int { return DynamicHeaderTable.defaultSize }
public static var defaultDynamicTableSize: Int { DynamicHeaderTable.defaultSize }
private static let defaultDataBufferSize = 128
public struct HeaderDefinition: Sendable {
var name: String
var value: String
var indexing: HPACKIndexing
}
private enum EncoderState {
case idle
case resized(smallestMaxTableSize: Int?)
case encoding
}
// private but tests
var headerIndexTable: IndexedHeaderTable
private var state: EncoderState
private var buffer: ByteBuffer
/// Whether to use Huffman encoding.
public let useHuffmanEncoding: Bool
/// The current size of the dynamic table.
///
/// This is defined as the sum of [name] + [value] + 32 for each header.
public var dynamicTableSize: Int {
return self.headerIndexTable.dynamicTableLength
self.headerIndexTable.dynamicTableLength
}
/// The current maximum size to which the dynamic header table may grow.
public private(set) var allowedDynamicTableSize: Int {
get { return self.headerIndexTable.dynamicTableAllowedLength }
get { self.headerIndexTable.dynamicTableAllowedLength }
set { self.headerIndexTable.dynamicTableAllowedLength = newValue }
}
/// The hard maximum size of the dynamic header table, set via an HTTP/2
/// SETTINGS frame.
public var maximumDynamicTableSize: Int {
get { return self.headerIndexTable.maxDynamicTableLength }
get { self.headerIndexTable.maxDynamicTableLength }
// This should have been private, but we messed up. Nevermind.
@available(*, deprecated, message: "Setting the dynamic table length on the HPACK encoder doesn't have the intended effect.", renamed: "setDynamicTableSize(_:)")
@available(
*,
deprecated,
message: "Setting the dynamic table length on the HPACK encoder doesn't have the intended effect.",
renamed: "setDynamicTableSize(_:)"
)
set { self.headerIndexTable.maxDynamicTableLength = newValue }
}
/// Sets the maximum size for the dynamic table and encodes the new value
/// at the start of the current packed header block to send to the peer.
///
@@ -82,7 +87,7 @@ public struct HPACKEncoder {
// no need to change anything
return
}
switch self.state {
case .idle:
self.state = .resized(smallestMaxTableSize: nil)
@@ -96,19 +101,23 @@ public struct HPACKEncoder {
// we don't need to change smallest recorded resize value
break
}
// set the new size on our dynamic table
self.headerIndexTable.maxDynamicTableLength = size
self.allowedDynamicTableSize = size
}
/// Initializer and returns a new HPACK encoder.
///
/// - Parameters:
/// - allocator: An allocator for `ByteBuffer`s.
/// - useHuffmanEncoding: Should huffman enccoding be used.
/// - maxDynamicTableSize: An initial maximum size for the encoder's dynamic header table.
public init(allocator: ByteBufferAllocator, useHuffmanEncoding: Bool = true, maxDynamicTableSize: Int = HPACKEncoder.defaultDynamicTableSize) {
public init(
allocator: ByteBufferAllocator,
useHuffmanEncoding: Bool = true,
maxDynamicTableSize: Int = HPACKEncoder.defaultDynamicTableSize
) {
self.headerIndexTable = IndexedHeaderTable(allocator: allocator, maxDynamicTableSize: maxDynamicTableSize)
self.useHuffmanEncoding = useHuffmanEncoding
self.state = .idle
@@ -117,7 +126,7 @@ public struct HPACKEncoder {
// mode. I want to remove it: see https://github.com/apple/swift-nio-http2/issues/85.
self.buffer = allocator.buffer(capacity: 128)
}
/// Sets up the encoder to begin encoding a new header block.
///
/// - Parameter allocator: Used to allocate the `ByteBuffer` that will contain the encoded
@@ -145,17 +154,17 @@ public struct HPACKEncoder {
break
}
}
/// Finishes encoding the current header block and returns the resulting buffer.
public mutating func endEncoding() throws -> ByteBuffer {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
self.state = .idle
return self.buffer
}
/// A one-shot encoder that writes to a provided buffer.
///
/// In general this encoding mechanism is more efficient than the incremental one.
@@ -188,22 +197,22 @@ public struct HPACKEncoder {
try self.append(headers: headers)
}
/// Appends() headers in the default fashion: indexed if possible, literal+indexable if not.
///
/// - Parameter headers: A sequence of key/value pairs representing HTTP headers.
public mutating func append<S: Sequence>(headers: S) throws where S.Element == (name: String, value: String) {
try self.append(headers: headers.lazy.map { HeaderDefinition(name: $0.0, value: $0.1, indexing: .indexable) })
}
/// Appends headers with their specified indexability.
///
/// - Parameter headers: A sequence of key/value/indexability tuples representing HTTP headers.
public mutating func append<S : Sequence>(headers: S) throws where S.Element == HeaderDefinition {
public mutating func append<S: Sequence>(headers: S) throws where S.Element == HeaderDefinition {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
for header in headers {
switch header.indexing {
case .indexable:
@@ -215,7 +224,7 @@ public struct HPACKEncoder {
}
}
}
/// Appends a set of headers with their associated indexability.
///
/// - Parameter headers: A `HPACKHeaders` structure containing a set of HTTP/2 header values.
@@ -223,7 +232,7 @@ public struct HPACKEncoder {
guard case .encoding = self.state else {
throw NIOHPACKErrors.EncoderNotStarted()
}
for header in headers {
switch header.indexable {
case .indexable:
@@ -235,7 +244,7 @@ public struct HPACKEncoder {
}
}
}
/// Appends a header/value pair, using indexed names/values if possible. If no indexed pair is available,
/// it will use an indexed header and literal value, or a literal header and value. The name/value pair
/// will be indexed for future use.
@@ -245,7 +254,7 @@ public struct HPACKEncoder {
}
try self._appendIndexed(header: name, value: value)
}
/// Returns `true` if the item needs to be added to the header table
private mutating func _appendIndexed(header name: String, value: String) throws {
if let (index, hasValue) = self.headerIndexTable.firstHeaderMatch(for: name, value: value) {
@@ -267,11 +276,11 @@ public struct HPACKEncoder {
self.appendEncodedString(name)
self.appendEncodedString(value)
}
// add to the header table
try self.headerIndexTable.add(headerNamed: name, value: value)
}
private mutating func appendEncodedString(_ string: String) {
let utf8 = string.utf8
@@ -279,14 +288,18 @@ public struct HPACKEncoder {
if self.useHuffmanEncoding {
// problem: we need to encode the length before the encoded bytes, so we can't just receive the length
// after encoding to the target buffer itself. So we have to determine the length first.
self.buffer.write(encodedInteger: UInt64(ByteBuffer.huffmanEncodedLength(of: utf8)), prefix: 7, prefixBits: 0x80)
self.buffer.write(
encodedInteger: UInt64(ByteBuffer.huffmanEncodedLength(of: utf8)),
prefix: 7,
prefixBits: 0x80
)
self.buffer.writeHuffmanEncoded(bytes: utf8)
} else {
self.buffer.write(encodedInteger: UInt64(utf8.count), prefix: 7, prefixBits: 0)
self.buffer.writeBytes(utf8)
}
}
/// Appends a header that is *not* to be entered into the dynamic header table, but allows that
/// stipulation to be overriden by a proxy server/rewriter.
public mutating func appendNonIndexed(header: String, value: String) throws {
@@ -295,7 +308,7 @@ public struct HPACKEncoder {
}
self._appendNonIndexed(header: header, value: value)
}
private mutating func _appendNonIndexed(header: String, value: String) {
if let (index, _) = self.headerIndexTable.firstHeaderMatch(for: header, value: nil) {
// we actually don't care if it has a value in this instance; we're only indexing the
@@ -304,12 +317,12 @@ public struct HPACKEncoder {
// now append the value
self.appendEncodedString(value)
} else {
self.buffer.writeInteger(UInt8(0)) // top 4 bits are zero, and index is zero (no index)
self.buffer.writeInteger(UInt8(0)) // top 4 bits are zero, and index is zero (no index)
self.appendEncodedString(header)
self.appendEncodedString(value)
}
}
/// Appends a header that is *never* indexed, preventing even rewriting proxies from doing so.
public mutating func appendNeverIndexed(header: String, value: String) throws {
guard case .encoding = self.state else {
@@ -317,7 +330,7 @@ public struct HPACKEncoder {
}
self._appendNeverIndexed(header: header, value: value)
}
private mutating func _appendNeverIndexed(header: String, value: String) {
if let (index, _) = self.headerIndexTable.firstHeaderMatch(for: header, value: nil) {
// we only use the index in this instance
@@ -325,7 +338,7 @@ public struct HPACKEncoder {
// now append the value
self.appendEncodedString(value)
} else {
self.buffer.writeInteger(UInt8(0x10)) // prefix bits + zero index
self.buffer.writeInteger(UInt8(0x10)) // prefix bits + zero index
self.appendEncodedString(header)
self.appendEncodedString(value)
}
+20 -16
View File
@@ -14,13 +14,13 @@
import NIOCore
public protocol NIOHPACKError : Error, Equatable { }
public protocol NIOHPACKError: Error, Equatable {}
/// Errors raised by NIOHPACK while encoding/decoding data.
public enum NIOHPACKErrors {
/// An indexed header referenced an index that doesn't exist in our
/// header tables.
public struct InvalidHeaderIndex : NIOHPACKError {
public struct InvalidHeaderIndex: NIOHPACKError {
/// The offending index.
public let suppliedIndex: Int
@@ -31,14 +31,14 @@ public enum NIOHPACKErrors {
/// A header block indicated an indexed header with no accompanying
/// value, but the index referenced an entry with no value of its own
/// e.g. one of the many valueless items in the static header table.
public struct IndexedHeaderWithNoValue : NIOHPACKError {
public struct IndexedHeaderWithNoValue: NIOHPACKError {
/// The offending index.
public let index: Int
}
/// An encoded string contained an invalid length that extended
/// beyond its frame's payload size.
public struct StringLengthBeyondPayloadSize : NIOHPACKError {
public struct StringLengthBeyondPayloadSize: NIOHPACKError {
/// The length supplied.
public let length: Int
@@ -47,20 +47,20 @@ public enum NIOHPACKErrors {
}
/// Decoded string data could not be parsed as valid UTF-8.
public struct InvalidUTF8Data : NIOHPACKError {
public struct InvalidUTF8Data: NIOHPACKError {
/// The offending bytes.
public let bytes: ByteBuffer
}
/// The start byte of a header did not match any format allowed by
/// the HPACK specification.
public struct InvalidHeaderStartByte : NIOHPACKError {
public struct InvalidHeaderStartByte: NIOHPACKError {
/// The offending byte.
public let byte: UInt8
}
/// A dynamic table size update specified an invalid size.
public struct InvalidDynamicTableSize : NIOHPACKError {
public struct InvalidDynamicTableSize: NIOHPACKError {
/// The offending size.
public let requestedSize: Int
@@ -70,11 +70,12 @@ public enum NIOHPACKErrors {
/// A dynamic table size update was found outside its allowed place.
/// They may only be included at the start of a header block.
public struct IllegalDynamicTableSizeChange : NIOHPACKError {}
public struct IllegalDynamicTableSizeChange: NIOHPACKError {}
/// A new header could not be added to the dynamic table. Usually
/// this means the header itself is larger than the current
/// dynamic table size.
public struct FailedToAddIndexedHeader<Name: Collection & Sendable, Value: Collection & Sendable> : NIOHPACKError where Name.Element == UInt8, Value.Element == UInt8 {
public struct FailedToAddIndexedHeader<Name: Collection & Sendable, Value: Collection & Sendable>: NIOHPACKError
where Name.Element == UInt8, Value.Element == UInt8 {
/// The table size required to be able to add this header to the table.
public let bytesNeeded: Int
@@ -84,7 +85,10 @@ public enum NIOHPACKErrors {
/// The value of the header that could not be written.
public let value: Value
public static func == (lhs: NIOHPACKErrors.FailedToAddIndexedHeader<Name, Value>, rhs: NIOHPACKErrors.FailedToAddIndexedHeader<Name, Value>) -> Bool {
public static func == (
lhs: NIOHPACKErrors.FailedToAddIndexedHeader<Name, Value>,
rhs: NIOHPACKErrors.FailedToAddIndexedHeader<Name, Value>
) -> Bool {
guard lhs.bytesNeeded == rhs.bytesNeeded else {
return false
}
@@ -93,29 +97,29 @@ public enum NIOHPACKErrors {
}
/// Ran out of input bytes while decoding.
public struct InsufficientInput : NIOHPACKError {}
public struct InsufficientInput: NIOHPACKError {}
/// HPACK encoder asked to begin a new header block while partway through encoding
/// another block.
public struct EncoderAlreadyActive : NIOHPACKError {}
public struct EncoderAlreadyActive: NIOHPACKError {}
/// HPACK encoder asked to append a header without first calling `beginEncoding(allocator:)`.
public struct EncoderNotStarted : NIOHPACKError {}
public struct EncoderNotStarted: NIOHPACKError {}
/// HPACK decoder asked to decode an indexed header with index zero.
public struct ZeroHeaderIndex: NIOHPACKError {
public init() { }
public init() {}
}
/// HPACK decoder asked to decode a header list that would violate the configured
/// max header list size.
public struct MaxHeaderListSizeViolation: NIOHPACKError {
public init() { }
public init() {}
}
/// HPACK decoder asked to decode a header field name that was empty.
public struct EmptyLiteralHeaderFieldName: NIOHPACKError {
public init() { }
public init() {}
}
/// The integer being decoded is not representable by this implementation.
+53 -52
View File
@@ -39,8 +39,10 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
// see 8.1.2.2. Connection-Specific Header Fields in RFC 7540
@usableFromInline
static let illegalHeaders: [String] = ["connection", "keep-alive", "proxy-connection",
"transfer-encoding", "upgrade"]
static let illegalNames: [String] = [
"connection", "keep-alive", "proxy-connection",
"transfer-encoding", "upgrade",
]
/// Constructor that can be used to map between `HTTPHeaders` and `HPACKHeaders` types.
@inlinable
@@ -55,14 +57,14 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
if connectionHeaderValue.count > Self.connectionHeaderValueArraySizeLimit {
var headersToRemove = Set(connectionHeaderValue)
// Since we have a set we can just merge in the illegal headers.
headersToRemove.formUnion(Self.illegalHeaders.lazy.map { $0[...] })
headersToRemove.formUnion(Self.illegalNames.lazy.map { $0[...] })
self.headers.removeAll { header in
headersToRemove.contains(header.name[...])
}
} else {
self.headers.removeAll { header in
connectionHeaderValue.contains(header.name[...]) ||
HPACKHeaders.illegalHeaders.contains(header.name)
connectionHeaderValue.contains(header.name[...])
|| HPACKHeaders.illegalNames.contains(header.name)
}
}
} else {
@@ -153,7 +155,8 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
/// - indexing: The types of indexing and rewriting operations a decoder may take with
/// regard to this header.
@inlinable
public mutating func add<S: Sequence>(contentsOf other: S, indexing: HPACKIndexing = .indexable) where S.Element == (String, String) {
public mutating func add<S: Sequence>(contentsOf other: S, indexing: HPACKIndexing = .indexable)
where S.Element == (String, String) {
self.reserveCapacity(self.headers.count + other.underestimatedCount)
for (name, value) in other {
self.add(name: name, value: value, indexing: indexing)
@@ -205,7 +208,7 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
@inlinable
public mutating func remove(name nameToRemove: String) {
self.headers.removeAll { header in
return nameToRemove.isEqualCaseInsensitiveASCIIBytes(to: header.name)
nameToRemove.isEqualCaseInsensitiveASCIIBytes(to: header.name)
}
}
@@ -222,7 +225,7 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
/// - Returns: A list of the values for that header field name.
@inlinable
public subscript(name: String) -> [String] {
return self._values(forHeader: name, canonicalForm: false)
self._values(forHeader: name, canonicalForm: false)
}
/// Retrieves the first value for a given header field name from the block.
@@ -273,7 +276,7 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
/// - Returns: A list of the values for that header field name.
@inlinable
public subscript(canonicalForm name: String) -> [String] {
return self._values(forHeader: name, canonicalForm: true)
self._values(forHeader: name, canonicalForm: true)
}
/// Return a sequence of header values for the header field named `name`.
@@ -298,7 +301,10 @@ public struct HPACKHeaders: ExpressibleByDictionaryLiteral, Sendable {
/// Special internal function for use by tests.
internal subscript(position: Int) -> (String, String) {
precondition(position < self.headers.endIndex, "Position \(position) is beyond bounds of \(self.headers.endIndex)")
precondition(
position < self.headers.endIndex,
"Position \(position) is beyond bounds of \(self.headers.endIndex)"
)
let header = self.headers[position]
return (header.name, header.value)
}
@@ -341,7 +347,7 @@ extension HPACKHeaders {
}
public func makeIterator() -> Iterator {
return Iterator(headers: self.headers, name: self.name, canonicalize: self.canonicalize)
Iterator(headers: self.headers, name: self.name, canonicalize: self.canonicalize)
}
}
}
@@ -423,7 +429,7 @@ extension HPACKHeaders {
/// The total number of headers that can be contained without allocating new storage.
@inlinable
public var capacity: Int {
return self.headers.capacity
self.headers.capacity
}
/// Reserves enough space to store the specified number of headers.
@@ -458,28 +464,28 @@ extension HPACKHeaders: RandomAccessCollection {
@inlinable
public static func < (lhs: Index, rhs: Index) -> Bool {
return lhs._base < rhs._base
lhs._base < rhs._base
}
}
@inlinable
public var startIndex: HPACKHeaders.Index {
return .init(_base: self.headers.startIndex)
.init(_base: self.headers.startIndex)
}
@inlinable
public var endIndex: HPACKHeaders.Index {
return .init(_base: self.headers.endIndex)
.init(_base: self.headers.endIndex)
}
@inlinable
public func index(before i: HPACKHeaders.Index) -> HPACKHeaders.Index {
return .init(_base: self.headers.index(before: i._base))
.init(_base: self.headers.index(before: i._base))
}
@inlinable
public func index(after i: HPACKHeaders.Index) -> HPACKHeaders.Index {
return .init(_base: self.headers.index(after: i._base))
.init(_base: self.headers.index(after: i._base))
}
@inlinable
@@ -517,11 +523,10 @@ extension HPACKHeaders: RandomAccessCollection {
@inlinable
public func makeIterator() -> HPACKHeaders.Iterator {
return Iterator(self)
Iterator(self)
}
}
extension HPACKHeaders: CustomStringConvertible {
public var description: String {
var headersArray: [(HPACKIndexing, String, String)] = []
@@ -539,7 +544,7 @@ extension HPACKHeaders: CustomStringConvertible {
// More discussion at https://github.com/apple/swift-nio-http2/issues/342.
extension HPACKHeaders: Equatable {
@inlinable
public static func ==(lhs: HPACKHeaders, rhs: HPACKHeaders) -> Bool {
public static func == (lhs: HPACKHeaders, rhs: HPACKHeaders) -> Bool {
guard lhs.headers.count == rhs.headers.count else {
return false
}
@@ -619,7 +624,6 @@ internal struct HPACKHeader: Sendable {
}
}
extension HPACKHeader {
@inlinable
internal var size: Int {
@@ -630,25 +634,24 @@ extension HPACKHeader {
//
// The size of an entry is calculated using the length of its name and value
// without any Huffman encoding applied.
return name.utf8.count + value.utf8.count + 32
name.utf8.count + value.utf8.count + 32
}
}
internal extension UInt8 {
extension UInt8 {
@inlinable
var isASCII: Bool {
return self <= 127
self <= 127
}
}
/* private but inlinable */
internal extension UTF8.CodeUnit {
// private but inlinable
extension UTF8.CodeUnit {
@inlinable
var isASCIIWhitespace: Bool {
switch self {
case UInt8(ascii: " "),
UInt8(ascii: "\t"):
UInt8(ascii: "\t"):
return true
default:
return false
@@ -660,7 +663,7 @@ extension Substring.UTF8View {
@inlinable
func _trimWhitespace() -> Substring.UTF8View {
guard let firstNonWhitespace = self.firstIndex(where: { !$0.isASCIIWhitespace }) else {
// The whole substring is ASCII whitespace.
// The whole substring is ASCII whitespace.
return Substring().utf8
}
@@ -673,7 +676,7 @@ extension Substring.UTF8View {
extension String.UTF8View {
@inlinable
func _lazySplit(separator: UTF8.CodeUnit) -> LazyUTF8ViewSplitSequence {
return LazyUTF8ViewSplitSequence(self, separator: separator)
LazyUTF8ViewSplitSequence(self, separator: separator)
}
@usableFromInline
@@ -691,7 +694,7 @@ extension String.UTF8View {
@inlinable
func makeIterator() -> Iterator {
return Iterator(self)
Iterator(self)
}
@usableFromInline
@@ -729,7 +732,6 @@ extension String.UTF8View {
}
}
extension String.UTF8View {
/// Compares two UTF8 strings as case insensitive ASCII bytes.
///
@@ -737,41 +739,40 @@ extension String.UTF8View {
/// - Returns: Whether the collection contains **EXACTLY** this array or no, but by ignoring case.
@inlinable
func compareCaseInsensitiveASCIIBytes(to other: String.UTF8View) -> Bool {
// fast path: we can get the underlying bytes of both
let maybeMaybeResult = self.withContiguousStorageIfAvailable { lhsBuffer -> Bool? in
other.withContiguousStorageIfAvailable { rhsBuffer in
if lhsBuffer.count != rhsBuffer.count {
// fast path: we can get the underlying bytes of both
let maybeMaybeResult = self.withContiguousStorageIfAvailable { lhsBuffer -> Bool? in
other.withContiguousStorageIfAvailable { rhsBuffer in
if lhsBuffer.count != rhsBuffer.count {
return false
}
for idx in 0..<lhsBuffer.count {
// let's hope this gets vectorised ;)
if lhsBuffer[idx] & 0xdf != rhsBuffer[idx] & 0xdf && lhsBuffer[idx].isASCII {
return false
}
for idx in 0 ..< lhsBuffer.count {
// let's hope this gets vectorised ;)
if lhsBuffer[idx] & 0xdf != rhsBuffer[idx] & 0xdf && lhsBuffer[idx].isASCII {
return false
}
}
return true
}
return true
}
}
if let maybeResult = maybeMaybeResult, let result = maybeResult {
return result
} else {
return self._compareCaseInsensitiveASCIIBytesSlowPath(to: other)
}
if let maybeResult = maybeMaybeResult, let result = maybeResult {
return result
} else {
return self._compareCaseInsensitiveASCIIBytesSlowPath(to: other)
}
}
@inlinable
@inline(never)
func _compareCaseInsensitiveASCIIBytesSlowPath(to other: String.UTF8View) -> Bool {
return self.elementsEqual(other, by: { return (($0 & 0xdf) == ($1 & 0xdf) && $0.isASCII) })
self.elementsEqual(other, by: { (($0 & 0xdf) == ($1 & 0xdf) && $0.isASCII) })
}
}
extension String {
@inlinable
internal func isEqualCaseInsensitiveASCIIBytes(to: String) -> Bool {
return self.utf8.compareCaseInsensitiveASCIIBytes(to: to.utf8)
self.utf8.compareCaseInsensitiveASCIIBytes(to: to.utf8)
}
}
+17 -19
View File
@@ -27,7 +27,7 @@ internal struct HeaderTableEntry: Sendable {
// The size of an entry is calculated using the length of its name and value
// without any Huffman encoding applied.
var length: Int {
return self.name.utf8.count + self.value.utf8.count + 32
self.name.utf8.count + self.value.utf8.count + 32
}
}
@@ -39,19 +39,19 @@ struct HeaderTableStorage {
static let defaultMaxSize = 4096
private var headers: CircularBuffer<HeaderTableEntry>
internal private(set) var maxSize: Int
internal private(set) var length: Int = 0
var count: Int {
return self.headers.count
self.headers.count
}
init(maxSize: Int = HeaderTableStorage.defaultMaxSize) {
self.maxSize = maxSize
self.headers = CircularBuffer(initialCapacity: self.maxSize / 64) // rough guess: 64 bytes per header
self.headers = CircularBuffer(initialCapacity: self.maxSize / 64) // rough guess: 64 bytes per header
}
init(staticHeaderList: [(String, String)]) {
self.headers = CircularBuffer(initialCapacity: staticHeaderList.count)
@@ -64,7 +64,7 @@ struct HeaderTableStorage {
self.maxSize = len
}
subscript(index: Int) -> HeaderTableEntry {
let baseIndex = self.headers.index(self.headers.startIndex, offsetBy: index)
return self.headers[baseIndex]
@@ -114,7 +114,7 @@ struct HeaderTableStorage {
return .none
}
}
func firstIndex(matching name: String) -> Int? {
for (idx, header) in self.headers.enumerated() {
if header.name.isEqualCaseInsensitiveASCIIBytes(to: name) {
@@ -123,7 +123,7 @@ struct HeaderTableStorage {
}
return nil
}
mutating func setTableSize(to newSize: Int) {
precondition(newSize >= 0)
if newSize < self.length {
@@ -135,7 +135,7 @@ struct HeaderTableStorage {
self.maxSize = newSize
}
mutating func add(name: String, value: String) {
let entry = HeaderTableEntry(name: name, value: value)
@@ -145,7 +145,6 @@ struct HeaderTableStorage {
newLength = self.length + entry.length
}
if newLength > self.maxSize {
// We can't free up enough space. This is not an error: RFC 7541 § 4.4 explicitly allows it.
// In this case, the append fails but the above purge is preserved.
@@ -155,7 +154,7 @@ struct HeaderTableStorage {
self.headers.prepend(entry)
self.length = newLength
}
/// Purges `toRelease` bytes from the table, where 'bytes' refers to the byte-count
/// of a table entry specified in RFC 7541: [name octets] + [value octets] + 32.
///
@@ -167,14 +166,14 @@ struct HeaderTableStorage {
self.length = 0
return
}
var available = self.maxSize - self.length
let needed = available + count
while available < needed && !self.headers.isEmpty {
available += self.purgeOne()
}
}
@discardableResult
private mutating func purgeOne() -> Int {
precondition(self.headers.isEmpty == false, "should not call purgeOne() unless we have something to purge")
@@ -184,16 +183,16 @@ struct HeaderTableStorage {
self.length -= entry.length
return entry.length
}
// internal for testing
func dumpHeaders(offsetBy amount: Int = 0) -> String {
return self.headers.enumerated().reduce("") {
self.headers.enumerated().reduce("") {
$0 + "\($1.0 + amount) - \($1.1.name) : \($1.1.value)\n"
}
}
}
extension HeaderTableStorage : CustomStringConvertible {
extension HeaderTableStorage: CustomStringConvertible {
@usableFromInline
var description: String {
var array: [(String, String)] = []
@@ -207,4 +206,3 @@ extension HeaderTableStorage : CustomStringConvertible {
// The `@unchecked` is needed because at the time of writing `NIOCore` didn't have `Sendable` support.
extension HeaderTableStorage: Sendable {}
+49 -38
View File
@@ -22,19 +22,19 @@ extension ByteBuffer {
var offset = 0
var remainingBits = 8
}
/// Returns the number of *bits* required to encode a given string.
fileprivate static func encodedBitLength<C : Collection>(of bytes: C) -> Int where C.Element == UInt8 {
fileprivate static func encodedBitLength<C: Collection>(of bytes: C) -> Int where C.Element == UInt8 {
let clen = bytes.reduce(0) { $0 + StaticHuffmanTable[Int($1)].nbits }
// round up to nearest multiple of 8 for EOS prefix
return (clen + 7) & ~7
}
/// Returns the number of bytes required to encode a given string.
static func huffmanEncodedLength<C : Collection>(of bytes: C) -> Int where C.Element == UInt8 {
return self.encodedBitLength(of: bytes) / 8
static func huffmanEncodedLength<C: Collection>(of bytes: C) -> Int where C.Element == UInt8 {
self.encodedBitLength(of: bytes) / 8
}
/// Encodes the given string to the buffer, using HPACK Huffman encoding.
///
/// - Parameter string: The string data to encode.
@@ -43,33 +43,37 @@ extension ByteBuffer {
mutating func setHuffmanEncoded<C: Collection>(bytes stringBytes: C) -> Int where C.Element == UInt8 {
let clen = ByteBuffer.encodedBitLength(of: stringBytes)
self.ensureBitsAvailable(clen)
return self.withUnsafeMutableWritableBytes { bytes in
var state = _EncoderState()
for ch in stringBytes {
ByteBuffer.appendSym_fast(StaticHuffmanTable[Int(ch)], &state, bytes: bytes)
}
if state.remainingBits > 0 && state.remainingBits < 8 {
// set all remaining bits of the last byte to 1
bytes[state.offset] |= UInt8(1 << state.remainingBits) - 1
state.offset += 1
state.remainingBits = (state.offset == bytes.count ? 0 : 8)
}
return state.offset
}
}
@discardableResult
mutating func writeHuffmanEncoded<C: Collection>(bytes stringBytes: C) -> Int where C.Element == UInt8 {
let written = self.setHuffmanEncoded(bytes: stringBytes)
self.moveWriterIndex(forwardBy: written)
return written
}
fileprivate static func appendSym_fast(_ sym: HuffmanTableEntry, _ state: inout _EncoderState, bytes: UnsafeMutableRawBufferPointer) {
fileprivate static func appendSym_fast(
_ sym: HuffmanTableEntry,
_ state: inout _EncoderState,
bytes: UnsafeMutableRawBufferPointer
) {
// will it fit as-is?
if sym.nbits == state.remainingBits {
bytes[state.offset] |= UInt8(sym.bits)
@@ -81,23 +85,23 @@ extension ByteBuffer {
state.remainingBits -= sym.nbits
} else {
var (code, nbits) = sym
nbits -= state.remainingBits
bytes[state.offset] |= UInt8(code >> nbits)
state.offset += 1
if nbits & 0x7 != 0 {
// align code to MSB
code <<= 8 - (nbits & 0x7)
}
// we can short-circuit if less than 8 bits are remaining
if nbits < 8 {
bytes[state.offset] = UInt8(truncatingIfNeeded: code)
state.remainingBits = 8 - nbits
return
}
// longer path for larger amounts
switch nbits {
case _ where nbits > 24:
@@ -117,7 +121,7 @@ extension ByteBuffer {
default:
break
}
if nbits == 8 {
bytes[state.offset] = UInt8(truncatingIfNeeded: code)
state.offset += 1
@@ -128,7 +132,7 @@ extension ByteBuffer {
}
}
}
fileprivate mutating func ensureBitsAvailable(_ bits: Int) {
let bytesNeeded = bits / 8
if bytesNeeded <= self.writableBytes {
@@ -138,13 +142,13 @@ extension ByteBuffer {
}
return
}
let neededToAdd = bytesNeeded - self.writableBytes
let newLength = self.capacity + neededToAdd
// reallocate to ensure we have the room we need
self.reserveCapacity(newLength)
// now zero all writable bytes that we expect to use
self.withUnsafeMutableWritableBytes { ptr in
ptr.copyBytes(from: repeatElement(0, count: bytesNeeded))
@@ -153,24 +157,23 @@ extension ByteBuffer {
}
/// Errors that may be encountered by the Huffman decoder.
public enum HuffmanDecodeError
{
public enum HuffmanDecodeError {
/// The decoder entered an invalid state. Usually this means invalid input.
public struct InvalidState : NIOHPACKError {
public struct InvalidState: NIOHPACKError {
fileprivate init() {}
}
/// The output data could not be validated as UTF-8.
public struct InvalidUTF8 : NIOHPACKError {
public struct InvalidUTF8: NIOHPACKError {
fileprivate init() {}
}
}
/// The decoder table. This structure doesn't actually take up any space, I think?
fileprivate let decoderTable = HuffmanDecoderTable()
private let decoderTable = HuffmanDecoderTable()
extension ByteBuffer {
/// Decodes a huffman-encoded string from the `ByteBuffer`.
///
/// - Parameter at: The location of the encoded bytes to read.
@@ -179,7 +182,10 @@ extension ByteBuffer {
/// - Throws: HuffmanDecodeError if the data could not be decoded.
@discardableResult
func getHuffmanEncodedString(at index: Int, length: Int) throws -> String {
precondition(index + length <= self.capacity, "Requested range out of bounds: \(index ..< index+length) vs. \(self.capacity)")
precondition(
index + length <= self.capacity,
"Requested range out of bounds: \(index ..< index+length) vs. \(self.capacity)"
)
if length == 0 {
return ""
}
@@ -226,10 +232,9 @@ extension ByteBuffer {
return offset
}
return decoded
}
/// Decodes a huffman-encoded string from the provided `ByteBuffer`, starting at the buffer's
/// current `readerIndex`. Updates the `readerIndex` when it completes.
///
@@ -249,24 +254,30 @@ extension String {
/// This feature will be useful when decoding Huffman-encoded HPACK strings.
///
/// As this API does not exist prior to 5.3 on Linux, or on older Apple platforms, we fake it out with a pointer and accept the extra copy.
init(backportUnsafeUninitializedCapacity capacity: Int,
initializingUTF8With initializer: (_ buffer: UnsafeMutableBufferPointer<UInt8>) throws -> Int) rethrows {
init(
backportUnsafeUninitializedCapacity capacity: Int,
initializingUTF8With initializer: (_ buffer: UnsafeMutableBufferPointer<UInt8>) throws -> Int
) rethrows {
let buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: capacity)
defer {
buffer.deallocate()
}
let initializedCount = try initializer(buffer)
precondition(initializedCount <= capacity, "Overran buffer in initializer!")
self = String(decoding: UnsafeMutableBufferPointer(start: buffer.baseAddress!, count: initializedCount), as: UTF8.self)
self = String(
decoding: UnsafeMutableBufferPointer(start: buffer.baseAddress!, count: initializedCount),
as: UTF8.self
)
}
}
extension String {
init(customUnsafeUninitializedCapacity capacity: Int,
initializingUTF8With initializer: (_ buffer: UnsafeMutableBufferPointer<UInt8>) throws -> Int) rethrows {
init(
customUnsafeUninitializedCapacity capacity: Int,
initializingUTF8With initializer: (_ buffer: UnsafeMutableBufferPointer<UInt8>) throws -> Int
) rethrows {
if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) {
try self.init(unsafeUninitializedCapacity: capacity, initializingUTF8With: initializer)
} else {
File diff suppressed because it is too large. Load diff
+22 -19
View File
@@ -24,7 +24,7 @@ public struct IndexedHeaderTable {
// TODO(cory): This property should be removed, we only keep it for use in headerViews(at:).
private var allocator: ByteBufferAllocator
/// Creates a new header table, optionally specifying a maximum size for the dynamic
/// portion of the table.
///
@@ -34,7 +34,7 @@ public struct IndexedHeaderTable {
self.dynamicTable = DynamicHeaderTable(maximumLength: maxDynamicTableSize)
self.allocator = allocator
}
/// Obtains the header key/value pair at the given index within the table.
///
/// - note: Per RFC 7541, this uses a *1-based* index.
@@ -48,12 +48,15 @@ public struct IndexedHeaderTable {
} else if index - self.staticTable.count < self.dynamicTable.count {
result = self.dynamicTable[index - self.staticTable.count]
} else {
throw NIOHPACKErrors.InvalidHeaderIndex(suppliedIndex: index, availableIndex: self.staticTable.count + self.dynamicTable.count - 1)
throw NIOHPACKErrors.InvalidHeaderIndex(
suppliedIndex: index,
availableIndex: self.staticTable.count + self.dynamicTable.count - 1
)
}
return (name: result.name, value: result.value)
}
/// Obtains the header key/value pair at the given index within the table as sequences of
/// raw bytes.
///
@@ -74,7 +77,7 @@ public struct IndexedHeaderTable {
return (nameBuffer.readableBytesView, valueBuffer.readableBytesView)
}
/// Searches the table to locate an existing header with the given name and value. If
/// no item exists that contains a matching value, it will return the index of the first
/// item with a matching header name instead, to be encoded as index+value.
@@ -107,7 +110,7 @@ public struct IndexedHeaderTable {
return (index, false)
}
}
// no complete match: search the dynamic table now
if let result = self.dynamicTable.findExistingHeader(named: name, value: value) {
if let staticIndex = firstHeaderIndex, result.containsValue == false {
@@ -127,7 +130,7 @@ public struct IndexedHeaderTable {
return nil
}
}
/// Appends a header to the table.
///
/// This call may result in an empty table, as per RFC 7541 § 4.4:
@@ -142,7 +145,7 @@ public struct IndexedHeaderTable {
// This function is unnecessarily marked throws, but none of its underlying functions throw anymore.
self.dynamicTable.addHeader(named: name, value: value)
}
/// Appends a header to the table.
///
/// This call may result in an empty table, as per RFC 7541 § 4.4:
@@ -157,38 +160,38 @@ public struct IndexedHeaderTable {
/// - name: A sequence of contiguous bytes containing the name of the header to insert.
/// - value: A sequence of contiguous bytes containing the value of the header to insert.
@available(*, deprecated, renamed: "add(headerNamed:value:)")
public mutating func add<Name: Collection, Value: Collection>(headerNamed name: Name, value: Value) throws where Name.Element == UInt8, Value.Element == UInt8 {
public mutating func add<Name: Collection, Value: Collection>(headerNamed name: Name, value: Value) throws
where Name.Element == UInt8, Value.Element == UInt8 {
let nameString = String(decoding: name, as: UTF8.self)
let valueString = String(decoding: value, as: UTF8.self)
try self.add(headerNamed: nameString, value: valueString)
}
/// Internal for test access.
internal func dumpHeaders() -> String {
return "\(staticTable.dumpHeaders())\n\(dynamicTable.dumpHeaders())"
"\(staticTable.dumpHeaders())\n\(dynamicTable.dumpHeaders())"
}
/// The length, in bytes, of the dynamic portion of the header table.
public var dynamicTableLength: Int {
return self.dynamicTable.length
self.dynamicTable.length
}
/// The current allowed length of the dynamic portion of the header table. May be
/// less than the current protocol-assigned maximum supplied by a SETTINGS frame.
public var dynamicTableAllowedLength: Int {
get { return self.dynamicTable.allowedLength }
get { self.dynamicTable.allowedLength }
set { self.dynamicTable.allowedLength = newValue }
}
/// The hard limit on the size to which the dynamic table may grow. Only a SETTINGS
/// frame can change this: it can't grow beyond this size due to changes within
/// header blocks.
public var maxDynamicTableLength: Int {
get { return self.dynamicTable.maximumTableLength }
get { self.dynamicTable.maximumTableLength }
set { self.dynamicTable.maximumTableLength = newValue }
}
}
extension IndexedHeaderTable: Sendable {}
+11 -7
View File
@@ -14,7 +14,7 @@
import NIOCore
/* private but tests */
// private but tests
/// Encodes an integer value into a provided memory location.
///
/// - Parameters:
@@ -24,8 +24,12 @@ import NIOCore
/// - prefixBits: Existing bits to place in that first byte of `buffer` before encoding `value`.
/// - Returns: Returns the number of bytes used to encode the integer.
@discardableResult
func encodeInteger(_ value: UInt64, to buffer: inout ByteBuffer,
prefix: Int, prefixBits: UInt8 = 0) -> Int {
func encodeInteger(
_ value: UInt64,
to buffer: inout ByteBuffer,
prefix: Int,
prefixBits: UInt8 = 0
) -> Int {
assert(prefix <= 8)
assert(prefix >= 1)
@@ -62,16 +66,16 @@ func encodeInteger(_ value: UInt64, to buffer: inout ByteBuffer,
return buffer.writerIndex &- start
}
fileprivate let valueMask: UInt8 = 127
fileprivate let continuationMask: UInt8 = 128
private let valueMask: UInt8 = 127
private let continuationMask: UInt8 = 128
/* private but tests */
// private but tests
struct DecodedInteger {
var value: Int
var bytesRead: Int
}
/* private but tests */
// private but tests
func decodeInteger(from bytes: ByteBufferView, prefix: Int) throws -> DecodedInteger {
precondition((1...8).contains(prefix))
if bytes.isEmpty {
+2 -2
View File
@@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//
internal let StaticHeaderTable: [(String, String)] = [
("", ""), // real indexing begins at 1
("", ""), // real indexing begins at 1
(":authority", ""),
(":method", "GET"),
(":method", "POST"),
@@ -74,5 +74,5 @@ internal let StaticHeaderTable: [(String, String)] = [
("user-agent", ""),
("vary", ""),
("via", ""),
("www-authenticate", "")
("www-authenticate", ""),
]
File diff suppressed because it is too large. Load diff
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import NIOCore
/// A representation of the state of the HTTP/2 streams in a single HTTP/2 connection.
struct ConnectionStreamState {
/// The "safe" default value of SETTINGS_MAX_CONCURRENT_STREAMS.
@@ -61,7 +60,7 @@ struct ConnectionStreamState {
/// The total number of streams currently active.
var openStreams: Int {
return Int(self.clientStreamCount) + Int(self.serverStreamCount)
Int(self.clientStreamCount) + Int(self.serverStreamCount)
}
init() {
@@ -79,9 +78,17 @@ struct ConnectionStreamState {
/// - remoteInitialWindowSize: The initial window size of the remote peer.
/// - requestVerb: the HTTP method used on the request
/// - throws: If the stream ID is invalid.
mutating func createRemotelyPushedStream(streamID: HTTP2StreamID, remoteInitialWindowSize: UInt32, requestVerb: String?) throws {
mutating func createRemotelyPushedStream(
streamID: HTTP2StreamID,
remoteInitialWindowSize: UInt32,
requestVerb: String?
) throws {
try self.reserveServerStreamID(streamID)
let streamState = HTTP2StreamStateMachine(receivedPushPromiseCreatingStreamID: streamID, remoteInitialWindowSize: remoteInitialWindowSize, requestVerb: requestVerb)
let streamState = HTTP2StreamStateMachine(
receivedPushPromiseCreatingStreamID: streamID,
remoteInitialWindowSize: remoteInitialWindowSize,
requestVerb: requestVerb
)
self.activeStreams.insert(streamState)
}
@@ -94,9 +101,17 @@ struct ConnectionStreamState {
/// - streamID: The ID of the pushed stream.
/// - localInitialWindowSize: Our initial window size..
/// - throws: If the stream ID is invalid.
mutating func createLocallyPushedStream(streamID: HTTP2StreamID, localInitialWindowSize: UInt32, requestVerb: String?) throws {
mutating func createLocallyPushedStream(
streamID: HTTP2StreamID,
localInitialWindowSize: UInt32,
requestVerb: String?
) throws {
try self.reserveServerStreamID(streamID)
let streamState = HTTP2StreamStateMachine(sentPushPromiseCreatingStreamID: streamID, localInitialWindowSize: localInitialWindowSize, requestVerb: requestVerb)
let streamState = HTTP2StreamStateMachine(
sentPushPromiseCreatingStreamID: streamID,
localInitialWindowSize: localInitialWindowSize,
requestVerb: requestVerb
)
self.activeStreams.insert(streamState)
}
@@ -113,17 +128,21 @@ struct ConnectionStreamState {
/// - modifier: A block that will be invoked to modify the stream state, if present.
/// - throws: Any errors thrown from the creator.
/// - Returns: The result of the state modification, as well as any state change that occurred to the stream.
mutating func modifyStreamStateCreateIfNeeded(streamID: HTTP2StreamID,
localRole: HTTP2StreamStateMachine.StreamRole,
localInitialWindowSize: UInt32,
remoteInitialWindowSize: UInt32,
modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) throws -> StateMachineResultWithStreamEffect {
mutating func modifyStreamStateCreateIfNeeded(
streamID: HTTP2StreamID,
localRole: HTTP2StreamStateMachine.StreamRole,
localInitialWindowSize: UInt32,
remoteInitialWindowSize: UInt32,
modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect
) throws -> StateMachineResultWithStreamEffect {
func creator() throws -> HTTP2StreamStateMachine {
try self.reserveClientStreamID(streamID)
let initialValue = HTTP2StreamStateMachine(streamID: streamID,
localRole: localRole,
localInitialWindowSize: localInitialWindowSize,
remoteInitialWindowSize: remoteInitialWindowSize)
let initialValue = HTTP2StreamStateMachine(
streamID: streamID,
localRole: localRole,
localInitialWindowSize: localInitialWindowSize,
remoteInitialWindowSize: remoteInitialWindowSize
)
return initialValue
}
@@ -158,12 +177,21 @@ struct ConnectionStreamState {
/// - ignoreClosed: Whether a closed stream should be ignored. Should be set to `true` when receiving window update or reset stream frames.
/// - modifier: A block that will be invoked to modify the stream state, if present.
/// - Returns: The result of the state modification, as well as any state change that occurred to the stream.
mutating func modifyStreamState(streamID: HTTP2StreamID,
ignoreRecentlyReset: Bool,
ignoreClosed: Bool = false,
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) -> StateMachineResultWithStreamEffect {
mutating func modifyStreamState(
streamID: HTTP2StreamID,
ignoreRecentlyReset: Bool,
ignoreClosed: Bool = false,
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect
) -> StateMachineResultWithStreamEffect {
guard let result = self.activeStreams.autoClosingTransform(streamID: streamID, modifier) else {
return StateMachineResultWithStreamEffect(result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: ignoreRecentlyReset, ignoreClosed: ignoreClosed), effect: nil)
return StateMachineResultWithStreamEffect(
result: self.streamMissing(
streamID: streamID,
ignoreRecentlyReset: ignoreRecentlyReset,
ignoreClosed: ignoreClosed
),
effect: nil
)
}
if let effect = result.effect, effect.closedStream {
@@ -185,14 +213,18 @@ struct ConnectionStreamState {
/// - modifier: A block that will be invoked to modify the stream state, if present.
/// - Returns: The result of the state modification, as well as any state change that occurred to the stream.
@inline(__always)
mutating func locallyResetStreamState(streamID: HTTP2StreamID,
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect) -> StateMachineResultWithStreamEffect {
mutating func locallyResetStreamState(
streamID: HTTP2StreamID,
_ modifier: (inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect
) -> StateMachineResultWithStreamEffect {
guard let result = self.activeStreams.autoClosingTransform(streamID: streamID, modifier) else {
// We never ignore recently reset streams here, as this should only ever be used when *sending* frames.
return StateMachineResultWithStreamEffect(result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: false, ignoreClosed: false), effect: nil)
return StateMachineResultWithStreamEffect(
result: self.streamMissing(streamID: streamID, ignoreRecentlyReset: false, ignoreClosed: false),
effect: nil
)
}
guard let effect = result.effect, effect.closedStream else {
preconditionFailure("Locally resetting stream state did not close it!")
}
@@ -254,9 +286,11 @@ struct ConnectionStreamState {
/// - droppedLocally: Whether this drop was caused by sending a GOAWAY frame or receiving it.
/// - initiator: The peer that sent the GOAWAY frame.
/// - Returns: the stream IDs closed by this operation.
mutating func dropAllStreamsWithIDHigherThan(_ streamID: HTTP2StreamID,
droppedLocally: Bool,
initiatedBy initiator: HTTP2ConnectionStateMachine.ConnectionRole) -> [HTTP2StreamID]? {
mutating func dropAllStreamsWithIDHigherThan(
_ streamID: HTTP2StreamID,
droppedLocally: Bool,
initiatedBy initiator: HTTP2ConnectionStateMachine.ConnectionRole
) -> [HTTP2StreamID]? {
var droppedIDs: [HTTP2StreamID] = []
self.activeStreams.dropDataWithStreamIDGreaterThan(streamID, initiatedBy: initiator) { data in
droppedIDs = data.map { $0.streamID }
@@ -287,22 +321,32 @@ struct ConnectionStreamState {
/// - ignoreRecentlyReset: Whether a recently reset stream should be ignored.
/// - ignoreClosed: Whether a closed stream should be ignored.
/// - Returns: A `StateMachineResult` for this frame error.
private func streamMissing(streamID: HTTP2StreamID, ignoreRecentlyReset: Bool, ignoreClosed: Bool) -> StateMachineResult {
private func streamMissing(
streamID: HTTP2StreamID,
ignoreRecentlyReset: Bool,
ignoreClosed: Bool
) -> StateMachineResult {
if ignoreRecentlyReset && self.recentlyResetStreams.contains(streamID) {
return .ignoreFrame
}
switch streamID.mayBeInitiatedBy(.client) {
case true where streamID > self.lastClientStreamID,
false where streamID > self.lastServerStreamID:
false where streamID > self.lastServerStreamID:
// The stream in question is idle.
return .connectionError(underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID), type: .protocolError)
return .connectionError(
underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID),
type: .protocolError
)
default:
// This stream must have already been closed.
if ignoreClosed {
return .ignoreFrame
} else {
return .connectionError(underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID), type: .streamClosed)
return .connectionError(
underlyingError: NIOHTTP2Errors.noSuchStream(streamID: streamID),
type: .streamClosed
)
}
}
}
@@ -317,11 +361,10 @@ struct ConnectionStreamState {
}
}
extension CircularBuffer {
// CircularBuffer may never be "full": that is, capacity may never equal count.
var effectiveCapacity: Int {
return self.capacity - 1
self.capacity - 1
}
/// Prepends `element` without expanding the capacity, by dropping the
@@ -357,7 +400,6 @@ extension CircularBuffer {
}
}
extension StreamStateChange {
fileprivate var closedStream: Bool {
switch self {
@@ -22,4 +22,5 @@ protocol MayReceiveFrames:
ReceivingPushPromiseState,
ReceivingWindowUpdateState,
ReceivingRstStreamState,
ReceivingGoawayState { }
ReceivingGoawayState
{}
@@ -24,17 +24,29 @@ protocol ReceivingDataState: HasFlowControlWindows {
extension ReceivingDataState {
/// Called to receive a DATA frame.
mutating func receiveData(streamID: HTTP2StreamID, contentLength: Int, flowControlledBytes: Int, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
mutating func receiveData(
streamID: HTTP2StreamID,
contentLength: Int,
flowControlledBytes: Int,
isEndStreamSet endStream: Bool
) -> StateMachineResultWithEffect {
do {
try self.inboundFlowControlWindow.consume(flowControlledBytes: flowControlledBytes)
} catch let error where error is NIOHTTP2Errors.FlowControlViolation {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .flowControlError),
effect: nil
)
} catch {
preconditionFailure("Unexpected error: \(error)")
}
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true) {
$0.receiveData(contentLength: contentLength, flowControlledBytes: flowControlledBytes, isEndStreamSet: endStream)
$0.receiveData(
contentLength: contentLength,
flowControlledBytes: flowControlledBytes,
isEndStreamSet: endStream
)
}
// We need to be a bit careful here. The backing code may have triggered either an ignoreFrame or streamError. While both of these
@@ -43,16 +55,26 @@ extension ReceivingDataState {
}
}
private extension StateMachineResultWithEffect {
static func withGuaranteedFlowControlEffect<ConnectionState: ReceivingDataState>(_ result: StateMachineResultWithStreamEffect, connectionState: ConnectionState) -> StateMachineResultWithEffect {
extension StateMachineResultWithEffect {
fileprivate static func withGuaranteedFlowControlEffect<ConnectionState: ReceivingDataState>(
_ result: StateMachineResultWithStreamEffect,
connectionState: ConnectionState
) -> StateMachineResultWithEffect {
// In most cases, this will update the underlying effect with the new flow control window info.
var newResult = StateMachineResultWithEffect(result,
inboundFlowControlWindow: connectionState.inboundFlowControlWindow,
outboundFlowControlWindow: connectionState.outboundFlowControlWindow)
var newResult = StateMachineResultWithEffect(
result,
inboundFlowControlWindow: connectionState.inboundFlowControlWindow,
outboundFlowControlWindow: connectionState.outboundFlowControlWindow
)
if newResult.effect == nil {
// But if we aren't noting it anywhere else, we note it here.
newResult.effect = .flowControlChange(.init(localConnectionWindowSize: Int(connectionState.outboundFlowControlWindow), remoteConnectionWindowSize: Int(connectionState.inboundFlowControlWindow), localStreamWindowSize: nil))
newResult.effect = .flowControlChange(
.init(
localConnectionWindowSize: Int(connectionState.outboundFlowControlWindow),
remoteConnectionWindowSize: Int(connectionState.inboundFlowControlWindow),
localStreamWindowSize: nil
)
)
}
return newResult
@@ -26,10 +26,20 @@ extension ReceivingGoawayState {
mutating func receiveGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
guard lastStreamID.mayBeInitiatedBy(self.role) || lastStreamID == .rootStream || lastStreamID == .maxID else {
// The remote peer has sent a GOAWAY with an invalid stream ID.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(), type: .protocolError), effect: nil)
return .init(
result: .connectionError(
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
type: .protocolError
),
effect: nil
)
}
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: false, initiatedBy: self.role)
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
lastStreamID,
droppedLocally: false,
initiatedBy: self.role
)
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
return .init(result: .succeed, effect: effect)
}
@@ -39,18 +49,33 @@ extension ReceivingGoawayState where Self: RemotelyQuiescingState {
mutating func receiveGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
guard lastStreamID.mayBeInitiatedBy(self.role) || lastStreamID == .rootStream || lastStreamID == .maxID else {
// The remote peer has sent a GOAWAY with an invalid stream ID.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(), type: .protocolError), effect: nil)
return .init(
result: .connectionError(
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
type: .protocolError
),
effect: nil
)
}
if lastStreamID > self.lastLocalStreamID {
// The remote peer has attempted to raise the lastStreamID.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(), type: .protocolError), effect: nil)
return .init(
result: .connectionError(
underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(),
type: .protocolError
),
effect: nil
)
}
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: false, initiatedBy: self.role)
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
lastStreamID,
droppedLocally: false,
initiatedBy: self.role
)
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
self.lastLocalStreamID = lastStreamID
return .init(result: .succeed, effect: effect)
}
}
@@ -17,7 +17,9 @@ import NIOHPACK
/// can validly accept headers.
///
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
protocol ReceivingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSettings, HasRemoteExtendedConnectSettings, ConnectionStateWithRole {
protocol ReceivingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSettings,
HasRemoteExtendedConnectSettings, ConnectionStateWithRole
{
var role: HTTP2ConnectionStateMachine.ConnectionRole { get }
var headerBlockValidation: HTTP2ConnectionStateMachine.ValidationState { get }
@@ -33,7 +35,11 @@ protocol ReceivingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSe
extension ReceivingHeadersState {
/// Called when we receive a HEADERS frame in this state.
mutating func receiveHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
mutating func receiveHeaders(
streamID: HTTP2StreamID,
headers: HPACKHeaders,
isEndStreamSet endStream: Bool
) -> StateMachineResultWithEffect {
let result: StateMachineResultWithStreamEffect
let validateHeaderBlock = self.headerBlockValidation == .enabled
let validateContentLength = self.contentLengthValidation == .enabled
@@ -42,33 +48,60 @@ extension ReceivingHeadersState {
if self.role == .server && streamID.mayBeInitiatedBy(.client) {
do {
result = try self.streamState.modifyStreamStateCreateIfNeeded(streamID: streamID, localRole: .server, localInitialWindowSize: self.localInitialWindowSize, remoteInitialWindowSize: self.remoteInitialWindowSize) {
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
result = try self.streamState.modifyStreamStateCreateIfNeeded(
streamID: streamID,
localRole: .server,
localInitialWindowSize: self.localInitialWindowSize,
remoteInitialWindowSize: self.remoteInitialWindowSize
) {
$0.receiveHeaders(
headers: headers,
validateHeaderBlock: validateHeaderBlock,
validateContentLength: validateContentLength,
localSupportsExtendedConnect: localSupportsExtendedConnect,
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
isEndStreamSet: endStream
)
}
} catch {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
} else {
// HEADERS cannot create streams for servers, so this must be for a stream we already know about.
result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true) {
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
$0.receiveHeaders(
headers: headers,
validateHeaderBlock: validateHeaderBlock,
validateContentLength: validateContentLength,
localSupportsExtendedConnect: localSupportsExtendedConnect,
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
isEndStreamSet: endStream
)
}
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
extension ReceivingHeadersState where Self: LocallyQuiescingState {
/// Called when we receive a HEADERS frame in this state.
///
/// If we've quiesced this connection, the remote peer is no longer allowed to create new streams.
/// We ignore any frame that appears to be creating a new stream, and then prevent this from creating
/// new streams.
mutating func receiveHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
mutating func receiveHeaders(
streamID: HTTP2StreamID,
headers: HPACKHeaders,
isEndStreamSet endStream: Bool
) -> StateMachineResultWithEffect {
let validateHeaderBlock = self.headerBlockValidation == .enabled
let validateContentLength = self.contentLengthValidation == .enabled
let localSupportsExtendedConnect = self.localSupportsExtendedConnect
@@ -83,10 +116,19 @@ extension ReceivingHeadersState where Self: LocallyQuiescingState {
// At this stage we've quiesced, so the remote peer is not allowed to create new streams.
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true) {
$0.receiveHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
$0.receiveHeaders(
headers: headers,
validateHeaderBlock: validateHeaderBlock,
validateContentLength: validateContentLength,
localSupportsExtendedConnect: localSupportsExtendedConnect,
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
isEndStreamSet: endStream
)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
@@ -30,11 +30,19 @@ protocol ReceivingPushPromiseState: HasFlowControlWindows {
}
extension ReceivingPushPromiseState {
mutating func receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
return self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
mutating func receivePushPromise(
originalStreamID: HTTP2StreamID,
childStreamID: HTTP2StreamID,
headers: HPACKHeaders
) -> StateMachineResultWithEffect {
self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
}
fileprivate mutating func _receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
fileprivate mutating func _receivePushPromise(
originalStreamID: HTTP2StreamID,
childStreamID: HTTP2StreamID,
headers: HPACKHeaders
) -> StateMachineResultWithEffect {
// In states that support a push promise we have two steps. Firstly, we want to create the child stream; then we want to
// pass the PUSH_PROMISE frame through the stream state machine for the parent stream.
//
@@ -46,38 +54,62 @@ extension ReceivingPushPromiseState {
//
// Before any of this, though, we need to check whether the remote peer is even allowed to push!
guard self.peerMayPush else {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(), type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(
underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(),
type: .protocolError
),
effect: nil
)
}
let validateHeaderBlock = self.headerBlockValidation == .enabled
let requestVerb = headers.first(name: ":method")
do {
try self.streamState.createRemotelyPushedStream(streamID: childStreamID,
remoteInitialWindowSize: self.remoteInitialWindowSize, requestVerb: requestVerb)
try self.streamState.createRemotelyPushedStream(
streamID: childStreamID,
remoteInitialWindowSize: self.remoteInitialWindowSize,
requestVerb: requestVerb
)
let result = self.streamState.modifyStreamState(streamID: originalStreamID, ignoreRecentlyReset: true) {
$0.receivePushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
} catch {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
}
/// Whether the remote peer may push.
var peerMayPush: Bool {
// In the case where we don't have local settings, we have to assume the default value, in which servers may push and clients may not.
return self.role == .client
self.role == .client
}
}
extension ReceivingPushPromiseState where Self: LocallyQuiescingState {
mutating func receivePushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
mutating func receivePushPromise(
originalStreamID: HTTP2StreamID,
childStreamID: HTTP2StreamID,
headers: HPACKHeaders
) -> StateMachineResultWithEffect {
// This check is duplicated here, because the protocol error of violating this setting is more important than ignoring the frame.
guard self.peerMayPush else {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(), type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(
underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(),
type: .protocolError
),
effect: nil
)
}
// If we're a client, the server is forbidden from initiating new streams, as we quiesced. However, RFC 7540 wants us to ignore this.
@@ -87,12 +119,16 @@ extension ReceivingPushPromiseState where Self: LocallyQuiescingState {
// We're a server, so the remote peer can't initiate a stream with a PUSH_PROMISE, but that's ok, the stream state machine
// will forbid this as it normally does.
return self._receivePushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
return self._receivePushPromise(
originalStreamID: originalStreamID,
childStreamID: childStreamID,
headers: headers
)
}
}
extension ReceivingPushPromiseState where Self: HasLocalSettings {
var peerMayPush: Bool {
return self.localSettings.enablePush == 1 && self.role == .client
self.localSettings.enablePush == 1 && self.role == .client
}
}
@@ -25,11 +25,17 @@ extension ReceivingRstStreamState {
mutating func receiveRstStream(streamID: HTTP2StreamID, reason: HTTP2ErrorCode) -> StateMachineResultWithEffect {
// RFC 7540 § 6.4 <https://httpwg.org/specs/rfc7540.html#RST_STREAM> does not explicitly forbid a peer sending
// multiple RST_STREAMs for the same stream which means we should ignore subsequent RST_STREAMs.
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true, ignoreClosed: true) {
let result = self.streamState.modifyStreamState(
streamID: streamID,
ignoreRecentlyReset: true,
ignoreClosed: true
) {
$0.receiveRstStream(reason: reason)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
@@ -28,25 +28,41 @@ extension ReceivingWindowUpdateState {
// This is an update for the connection. We police the errors here.
do {
try self.outboundFlowControlWindow.windowUpdate(by: increment)
let flowControlSize: NIOHTTP2ConnectionStateChange = .flowControlChange(.init(localConnectionWindowSize: Int(self.outboundFlowControlWindow),
remoteConnectionWindowSize: Int(self.inboundFlowControlWindow),
localStreamWindowSize: nil))
let flowControlSize: NIOHTTP2ConnectionStateChange = .flowControlChange(
.init(
localConnectionWindowSize: Int(self.outboundFlowControlWindow),
remoteConnectionWindowSize: Int(self.inboundFlowControlWindow),
localStreamWindowSize: nil
)
)
return StateMachineResultWithEffect(result: .succeed, effect: flowControlSize)
} catch let error where error is NIOHTTP2Errors.InvalidFlowControlWindowSize {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .flowControlError),
effect: nil
)
} catch let error where error is NIOHTTP2Errors.InvalidWindowIncrementSize {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
} catch {
preconditionFailure("Unexpected error: \(error)")
}
} else {
// This is an update for a specific stream: it's responsible for policing any errors.
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: true, ignoreClosed: true) {
let result = self.streamState.modifyStreamState(
streamID: streamID,
ignoreRecentlyReset: true,
ignoreClosed: true
) {
$0.receiveWindowUpdate(windowIncrement: increment)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
}
@@ -22,4 +22,5 @@ protocol MaySendFrames:
SendingPushPromiseState,
SendingWindowUpdateState,
SendingRstStreamState,
SendingGoawayState { }
SendingGoawayState
{}
@@ -24,20 +24,34 @@ protocol SendingDataState: HasFlowControlWindows {
extension SendingDataState {
/// Called to send a DATA frame.
mutating func sendData(streamID: HTTP2StreamID, contentLength: Int, flowControlledBytes: Int, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
mutating func sendData(
streamID: HTTP2StreamID,
contentLength: Int,
flowControlledBytes: Int,
isEndStreamSet endStream: Bool
) -> StateMachineResultWithEffect {
do {
try self.outboundFlowControlWindow.consume(flowControlledBytes: flowControlledBytes)
} catch let error where error is NIOHTTP2Errors.FlowControlViolation {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .flowControlError),
effect: nil
)
} catch {
preconditionFailure("Unexpected error: \(error)")
}
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
$0.sendData(contentLength: contentLength, flowControlledBytes: flowControlledBytes, isEndStreamSet: endStream)
$0.sendData(
contentLength: contentLength,
flowControlledBytes: flowControlledBytes,
isEndStreamSet: endStream
)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
@@ -24,12 +24,23 @@ protocol SendingGoawayState {
extension SendingGoawayState {
mutating func sendGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID else {
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID
else {
// The user has sent a GOAWAY with an invalid stream ID.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(), type: .protocolError), effect: nil)
return .init(
result: .connectionError(
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
type: .protocolError
),
effect: nil
)
}
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: true, initiatedBy: self.peerRole)
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
lastStreamID,
droppedLocally: true,
initiatedBy: self.peerRole
)
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
return .init(result: .succeed, effect: effect)
}
@@ -37,18 +48,35 @@ extension SendingGoawayState {
extension SendingGoawayState where Self: LocallyQuiescingState {
mutating func sendGoAwayFrame(lastStreamID: HTTP2StreamID) -> StateMachineResultWithEffect {
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID else {
guard lastStreamID.mayBeInitiatedBy(self.peerRole) || lastStreamID == .rootStream || lastStreamID == .maxID
else {
// The user has sent a GOAWAY with an invalid stream ID.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(), type: .protocolError), effect: nil)
return .init(
result: .connectionError(
underlyingError: NIOHTTP2Errors.invalidStreamIDForPeer(),
type: .protocolError
),
effect: nil
)
}
if lastStreamID > self.lastRemoteStreamID {
// The user has attempted to raise the lastStreamID.
return .init(result: .connectionError(underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(), type: .protocolError), effect: nil)
return .init(
result: .connectionError(
underlyingError: NIOHTTP2Errors.raisedGoawayLastStreamID(),
type: .protocolError
),
effect: nil
)
}
// Ok, this is a valid request, so we can now process it like .active.
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(lastStreamID, droppedLocally: true, initiatedBy: self.peerRole)
let droppedStreams = self.streamState.dropAllStreamsWithIDHigherThan(
lastStreamID,
droppedLocally: true,
initiatedBy: self.peerRole
)
let effect: NIOHTTP2ConnectionStateChange? = droppedStreams.map { .bulkStreamClosure(.init(closedStreams: $0)) }
self.lastRemoteStreamID = lastStreamID
return .init(result: .succeed, effect: effect)
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import NIOHPACK
/// A protocol that provides implementation for sending HEADERS frames, for those states that
/// can validly send headers.
///
@@ -34,7 +33,11 @@ protocol SendingHeadersState: HasFlowControlWindows, HasLocalExtendedConnectSett
extension SendingHeadersState {
/// Called when we send a HEADERS frame in this state.
mutating func sendHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
mutating func sendHeaders(
streamID: HTTP2StreamID,
headers: HPACKHeaders,
isEndStreamSet endStream: Bool
) -> StateMachineResultWithEffect {
let result: StateMachineResultWithStreamEffect
let validateHeaderBlock = self.headerBlockValidation == .enabled
let validateContentLength = self.contentLengthValidation == .enabled
@@ -43,48 +46,83 @@ extension SendingHeadersState {
if self.role == .client && streamID.mayBeInitiatedBy(.client) {
do {
result = try self.streamState.modifyStreamStateCreateIfNeeded(streamID: streamID,
localRole: .client,
localInitialWindowSize: self.localInitialWindowSize,
remoteInitialWindowSize: self.remoteInitialWindowSize) {
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
result = try self.streamState.modifyStreamStateCreateIfNeeded(
streamID: streamID,
localRole: .client,
localInitialWindowSize: self.localInitialWindowSize,
remoteInitialWindowSize: self.remoteInitialWindowSize
) {
$0.sendHeaders(
headers: headers,
validateHeaderBlock: validateHeaderBlock,
validateContentLength: validateContentLength,
localSupportsExtendedConnect: localSupportsExtendedConnect,
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
isEndStreamSet: endStream
)
}
} catch {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
} else {
// HEADERS cannot create streams for servers, so this must be for a stream we already know about.
result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
$0.sendHeaders(
headers: headers,
validateHeaderBlock: validateHeaderBlock,
validateContentLength: validateContentLength,
localSupportsExtendedConnect: localSupportsExtendedConnect,
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
isEndStreamSet: endStream
)
}
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
extension SendingHeadersState where Self: RemotelyQuiescingState {
/// If we've been remotely quiesced, we're forbidden from creating new streams. So we can only possibly
/// be modifying an existing one.
mutating func sendHeaders(streamID: HTTP2StreamID, headers: HPACKHeaders, isEndStreamSet endStream: Bool) -> StateMachineResultWithEffect {
mutating func sendHeaders(
streamID: HTTP2StreamID,
headers: HPACKHeaders,
isEndStreamSet endStream: Bool
) -> StateMachineResultWithEffect {
let validateHeaderBlock = self.headerBlockValidation == .enabled
let validateContentLength = self.contentLengthValidation == .enabled
let localSupportsExtendedConnect = self.localSupportsExtendedConnect
let remoteSupportsExtendedConnect = self.remoteSupportsExtendedConnect
if streamID.mayBeInitiatedBy(.client) &&
self.role == .client &&
streamID > self.streamState.lastClientStreamID {
if streamID.mayBeInitiatedBy(.client) && self.role == .client && streamID > self.streamState.lastClientStreamID
{
let error = NIOHTTP2Errors.createdStreamAfterGoaway()
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
$0.sendHeaders(headers: headers, validateHeaderBlock: validateHeaderBlock, validateContentLength: validateContentLength, localSupportsExtendedConnect: localSupportsExtendedConnect, remoteSupportsExtendedConnect: remoteSupportsExtendedConnect, isEndStreamSet: endStream)
$0.sendHeaders(
headers: headers,
validateHeaderBlock: validateHeaderBlock,
validateContentLength: validateContentLength,
localSupportsExtendedConnect: localSupportsExtendedConnect,
remoteSupportsExtendedConnect: remoteSupportsExtendedConnect,
isEndStreamSet: endStream
)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
@@ -30,11 +30,19 @@ protocol SendingPushPromiseState: HasFlowControlWindows {
}
extension SendingPushPromiseState {
mutating func sendPushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
return self._sendPushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
mutating func sendPushPromise(
originalStreamID: HTTP2StreamID,
childStreamID: HTTP2StreamID,
headers: HPACKHeaders
) -> StateMachineResultWithEffect {
self._sendPushPromise(originalStreamID: originalStreamID, childStreamID: childStreamID, headers: headers)
}
fileprivate mutating func _sendPushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
fileprivate mutating func _sendPushPromise(
originalStreamID: HTTP2StreamID,
childStreamID: HTTP2StreamID,
headers: HPACKHeaders
) -> StateMachineResultWithEffect {
let validateHeaderBlock = self.headerBlockValidation == .enabled
// While receivePushPromise has a two step process involving creating the child stream first, here we do it the other
@@ -42,17 +50,27 @@ extension SendingPushPromiseState {
// we don't have to emit a frame to report the error (we just return it to the user), we don't have to have a stream
// whose state we can modify.
func parentStateModifier(stateMachine: inout HTTP2StreamStateMachine) -> StateMachineResultWithStreamEffect {
return stateMachine.sendPushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
stateMachine.sendPushPromise(headers: headers, validateHeaderBlock: validateHeaderBlock)
}
// First, however, we need to check we can push at all!
guard self.mayPush else {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(), type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(
underlyingError: NIOHTTP2Errors.pushInViolationOfSetting(),
type: .protocolError
),
effect: nil
)
}
do {
let result = StateMachineResultWithEffect(
self.streamState.modifyStreamState(streamID: originalStreamID, ignoreRecentlyReset: false, parentStateModifier),
self.streamState.modifyStreamState(
streamID: originalStreamID,
ignoreRecentlyReset: false,
parentStateModifier
),
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
@@ -60,23 +78,34 @@ extension SendingPushPromiseState {
return result
}
let requestVerb = headers.first(name: ":method")
try self.streamState.createLocallyPushedStream(streamID: childStreamID, localInitialWindowSize: self.localInitialWindowSize, requestVerb: requestVerb)
try self.streamState.createLocallyPushedStream(
streamID: childStreamID,
localInitialWindowSize: self.localInitialWindowSize,
requestVerb: requestVerb
)
return result
} catch {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
}
/// Whether we may push.
var mayPush: Bool {
// In the case where we don't have remote settings, we have to assume the default value, in which case if we're a server we may push.
return self.role == .server
self.role == .server
}
}
extension SendingPushPromiseState where Self: RemotelyQuiescingState {
mutating func sendPushPromise(originalStreamID: HTTP2StreamID, childStreamID: HTTP2StreamID, headers: HPACKHeaders) -> StateMachineResultWithEffect {
mutating func sendPushPromise(
originalStreamID: HTTP2StreamID,
childStreamID: HTTP2StreamID,
headers: HPACKHeaders
) -> StateMachineResultWithEffect {
// This call should never be used, but we do want to ensure that conforming types cannot enter the above method.
// The state machine should return early in all cases where we might end up calling this function.
preconditionFailure("Must not be called")
@@ -85,6 +114,6 @@ extension SendingPushPromiseState where Self: RemotelyQuiescingState {
extension SendingPushPromiseState where Self: HasRemoteSettings {
var mayPush: Bool {
return self.remoteSettings.enablePush == 1 && self.role == .server
self.remoteSettings.enablePush == 1 && self.role == .server
}
}
@@ -26,8 +26,10 @@ extension SendingRstStreamState {
let result = self.streamState.locallyResetStreamState(streamID: streamID) {
$0.sendRstStream(reason: reason)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
@@ -30,9 +30,15 @@ extension SendingWindowUpdateState {
try self.inboundFlowControlWindow.windowUpdate(by: increment)
return StateMachineResultWithEffect(result: .succeed, effect: nil)
} catch let error where error is NIOHTTP2Errors.InvalidFlowControlWindowSize {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .flowControlError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .flowControlError),
effect: nil
)
} catch let error where error is NIOHTTP2Errors.InvalidWindowIncrementSize {
return StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
return StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
} catch {
preconditionFailure("Unexpected error: \(error)")
}
@@ -41,9 +47,11 @@ extension SendingWindowUpdateState {
let result = self.streamState.modifyStreamState(streamID: streamID, ignoreRecentlyReset: false) {
$0.sendWindowUpdate(windowIncrement: increment)
}
return StateMachineResultWithEffect(result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow)
return StateMachineResultWithEffect(
result,
inboundFlowControlWindow: self.inboundFlowControlWindow,
outboundFlowControlWindow: self.outboundFlowControlWindow
)
}
}
}
@@ -27,11 +27,15 @@ struct HTTP2SettingsState {
private var unacknowlegedSettingsFrames: [HTTP2Settings]
/// A callback that is invoked whenever a setting value has changed.
typealias OnValueChangeCallback = (_ setting: HTTP2SettingsParameter, _ oldValue: UInt32?, _ newValue: UInt32) throws -> Void
typealias OnValueChangeCallback = (_ setting: HTTP2SettingsParameter, _ oldValue: UInt32?, _ newValue: UInt32)
throws -> Void
init(localState: Bool) {
// Create the settings dictionary, and ensure it has space for the known SETTINGS values.
self.currentSettingsValues = [.headerTableSize: 4096, .enablePush: 1, .initialWindowSize: HTTP2SettingsState.defaultInitialWindowSize, .maxFrameSize: 1<<14]
self.currentSettingsValues = [
.headerTableSize: 4096, .enablePush: 1, .initialWindowSize: HTTP2SettingsState.defaultInitialWindowSize,
.maxFrameSize: 1 << 14,
]
self.currentSettingsValues.reserveCapacity(8)
// Create space for the unacknowledged SETTINGS frame data to be stored. In general this will be empty,
@@ -54,25 +58,25 @@ struct HTTP2SettingsState {
/// Obtain the current value of a settings parameter.
subscript(_ parameter: HTTP2SettingsParameter) -> UInt32? {
get {
return self.currentSettingsValues[parameter]
self.currentSettingsValues[parameter]
}
}
/// The current value of SETTINGS_INITIAL_WINDOW_SIZE.
var initialWindowSize: UInt32 {
// We can force-unwrap here as this setting always has a value.
return self[.initialWindowSize]!
self[.initialWindowSize]!
}
/// The current value of SETTINGS_ENABLE_PUSH.
var enablePush: UInt32 {
// We can force-unwrap here as this setting always has a value.
return self[.enablePush]!
self[.enablePush]!
}
/// The current value of SETTINGS_ENABLE_CONNECT_PROTOCOL
var enableConnectProtocol: UInt32? {
return self[.enableConnectProtocol]
self[.enableConnectProtocol]
}
/// The default value of SETTINGS_INITIAL_WINDOW_SIZE.
@@ -112,7 +116,7 @@ struct HTTP2SettingsState {
/// - settings: The received settings.
/// - onValueChange: A callback that will be invoked once for each setting change.
mutating func receiveSettings(_ settings: HTTP2Settings, onValueChange: OnValueChangeCallback) rethrows {
return try self.applySettings(settings, onValueChange: onValueChange)
try self.applySettings(settings, onValueChange: onValueChange)
}
/// Applies the given HTTP/2 settings to this state.
@@ -131,6 +135,6 @@ struct HTTP2SettingsState {
/// Obtain an empty dummy value, suitable for using as a temporary to avoid CoW operations.
static func dummyValue() -> HTTP2SettingsState {
return HTTP2SettingsState()
HTTP2SettingsState()
}
}
@@ -32,7 +32,10 @@ extension HasRemoteExtendedConnectSettings where Self: HasRemoteSettings {
}
extension HasRemoteSettings {
mutating func receiveSettingsChange(_ settings: HTTP2Settings, frameEncoder: inout HTTP2FrameEncoder) -> (StateMachineResultWithEffect, PostFrameOperation) {
mutating func receiveSettingsChange(
_ settings: HTTP2Settings,
frameEncoder: inout HTTP2FrameEncoder
) -> (StateMachineResultWithEffect, PostFrameOperation) {
// We do a little switcheroo here to avoid problems with overlapping accesses to
// self. It's a little more complex than normal because HTTP2SettingsState has
// two CoWable objects, and we don't want to CoW either of them, so we shove a dummy
@@ -74,7 +77,9 @@ extension HasRemoteSettings {
case .enableConnectProtocol:
// Must not transition from 1 -> 0
if originalValue == 1 && newValue == 0 {
throw NIOHTTP2Errors.invalidSetting(setting: HTTP2Setting(parameter: setting, value: Int(newValue)))
throw NIOHTTP2Errors.invalidSetting(
setting: HTTP2Setting(parameter: setting, value: Int(newValue))
)
}
effect.enableConnectProtocol = newValue == 1
default:
@@ -84,7 +89,9 @@ extension HasRemoteSettings {
}
return (.init(result: .succeed, effect: .remoteSettingsChanged(effect)), .sendAck)
} catch let err where err is NIOHTTP2Errors.InvalidFlowControlWindowSize {
return (.init(result: .connectionError(underlyingError: err, type: .flowControlError), effect: nil), .nothing)
return (
.init(result: .connectionError(underlyingError: err, type: .flowControlError), effect: nil), .nothing
)
} catch let err where err is NIOHTTP2Errors.InvalidSetting {
return (.init(result: .connectionError(underlyingError: err, type: .protocolError), effect: nil), .nothing)
} catch {
@@ -17,6 +17,6 @@
/// This protocol should only be conformed to by states for the HTTP/2 connection state machine.
protocol QuiescingState {
var streamState: ConnectionStreamState { get }
var quiescedByServer: Bool { get }
}
@@ -44,7 +44,6 @@ enum StateMachineResult {
case succeed
}
/// Operations that may need to be performed after receiving a frame.
enum PostFrameOperation {
/// An appropriate ACK must be sent.
@@ -54,7 +53,6 @@ enum PostFrameOperation {
case nothing
}
/// An encapsulation of a state machine result along with a possible triggered state change.
struct StateMachineResultWithEffect {
var result: StateMachineResult
@@ -66,12 +64,18 @@ struct StateMachineResultWithEffect {
self.effect = effect
}
init(_ streamEffect: StateMachineResultWithStreamEffect,
inboundFlowControlWindow: HTTP2FlowControlWindow,
outboundFlowControlWindow: HTTP2FlowControlWindow) {
init(
_ streamEffect: StateMachineResultWithStreamEffect,
inboundFlowControlWindow: HTTP2FlowControlWindow,
outboundFlowControlWindow: HTTP2FlowControlWindow
) {
self.result = streamEffect.result
self.effect = streamEffect.effect.map {
NIOHTTP2ConnectionStateChange($0, inboundFlowControlWindow: inboundFlowControlWindow, outboundFlowControlWindow: outboundFlowControlWindow)
NIOHTTP2ConnectionStateChange(
$0,
inboundFlowControlWindow: inboundFlowControlWindow,
outboundFlowControlWindow: outboundFlowControlWindow
)
}
}
}
+3 -4
View File
@@ -49,7 +49,7 @@ extension ContentLengthVerifier {
extension ContentLengthVerifier {
internal init(_ headers: HPACKHeaders, requestMethod: String?) throws {
if let requestMethod = requestMethod {
if let requestMethod = requestMethod {
if let status = headers.first(name: ":status"), status == "304" {
self.expectedContentLength = 0
return
@@ -82,13 +82,12 @@ extension ContentLengthVerifier {
/// The verifier for use when content length verification is disabled.
internal static var disabled: ContentLengthVerifier {
return ContentLengthVerifier(expectedContentLength: nil)
ContentLengthVerifier(expectedContentLength: nil)
}
}
extension ContentLengthVerifier: CustomStringConvertible {
var description: String {
return "ContentLengthVerifier(length: \(String(describing: self.expectedContentLength)))"
"ContentLengthVerifier(length: \(String(describing: self.expectedContentLength)))"
}
}
+16 -6
View File
@@ -30,16 +30,26 @@ struct DOSHeuristics<DeadlineClock: NIODeadlineClock> {
private var resetFrameRateControlStateMachine: HTTP2ResetFrameRateControlStateMachine
internal init(maximumSequentialEmptyDataFrames: Int, maximumResetFrameCount: Int, resetFrameCounterWindow: TimeAmount, clock: DeadlineClock = RealNIODeadlineClock()) {
precondition(maximumSequentialEmptyDataFrames >= 0,
"maximum sequential empty data frames must be positive, got \(maximumSequentialEmptyDataFrames)")
internal init(
maximumSequentialEmptyDataFrames: Int,
maximumResetFrameCount: Int,
resetFrameCounterWindow: TimeAmount,
clock: DeadlineClock = RealNIODeadlineClock()
) {
precondition(
maximumSequentialEmptyDataFrames >= 0,
"maximum sequential empty data frames must be positive, got \(maximumSequentialEmptyDataFrames)"
)
self.maximumSequentialEmptyDataFrames = maximumSequentialEmptyDataFrames
self.receivedEmptyDataFrames = 0
self.resetFrameRateControlStateMachine = .init(countThreshold: maximumResetFrameCount, timeWindow: resetFrameCounterWindow, clock: clock)
self.resetFrameRateControlStateMachine = .init(
countThreshold: maximumResetFrameCount,
timeWindow: resetFrameCounterWindow,
clock: clock
)
}
}
extension DOSHeuristics {
mutating func process(_ frame: HTTP2Frame) throws {
switch frame.payload {
@@ -120,7 +130,7 @@ extension DOSHeuristics {
self._state = .rateTooHigh
}
case .rateTooHigh:
break // no-op, there is no way to de-escalate from an excessive rate
break // no-op, there is no way to de-escalate from an excessive rate
}
}
}
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import NIOCore
/// An object that buffers new stream creation attempts to avoid violating
/// the HTTP/2 setting `SETTINGS_MAX_CONCURRENT_STREAMS`.
///
@@ -64,7 +63,9 @@ struct ConcurrentStreamBuffer {
///
/// Notes that the current number of outbound streams may have gone down, which is useful information
/// when flushing writes.
mutating func streamClosed(_ streamID: HTTP2StreamID) -> MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>? {
mutating func streamClosed(
_ streamID: HTTP2StreamID
) -> MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>? {
// We only care about outbound streams.
if streamID.mayBeInitiatedBy(self.mode) {
self.currentOutboundStreams -= 1
@@ -105,7 +106,11 @@ struct ConcurrentStreamBuffer {
self.bufferedFrames.markFlushPoint()
}
mutating func processOutboundFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?, channelWritable: Bool) throws -> OutboundFrameAction {
mutating func processOutboundFrame(
_ frame: HTTP2Frame,
promise: EventLoopPromise<Void>?,
channelWritable: Bool
) throws -> OutboundFrameAction {
// If this frame is not for a locally initiated stream, then that's fine, just pass it on. Even if the channel isn't
// writable, one of the other two buffers should catch this.
guard frame.streamID != .rootStream && frame.streamID.mayBeInitiatedBy(self.mode) else {
@@ -124,7 +129,8 @@ struct ConcurrentStreamBuffer {
// Again, we choose to ignore channel writability here because one of the other buffers should catch this frame.
if let firstElement = self.bufferedFrames.first,
frame.streamID >= firstElement.streamID,
let bufferIndex = self.bufferedFrames.binarySearch(key: { $0.streamID }, needle: frame.streamID) {
let bufferIndex = self.bufferedFrames.binarySearch(key: { $0.streamID }, needle: frame.streamID)
{
return self.bufferFrame(frame, promise: promise, bufferIndex: bufferIndex)
}
@@ -211,7 +217,11 @@ struct ConcurrentStreamBuffer {
return self.bufferedFrames.nextWriteFor(index)
}
private mutating func bufferFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?, bufferIndex index: SortedCircularBuffer.Index) -> OutboundFrameAction {
private mutating func bufferFrame(
_ frame: HTTP2Frame,
promise: EventLoopPromise<Void>?,
bufferIndex index: SortedCircularBuffer.Index
) -> OutboundFrameAction {
// Ok, we need to buffer this frame, and we know we have the index for it. What we do here depends on this frame type. For
// almost all frames, we just append them to the buffer. For RST_STREAM, however, we're in a different spot. RST_STREAM is a
// request to drop all resources for a given stream. We know we have some, but we shouldn't wait to unblock them, we should
@@ -223,9 +233,15 @@ struct ConcurrentStreamBuffer {
// If we're currently unbuffering this stream, we need to pass the RST_STREAM frame on for correctness. If we aren't, just
// drop it.
if writeBuffer.currentlyUnblocking {
return .forwardAndDrop(writeBuffer.frames, NIOHTTP2Errors.streamClosed(streamID: frame.streamID, errorCode: reason))
return .forwardAndDrop(
writeBuffer.frames,
NIOHTTP2Errors.streamClosed(streamID: frame.streamID, errorCode: reason)
)
} else {
return .succeedAndDrop(writeBuffer.frames, NIOHTTP2Errors.streamClosed(streamID: frame.streamID, errorCode: reason))
return .succeedAndDrop(
writeBuffer.frames,
NIOHTTP2Errors.streamClosed(streamID: frame.streamID, errorCode: reason)
)
}
}
@@ -310,7 +326,7 @@ private struct SortedCircularBuffer {
mutating func remove(at index: Index) -> Element {
// This one is easy: simple removal does what we need here.
return self._base.remove(at: index)
self._base.remove(at: index)
}
mutating func markFlushPoint() {
@@ -330,19 +346,19 @@ extension SortedCircularBuffer: RandomAccessCollection {
typealias Indices = CircularBuffer<ConcurrentStreamBuffer.FrameBuffer>.Indices
var startIndex: Index {
return self._base.startIndex
self._base.startIndex
}
var endIndex: Index {
return self._base.endIndex
self._base.endIndex
}
var indices: Indices {
return self._base.indices
self._base.indices
}
func distance(from start: Index, to end: Index) -> Int {
return self._base.distance(from: start, to: end)
self._base.distance(from: start, to: end)
}
func formIndex(after i: inout Index) {
@@ -354,27 +370,27 @@ extension SortedCircularBuffer: RandomAccessCollection {
}
func index(_ i: Index, offsetBy distance: Int) -> Index {
return self._base.index(i, offsetBy: distance)
self._base.index(i, offsetBy: distance)
}
func index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
return self._base.index(i, offsetBy: distance, limitedBy: limit)
self._base.index(i, offsetBy: distance, limitedBy: limit)
}
func index(after i: Index) -> Index {
return self._base.index(after: i)
self._base.index(after: i)
}
func index(before i: Index) -> Index {
return self._base.index(before: i)
self._base.index(before: i)
}
subscript(_ i: Index) -> Element {
return self._base[i]
self._base[i]
}
subscript(_ range: Range<Index>) -> SubSequence {
return self._base[range]
self._base[range]
}
}
@@ -407,9 +423,8 @@ extension SortedCircularBuffer {
}
}
private extension HTTP2StreamID {
func mayBeInitiatedBy(_ mode: NIOHTTP2Handler.ParserMode) -> Bool {
extension HTTP2StreamID {
fileprivate func mayBeInitiatedBy(_ mode: NIOHTTP2Handler.ParserMode) -> Bool {
switch mode {
case .client:
return self.networkStreamID % 2 == 1
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import NIOCore
/// A buffer that stores outbound control frames.
///
/// In general it is preferential to buffer outbound frames instead of passing them into the channel.
@@ -35,7 +34,6 @@ struct ControlFrameBuffer {
internal var maximumBufferSize: Int
}
// MARK:- ControlFrameBuffer initializers
extension ControlFrameBuffer {
internal init(maximumBufferSize: Int) {
@@ -46,10 +44,13 @@ extension ControlFrameBuffer {
}
}
// MARK:- ControlFrameBuffer frame processing
extension ControlFrameBuffer {
internal mutating func processOutboundFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?, channelWritable: Bool) throws -> OutboundFrameAction {
internal mutating func processOutboundFrame(
_ frame: HTTP2Frame,
promise: EventLoopPromise<Void>?,
channelWritable: Bool
) throws -> OutboundFrameAction {
switch frame.payload {
case .data:
// These frames are not buffered here. If it reached us, it's because we believe the channel is writable,
@@ -97,7 +98,6 @@ extension ControlFrameBuffer {
}
}
// MARK:- ControlFrameBuffer.PendingControlFrame definition
extension ControlFrameBuffer {
/// A buffered control frame write and its associated promise.
@@ -61,7 +61,7 @@ internal struct OutboundFlowControlBuffer {
/// The current value of SETTINGS_MAX_FRAME_SIZE set by the peer.
internal var maxFrameSize: Int
internal init(initialConnectionWindowSize: Int = 65535, initialMaxFrameSize: Int = 1<<14) {
internal init(initialConnectionWindowSize: Int = 65535, initialMaxFrameSize: Int = 1 << 14) {
// TODO(cory): HEAP! This should be a heap, sorted off the number of pending bytes!
self.streamDataBuffers = StreamMap()
self.connectionWindowSize = initialConnectionWindowSize
@@ -72,7 +72,10 @@ internal struct OutboundFlowControlBuffer {
self.flushableStreams.reserveCapacity(16)
}
internal mutating func processOutboundFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?) throws -> OutboundFrameAction {
internal mutating func processOutboundFrame(
_ frame: HTTP2Frame,
promise: EventLoopPromise<Void>?
) throws -> OutboundFrameAction {
// A side note: there is no special handling for RST_STREAM frames here, unlike in the concurrent streams buffer. This is because
// RST_STREAM frames will cause stream closure notifications, which will force us to drop our buffers. For this reason we can
// simplify our code here, which helps a lot.
@@ -93,7 +96,8 @@ internal struct OutboundFlowControlBuffer {
// Headers are special. If we have a data frame buffered, we buffer behind it to avoid frames
// being reordered. However, if we don't have a data frame buffered we pass the headers frame on
// immediately, as there is no risk of violating ordering guarantees.
let bufferResult = self.streamDataBuffers.modify(streamID: streamID) { (state: inout StreamFlowControlState) -> Bool in
let bufferResult = self.streamDataBuffers.modify(streamID: streamID) {
(state: inout StreamFlowControlState) -> Bool in
if state.haveBufferedDataFrame {
state.bufferWrite((frame.payload, promise))
return true
@@ -135,7 +139,7 @@ internal struct OutboundFlowControlBuffer {
}
private func nextStreamToSend() -> HTTP2StreamID? {
return self.flushableStreams.first
self.flushableStreams.first
}
internal mutating func updateWindowOfStream(_ streamID: HTTP2StreamID, newSize: Int32) {
@@ -172,7 +176,9 @@ internal struct OutboundFlowControlBuffer {
// We received a stream closed event. Drop any stream state we're holding.
//
// - returns: Any buffered stream state we may have been holding so their promises can be failed.
internal mutating func streamClosed(_ streamID: HTTP2StreamID) -> MarkedCircularBuffer<(HTTP2Frame.FramePayload, EventLoopPromise<Void>?)>? {
internal mutating func streamClosed(
_ streamID: HTTP2StreamID
) -> MarkedCircularBuffer<(HTTP2Frame.FramePayload, EventLoopPromise<Void>?)>? {
self.flushableStreams.remove(streamID)
guard var streamData = self.streamDataBuffers.removeValue(forStreamID: streamID) else {
// Huh, we didn't have any data for this stream. Oh well. That was easy.
@@ -194,8 +200,11 @@ internal struct OutboundFlowControlBuffer {
return nil
}
let nextWrite = self.streamDataBuffers.modify(streamID: nextStreamID) { (state: inout StreamFlowControlState) -> DataBuffer.BufferElement in
let (nextWrite, writabilityState) = state.nextWrite(maxSize: min(self.connectionWindowSize, self.maxFrameSize))
let nextWrite = self.streamDataBuffers.modify(streamID: nextStreamID) {
(state: inout StreamFlowControlState) -> DataBuffer.BufferElement in
let (nextWrite, writabilityState) = state.nextWrite(
maxSize: min(self.connectionWindowSize, self.maxFrameSize)
)
switch writabilityState {
case .changed(newValue: false):
@@ -233,7 +242,6 @@ internal struct OutboundFlowControlBuffer {
}
}
// MARK: Priority API
extension OutboundFlowControlBuffer {
/// A frame with new priority data has been received that affects prioritisation of outbound frames.
@@ -247,18 +255,17 @@ extension OutboundFlowControlBuffer {
}
}
private struct StreamFlowControlState: PerStreamData {
let streamID: HTTP2StreamID
internal private(set) var currentWindowSize: Int
private var dataBuffer: DataBuffer
var haveBufferedDataFrame: Bool {
return self.dataBuffer.haveBufferedDataFrame
self.dataBuffer.haveBufferedDataFrame
}
var hasFlowControlWindowSpaceForNextWrite: Bool {
return self.currentWindowSize > 0 || self.dataBuffer.nextWriteIsZeroSized
self.currentWindowSize > 0 || self.dataBuffer.nextWriteIsZeroSized
}
init(streamID: HTTP2StreamID, initialWindowSize: Int) {
@@ -303,7 +310,7 @@ private struct StreamFlowControlState: PerStreamData {
/// Removes all pending writes, invalidating this structure as it does so.
mutating func evacuatePendingWrites() -> MarkedCircularBuffer<DataBuffer.BufferElement> {
return self.dataBuffer.evacuatePendingWrites()
self.dataBuffer.evacuatePendingWrites()
}
func failAllWrites(error: ChannelError) {
@@ -336,22 +343,21 @@ private struct StreamFlowControlState: PerStreamData {
}
}
private struct DataBuffer {
typealias BufferElement = (HTTP2Frame.FramePayload, EventLoopPromise<Void>?)
private var bufferedChunks: MarkedCircularBuffer<BufferElement>
var haveBufferedDataFrame: Bool {
return self.bufferedChunks.count > 0
self.bufferedChunks.count > 0
}
var nextWriteIsZeroSized: Bool {
return self.bufferedChunks.first?.0.isZeroSizedWrite ?? false
self.bufferedChunks.first?.0.isZeroSizedWrite ?? false
}
var hasMark: Bool {
return self.bufferedChunks.hasMark
self.bufferedChunks.hasMark
}
/// An empty buffer, we use this avoid an allocation in 'evacuatePendingWrites'.
@@ -410,16 +416,14 @@ private struct DataBuffer {
}
}
/// Used to communicate whether a stream has had its writability state change.
fileprivate enum WritabilityState {
private enum WritabilityState {
case changed(newValue: Bool)
case unchanged
}
private extension IOData {
mutating func slicePrefix(_ length: Int) -> IOData {
extension IOData {
fileprivate mutating func slicePrefix(_ length: Int) -> IOData {
assert(length < self.readableBytes)
switch self {
@@ -430,7 +434,11 @@ private extension IOData {
return .byteBuffer(newBuf)
case .fileRegion(var region):
let newRegion = FileRegion(fileHandle: region.fileHandle, readerIndex: region.readerIndex, endIndex: region.readerIndex + length)
let newRegion = FileRegion(
fileHandle: region.fileHandle,
readerIndex: region.readerIndex,
endIndex: region.readerIndex + length
)
region.moveReaderIndex(forwardBy: length)
self = .fileRegion(region)
return .fileRegion(newRegion)
@@ -438,7 +446,6 @@ private extension IOData {
}
}
extension HTTP2Frame.FramePayload {
fileprivate var isZeroSizedWrite: Bool {
switch self {
@@ -450,7 +457,6 @@ extension HTTP2Frame.FramePayload {
}
}
extension StreamMap where Element == StreamFlowControlState {
//
/// Apply a transform to a wrapped DataBuffer.
@@ -461,6 +467,6 @@ extension StreamMap where Element == StreamFlowControlState {
/// - Returns: Whether the value was present or not.
@discardableResult
fileprivate mutating func apply(streamID: HTTP2StreamID, _ body: (inout Element) -> Void) -> Bool {
return self.modify(streamID: streamID, body) != nil
self.modify(streamID: streamID, body) != nil
}
}
@@ -53,11 +53,13 @@ internal struct CompoundOutboundBuffer {
private var controlFrameBuffer: ControlFrameBuffer
}
// MARK: CompoundOutboundBuffer initializers
extension CompoundOutboundBuffer {
internal init(mode: NIOHTTP2Handler.ParserMode, initialMaxOutboundStreams: Int, maxBufferedControlFrames: Int) {
self.concurrentStreamsBuffer = ConcurrentStreamBuffer(mode: mode, initialMaxOutboundStreams: initialMaxOutboundStreams)
self.concurrentStreamsBuffer = ConcurrentStreamBuffer(
mode: mode,
initialMaxOutboundStreams: initialMaxOutboundStreams
)
self.flowControlBuffer = OutboundFlowControlBuffer()
self.controlFrameBuffer = ControlFrameBuffer(maximumBufferSize: maxBufferedControlFrames)
}
@@ -72,9 +74,15 @@ extension CompoundOutboundBuffer {
/// which MarkedCircularBuffer is based, has internal invariants which require it to have a non-zero capacity).
/// This static empty buffer allows us to avoid that allocation in each `processOutboundFrame` call when there
/// are no frames to drop.
private static let emptyFramesToDrop = MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>(initialCapacity: 0)
private static let emptyFramesToDrop = MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>(
initialCapacity: 0
)
mutating func processOutboundFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?, channelWritable: Bool) throws -> OutboundFrameAction {
mutating func processOutboundFrame(
_ frame: HTTP2Frame,
promise: EventLoopPromise<Void>?,
channelWritable: Bool
) throws -> OutboundFrameAction {
var framesToDrop = Self.emptyFramesToDrop
var error: NIOHTTP2Errors.StreamClosed? = nil
@@ -86,7 +94,11 @@ extension CompoundOutboundBuffer {
// to the dropped frames.
// 4. If a buffer returns .succeedAndDrop, return .succeedAndDrop.
// The return value of the last buffer is the return value of the compound buffer, unless we returned early.
switch try self.concurrentStreamsBuffer.processOutboundFrame(frame, promise: promise, channelWritable: channelWritable) {
switch try self.concurrentStreamsBuffer.processOutboundFrame(
frame,
promise: promise,
channelWritable: channelWritable
) {
case .nothing:
return .nothing
case .succeedAndDrop(let framesToDrop, let error):
@@ -116,7 +128,11 @@ extension CompoundOutboundBuffer {
break
}
switch try self.controlFrameBuffer.processOutboundFrame(frame, promise: promise, channelWritable: channelWritable) {
switch try self.controlFrameBuffer.processOutboundFrame(
frame,
promise: promise,
channelWritable: channelWritable
) {
case .nothing:
return .nothing
case .forward:
@@ -177,8 +193,13 @@ extension CompoundOutboundBuffer {
// Ok, now to move on to work on the concurrent streams buffer.
while let (frame, promise) = self.concurrentStreamsBuffer.nextFlushedWritableFrame() {
do {
if try self.flowControlBuffer.processOutboundFrame(frame, promise: promise).shouldForward &&
self.controlFrameBuffer.processOutboundFrame(frame, promise: promise, channelWritable: channelWritable).shouldForward {
if try self.flowControlBuffer.processOutboundFrame(frame, promise: promise).shouldForward
&& self.controlFrameBuffer.processOutboundFrame(
frame,
promise: promise,
channelWritable: channelWritable
).shouldForward
{
return .frame(frame, promise)
}
} catch {
@@ -191,7 +212,11 @@ extension CompoundOutboundBuffer {
while let (frame, promise) = self.flowControlBuffer.nextFlushedWritableFrame() {
do {
if try self.controlFrameBuffer.processOutboundFrame(frame, promise: promise, channelWritable: channelWritable).shouldForward {
if try self.controlFrameBuffer.processOutboundFrame(
frame,
promise: promise,
channelWritable: channelWritable
).shouldForward {
return .frame(frame, promise)
}
} catch {
@@ -244,7 +269,7 @@ extension CompoundOutboundBuffer {
var maxFrameSize: Int {
get {
return self.flowControlBuffer.maxFrameSize
self.flowControlBuffer.maxFrameSize
}
set {
self.flowControlBuffer.maxFrameSize = newValue
@@ -253,7 +278,7 @@ extension CompoundOutboundBuffer {
var connectionWindowSize: Int {
get {
return self.flowControlBuffer.connectionWindowSize
self.flowControlBuffer.connectionWindowSize
}
set {
self.flowControlBuffer.connectionWindowSize = newValue
@@ -262,7 +287,7 @@ extension CompoundOutboundBuffer {
var maxOutboundStreams: Int {
get {
return self.concurrentStreamsBuffer.maxOutboundStreams
self.concurrentStreamsBuffer.maxOutboundStreams
}
set {
self.concurrentStreamsBuffer.maxOutboundStreams = newValue
@@ -301,7 +326,7 @@ extension CompoundOutboundBuffer.DroppedPromisesCollection: Collection {
static func < (lhs: Index, rhs: Index) -> Bool {
switch (lhs.backingIndex, rhs.backingIndex) {
case (.concurrentStreams, .flowControl),
(.concurrentStreams, .endIndex):
(.concurrentStreams, .endIndex):
return true
case (.concurrentStreams(let lhsBackingIndex), .concurrentStreams(let rhsBackingIndex)):
return lhsBackingIndex < rhsBackingIndex
@@ -328,7 +353,7 @@ extension CompoundOutboundBuffer.DroppedPromisesCollection: Collection {
}
var endIndex: Index {
return Index(.endIndex)
Index(.endIndex)
}
func index(after index: Index) -> Index {
@@ -377,7 +402,6 @@ extension CompoundOutboundBuffer.DroppedPromisesCollection: Collection {
}
}
extension OutboundFrameAction {
/// Whether this frame should be forwarded. Must only be used in cases where the frame
/// cannot lead to drops.
+39 -29
View File
@@ -18,36 +18,35 @@ extension HPACKHeaders {
///
/// If the header block is not valid, throws an error.
internal func validateRequestBlock(supportsExtendedConnect: Bool) throws {
return try RequestBlockValidator.validateBlock(self, supportsExtendedConnect: supportsExtendedConnect)
try RequestBlockValidator.validateBlock(self, supportsExtendedConnect: supportsExtendedConnect)
}
/// Checks that a given HPACKHeaders block is a valid response header block, meeting all of the constraints of RFC 7540.
///
/// If the header block is not valid, throws an error.
internal func validateResponseBlock() throws {
return try ResponseBlockValidator.validateBlock(self)
try ResponseBlockValidator.validateBlock(self)
}
/// Checks that a given HPACKHeaders block is a valid trailer block, meeting all of the constraints of RFC 7540.
///
/// If the header block is not valid, throws an error.
internal func validateTrailersBlock() throws {
return try TrailersValidator.validateBlock(self)
try TrailersValidator.validateBlock(self)
}
}
/// A HTTP/2 header block is divided into two sections: the leading section, containing pseudo-headers, and
/// the regular header section. Once the first regular header has been seen and we have transitioned into the
/// header section, it is an error to see a pseudo-header again in this block.
fileprivate enum BlockSection {
private enum BlockSection {
case pseudoHeaders
case headers
fileprivate mutating func validField(_ field: HeaderFieldName) throws {
switch (self, field.fieldType) {
case (.pseudoHeaders, .pseudoHeaderField),
(.headers, .regularHeaderField):
(.headers, .regularHeaderField):
// Another header of the same type we're expecting. Do nothing.
break
@@ -63,9 +62,8 @@ fileprivate enum BlockSection {
}
}
/// A `HeaderBlockValidator` is an object that can confirm that a HPACK block meets certain constraints.
fileprivate protocol HeaderBlockValidator {
private protocol HeaderBlockValidator {
init()
var allowedPseudoHeaderFields: PseudoHeaders { get }
@@ -75,7 +73,6 @@ fileprivate protocol HeaderBlockValidator {
mutating func validateNextField(name: HeaderFieldName, value: String, pseudoHeaderType: PseudoHeaders?) throws
}
extension HeaderBlockValidator {
/// Validates that a header block meets the requirements of this `HeaderBlockValidator`.
fileprivate static func validateBlock(_ block: HPACKHeaders, supportsExtendedConnect: Bool = false) throws {
@@ -88,29 +85,37 @@ extension HeaderBlockValidator {
try blockSection.validField(fieldName)
try fieldName.legalHeaderField(value: value)
let thisPseudoHeaderFieldType = try seenPseudoHeaders.seenNewHeaderField(fieldName, supportsExtendedConnect: supportsExtendedConnect)
let thisPseudoHeaderFieldType = try seenPseudoHeaders.seenNewHeaderField(
fieldName,
supportsExtendedConnect: supportsExtendedConnect
)
try validator.validateNextField(name: fieldName, value: value, pseudoHeaderType: thisPseudoHeaderFieldType)
}
// We must only have seen pseudo-header fields allowed on this type of header block,
// and at least the mandatory set.
guard validator.allowedPseudoHeaderFields.isSuperset(of: seenPseudoHeaders) &&
validator.mandatoryPseudoHeaderFields.isSubset(of: seenPseudoHeaders) else {
guard
validator.allowedPseudoHeaderFields.isSuperset(of: seenPseudoHeaders)
&& validator.mandatoryPseudoHeaderFields.isSubset(of: seenPseudoHeaders)
else {
throw NIOHTTP2Errors.invalidPseudoHeaders(block)
}
}
}
/// An object that can be used to validate if a given header block is a valid request header block.
fileprivate struct RequestBlockValidator {
private struct RequestBlockValidator {
private var isConnectRequest: Bool = false
private var containsProtocolPseudoHeader: Bool = false
}
extension RequestBlockValidator: HeaderBlockValidator {
fileprivate mutating func validateNextField(name: HeaderFieldName, value: String, pseudoHeaderType: PseudoHeaders?) throws {
fileprivate mutating func validateNextField(
name: HeaderFieldName,
value: String,
pseudoHeaderType: PseudoHeaders?
) throws {
// We have a wrinkle here: the set of allowed and mandatory pseudo headers for requests depends on whether this request is a CONNECT request.
// If it isn't, RFC 7540 § 8.1.2.3 rules, and says that:
//
@@ -192,40 +197,45 @@ extension RequestBlockValidator: HeaderBlockValidator {
}
}
/// An object that can be used to validate if a given header block is a valid response header block.
fileprivate struct ResponseBlockValidator {
private struct ResponseBlockValidator {
let allowedPseudoHeaderFields: PseudoHeaders = .allowedResponseHeaders
let mandatoryPseudoHeaderFields: PseudoHeaders = .mandatoryResponseHeaders
}
extension ResponseBlockValidator: HeaderBlockValidator {
fileprivate mutating func validateNextField(name: HeaderFieldName, value: String, pseudoHeaderType: PseudoHeaders?) throws {
fileprivate mutating func validateNextField(
name: HeaderFieldName,
value: String,
pseudoHeaderType: PseudoHeaders?
) throws {
return
}
}
/// An object that can be used to validate if a given header block is a valid trailer block.
fileprivate struct TrailersValidator {
private struct TrailersValidator {
let allowedPseudoHeaderFields: PseudoHeaders = []
let mandatoryPseudoHeaderFields: PseudoHeaders = []
}
extension TrailersValidator: HeaderBlockValidator {
fileprivate mutating func validateNextField(name: HeaderFieldName, value: String, pseudoHeaderType: PseudoHeaders?) throws {
fileprivate mutating func validateNextField(
name: HeaderFieldName,
value: String,
pseudoHeaderType: PseudoHeaders?
) throws {
return
}
}
/// A structure that carries the details of a specific header field name.
///
/// Used to validate the correctness of a specific header field name at a given
/// point in a header block.
fileprivate struct HeaderFieldName {
private struct HeaderFieldName {
/// The type of this header-field: pseudo-header or regular.
fileprivate var fieldType: FieldType
@@ -284,7 +294,6 @@ extension HeaderFieldName {
}
}
extension Substring.UTF8View {
/// Whether this is a valid HTTP/2 header field name.
fileprivate var isValidFieldName: Bool {
@@ -322,10 +331,10 @@ extension Substring.UTF8View {
///
/// We can then translate this into a straightforward switch statement to check whether the code
/// units are valid.
return self.allSatisfy { codeUnit in
self.allSatisfy { codeUnit in
switch codeUnit {
case 0x21, 0x23...0x27, 0x2a, 0x2b, 0x2d, 0x2e, 0x30...0x39,
0x5e...0x7a, 0x7c, 0x7e:
0x5e...0x7a, 0x7c, 0x7e:
return true
default:
return false
@@ -334,9 +343,8 @@ extension Substring.UTF8View {
}
}
/// A set of all pseudo-headers defined in HTTP/2.
fileprivate struct PseudoHeaders: OptionSet {
private struct PseudoHeaders: OptionSet {
var rawValue: UInt8
static let path = PseudoHeaders(rawValue: 1 << 0)
@@ -348,7 +356,9 @@ fileprivate struct PseudoHeaders: OptionSet {
static let mandatoryRequestHeaders: PseudoHeaders = [.path, .method, .scheme]
static let allowedRequestHeaders: PseudoHeaders = [.path, .method, .scheme, .authority]
static let allowedExtendedConnectRequestHeaders: PseudoHeaders = [.path, .method, .scheme, .authority, .extConnectProtocol]
static let allowedExtendedConnectRequestHeaders: PseudoHeaders = [
.path, .method, .scheme, .authority, .extConnectProtocol,
]
static let mandatoryConnectRequestHeaders: PseudoHeaders = [.method, .authority]
static let allowedConnectRequestHeaders: PseudoHeaders = [.method, .authority]
static let mandatoryResponseHeaders: PseudoHeaders = [.status]
@@ -106,7 +106,7 @@ extension NIOHTTP2Handler.InboundStreamMultiplexer {
case .inline(let inlineStreamMultiplexer):
inlineStreamMultiplexer.propagateChannelActive()
case .legacy:
break // do nothing
break // do nothing
}
}
@@ -115,7 +115,7 @@ extension NIOHTTP2Handler.InboundStreamMultiplexer {
case .inline(let inlineStreamMultiplexer):
inlineStreamMultiplexer.propagateChannelInactive()
case .legacy:
break // do nothing
break // do nothing
}
}
@@ -133,7 +133,7 @@ extension NIOHTTP2Handler.InboundStreamMultiplexer {
case .inline(let inlineStreamMultiplexer):
inlineStreamMultiplexer.propagateChannelWritabilityChanged()
case .legacy:
break // do nothing
break // do nothing
}
}
@@ -142,7 +142,7 @@ extension NIOHTTP2Handler.InboundStreamMultiplexer {
case .inline(let inlineStreamMultiplexer):
inlineStreamMultiplexer.propagateReadComplete()
case .legacy:
break // do nothing
break // do nothing
}
}
@@ -151,7 +151,7 @@ extension NIOHTTP2Handler.InboundStreamMultiplexer {
case .inline(let inlineStreamMultiplexer):
inlineStreamMultiplexer.processedFrame(frame: frame)
case .legacy:
break // do nothing
break // do nothing
}
}
}
@@ -164,7 +164,6 @@ internal struct LegacyInboundStreamMultiplexer {
let context: ChannelHandlerContext
}
extension LegacyInboundStreamMultiplexer: HTTP2InboundStreamMultiplexer {
func receivedFrame(_ frame: HTTP2Frame) {
self.context.fireChannelRead(NIOAny(frame))
@@ -26,7 +26,16 @@ internal struct InlineStreamMultiplexer {
/// The delegate to be notified upon stream creation and close.
private var streamDelegate: NIOHTTP2StreamDelegate?
init(context: ChannelHandlerContext, outboundView: NIOHTTP2Handler.OutboundView, mode: NIOHTTP2Handler.ParserMode, inboundStreamStateInitializer: MultiplexerAbstractChannel.InboundStreamStateInitializer, targetWindowSize: Int, streamChannelOutboundBytesHighWatermark: Int, streamChannelOutboundBytesLowWatermark: Int, streamDelegate: NIOHTTP2StreamDelegate?) {
init(
context: ChannelHandlerContext,
outboundView: NIOHTTP2Handler.OutboundView,
mode: NIOHTTP2Handler.ParserMode,
inboundStreamStateInitializer: MultiplexerAbstractChannel.InboundStreamStateInitializer,
targetWindowSize: Int,
streamChannelOutboundBytesHighWatermark: Int,
streamChannelOutboundBytesLowWatermark: Int,
streamDelegate: NIOHTTP2StreamDelegate?
) {
self.context = context
self._commonStreamMultiplexer = HTTP2CommonInboundStreamMultiplexer(
mode: mode,
@@ -101,7 +110,7 @@ extension InlineStreamMultiplexer: HTTP2OutboundStreamMultiplexer {
case .proceed:
self.outboundView.flush(context: self.context)
case .waitForReadsToComplete:
break // flush will be executed on `readComplete`
break // flush will be executed on `readComplete`
}
}
@@ -147,21 +156,35 @@ extension InlineStreamMultiplexer {
}
extension InlineStreamMultiplexer {
internal func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOChannelInitializer) {
self._commonStreamMultiplexer.createStreamChannel(multiplexer: .inline(self), promise: promise, streamStateInitializer)
internal func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOChannelInitializer
) {
self._commonStreamMultiplexer.createStreamChannel(
multiplexer: .inline(self),
promise: promise,
streamStateInitializer
)
}
internal func createStreamChannel(_ streamStateInitializer: @escaping NIOChannelInitializer) -> EventLoopFuture<Channel> {
internal func createStreamChannel(
_ streamStateInitializer: @escaping NIOChannelInitializer
) -> EventLoopFuture<Channel> {
self._commonStreamMultiplexer.createStreamChannel(multiplexer: .inline(self), streamStateInitializer)
}
@inlinable
internal func createStreamChannel<Output: Sendable>(promise: EventLoopPromise<Output>?, _ initializer: @escaping NIOChannelInitializerWithOutput<Output>) {
internal func createStreamChannel<Output: Sendable>(
promise: EventLoopPromise<Output>?,
_ initializer: @escaping NIOChannelInitializerWithOutput<Output>
) {
self._commonStreamMultiplexer.createStreamChannel(multiplexer: .inline(self), promise: promise, initializer)
}
@inlinable
internal func createStreamChannel<Output: Sendable>(_ initializer: @escaping NIOChannelInitializerWithOutput<Output>) -> EventLoopFuture<Output> {
internal func createStreamChannel<Output: Sendable>(
_ initializer: @escaping NIOChannelInitializerWithOutput<Output>
) -> EventLoopFuture<Output> {
self._commonStreamMultiplexer.createStreamChannel(multiplexer: .inline(self), initializer)
}
}
@@ -184,7 +207,10 @@ extension NIOHTTP2Handler {
/// Cannot be created by users.
internal init(_ inlineStreamMultiplexer: InlineStreamMultiplexer, eventLoop: EventLoop) {
self.inlineStreamMultiplexer = InlineStreamMultiplexer.SendableView(inlineStreamMultiplexer, eventLoop: eventLoop)
self.inlineStreamMultiplexer = InlineStreamMultiplexer.SendableView(
inlineStreamMultiplexer,
eventLoop: eventLoop
)
self.eventLoop = eventLoop
}
@@ -200,7 +226,10 @@ extension NIOHTTP2Handler {
/// failed if an error occurs.
/// - streamStateInitializer: A callback that will be invoked to allow you to configure the
/// `ChannelPipeline` for the newly created channel.
public func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping StreamInitializer) {
public func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping StreamInitializer
) {
self.inlineStreamMultiplexer.createStreamChannel(promise: promise, streamStateInitializer)
}
@@ -214,7 +243,9 @@ extension NIOHTTP2Handler {
/// - Parameter streamStateInitializer: A callback that will be invoked to allow you to configure the
/// `ChannelPipeline` for the newly created channel.
/// - Returns: An `EventLoopFuture` containing the created `Channel`, fulfilled after the supplied `streamStateInitializer` has been executed on it.
public func createStreamChannel(_ streamStateInitializer: @escaping StreamInitializer) -> EventLoopFuture<Channel> {
public func createStreamChannel(
_ streamStateInitializer: @escaping StreamInitializer
) -> EventLoopFuture<Channel> {
self.inlineStreamMultiplexer.createStreamChannel(streamStateInitializer)
}
}
@@ -238,7 +269,7 @@ extension NIOHTTP2Handler {
/// and `IOData`.
///
/// Inbound (remotely-initiated) streams are accessible via the ``inbound`` property, having been initialized and
/// returned as the `InboundStreamOutput` type.
/// returned as the `InboundStreamOutput` type.
///
/// You can open a stream by calling ``openStream(_:)``. Locally-initiated stream channel objects are initialized upon creation using the supplied `initializer` which returns a type
/// `Output`. This type may be `HTTP2Frame` or changed to any other type.
@@ -249,8 +280,16 @@ extension NIOHTTP2Handler {
// Cannot be created by users.
@usableFromInline
internal init(_ inlineStreamMultiplexer: InlineStreamMultiplexer, eventLoop: EventLoop, continuation: any AnyContinuation, inboundStreamChannels: NIOHTTP2AsyncSequence<InboundStreamOutput>) {
self._inlineStreamMultiplexer = InlineStreamMultiplexer.SendableView(inlineStreamMultiplexer, eventLoop: eventLoop)
internal init(
_ inlineStreamMultiplexer: InlineStreamMultiplexer,
eventLoop: EventLoop,
continuation: any AnyContinuation,
inboundStreamChannels: NIOHTTP2AsyncSequence<InboundStreamOutput>
) {
self._inlineStreamMultiplexer = InlineStreamMultiplexer.SendableView(
inlineStreamMultiplexer,
eventLoop: eventLoop
)
self._inlineStreamMultiplexer.setChannelContinuation(continuation)
self.inbound = inboundStreamChannels
}
@@ -259,8 +298,10 @@ extension NIOHTTP2Handler {
/// initializing the stream's `Channel`.
/// - Returns: The result of the `initializer`.
@inlinable
public func openStream<Output: Sendable>(_ initializer: @escaping NIOChannelInitializerWithOutput<Output>) async throws -> Output {
return try await self._inlineStreamMultiplexer.createStreamChannel(initializer).get()
public func openStream<Output: Sendable>(
_ initializer: @escaping NIOChannelInitializerWithOutput<Output>
) async throws -> Output {
try await self._inlineStreamMultiplexer.createStreamChannel(initializer).get()
}
}
}
@@ -280,7 +321,10 @@ extension InlineStreamMultiplexer {
self._eventLoop = eventLoop
}
internal func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOHTTP2Handler.StreamInitializer) {
internal func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOHTTP2Handler.StreamInitializer
) {
// Always create streams channels on the next event loop tick. This avoids re-entrancy
// issues where handlers interposed between the two HTTP/2 handlers could create streams
// in channel active which become activated twice.
@@ -301,7 +345,9 @@ extension InlineStreamMultiplexer {
}
@inlinable
internal func createStreamChannel<Output: Sendable>(_ initializer: @escaping NIOChannelInitializerWithOutput<Output>) -> EventLoopFuture<Output> {
internal func createStreamChannel<Output: Sendable>(
_ initializer: @escaping NIOChannelInitializerWithOutput<Output>
) -> EventLoopFuture<Output> {
// Always create streams channels on the next event loop tick. This avoids re-entrancy
// issues where handlers interposed between the two HTTP/2 handlers could create streams
// in channel active which become activated twice.
@@ -317,7 +363,7 @@ extension InlineStreamMultiplexer {
return self._inlineStreamMultiplexer.setChannelContinuation(streamChannels)
} else {
self._eventLoop.execute {
return self._inlineStreamMultiplexer.setChannelContinuation(streamChannels)
self._inlineStreamMultiplexer.setChannelContinuation(streamChannels)
}
}
}
+398 -184
View File
@@ -14,7 +14,6 @@
import NIOCore
import NIOHPACK
/// NIO's default settings used for initial settings values on HTTP/2 streams, when the user hasn't
/// overridden that. This limits the max concurrent streams to 100, and limits the max header list
/// size to 16kB, to avoid trivial resource exhaustion on NIO HTTP/2 users.
@@ -112,7 +111,7 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
@usableFromInline
internal var inboundStreamMultiplexer: InboundStreamMultiplexer? {
return self.inboundStreamMultiplexerState.multiplexer
self.inboundStreamMultiplexerState.multiplexer
}
/// Holds and tracks the state of the ``InboundStreamMultiplexer``.
@@ -141,43 +140,55 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
}
}
mutating func initialize(context: ChannelHandlerContext, http2Handler: NIOHTTP2Handler, mode: NIOHTTP2Handler.ParserMode) {
mutating func initialize(
context: ChannelHandlerContext,
http2Handler: NIOHTTP2Handler,
mode: NIOHTTP2Handler.ParserMode
) {
switch self {
case .deinitialized:
preconditionFailure("This multiplexer has been de-initialized without being reconfigured with a new context.")
preconditionFailure(
"This multiplexer has been de-initialized without being reconfigured with a new context."
)
case .uninitializedLegacy:
self = .initialized(.legacy(LegacyInboundStreamMultiplexer(context: context)))
case .uninitializedInline(let streamConfiguration, let inboundStreamInitializer, let streamDelegate):
self = .initialized(.inline(
InlineStreamMultiplexer(
context: context,
outboundView: .init(http2Handler: http2Handler),
mode: mode,
inboundStreamStateInitializer: .excludesStreamID(inboundStreamInitializer),
targetWindowSize: max(0, min(streamConfiguration.targetWindowSize, Int(Int32.max))),
streamChannelOutboundBytesHighWatermark: streamConfiguration.outboundBufferSizeHighWatermark,
streamChannelOutboundBytesLowWatermark: streamConfiguration.outboundBufferSizeLowWatermark,
streamDelegate: streamDelegate
self = .initialized(
.inline(
InlineStreamMultiplexer(
context: context,
outboundView: .init(http2Handler: http2Handler),
mode: mode,
inboundStreamStateInitializer: .excludesStreamID(inboundStreamInitializer),
targetWindowSize: max(0, min(streamConfiguration.targetWindowSize, Int(Int32.max))),
streamChannelOutboundBytesHighWatermark: streamConfiguration
.outboundBufferSizeHighWatermark,
streamChannelOutboundBytesLowWatermark: streamConfiguration.outboundBufferSizeLowWatermark,
streamDelegate: streamDelegate
)
)
))
)
case .uninitializedAsync(let streamConfiguration, let inboundStreamInitializer, let streamDelegate):
self = .initialized(.inline(
InlineStreamMultiplexer(
context: context,
outboundView: .init(http2Handler: http2Handler),
mode: mode,
inboundStreamStateInitializer: .returnsAny(inboundStreamInitializer),
targetWindowSize: max(0, min(streamConfiguration.targetWindowSize, Int(Int32.max))),
streamChannelOutboundBytesHighWatermark: streamConfiguration.outboundBufferSizeHighWatermark,
streamChannelOutboundBytesLowWatermark: streamConfiguration.outboundBufferSizeLowWatermark,
streamDelegate: streamDelegate
self = .initialized(
.inline(
InlineStreamMultiplexer(
context: context,
outboundView: .init(http2Handler: http2Handler),
mode: mode,
inboundStreamStateInitializer: .returnsAny(inboundStreamInitializer),
targetWindowSize: max(0, min(streamConfiguration.targetWindowSize, Int(Int32.max))),
streamChannelOutboundBytesHighWatermark: streamConfiguration
.outboundBufferSizeHighWatermark,
streamChannelOutboundBytesLowWatermark: streamConfiguration.outboundBufferSizeLowWatermark,
streamDelegate: streamDelegate
)
)
))
)
case .initialized:
break //no-op
break //no-op
}
}
}
@@ -207,20 +218,24 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
/// - initialSettings: The settings that will be advertised to the peer in the preamble. Defaults to ``nioDefaultSettings``.
/// - headerBlockValidation: Whether to validate sent and received HTTP/2 header blocks. Defaults to ``ValidationState/enabled``.
/// - contentLengthValidation: Whether to validate the content length of sent and received streams. Defaults to ``ValidationState/enabled``.
public convenience init(mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled) {
self.init(mode: mode,
eventLoop: nil,
initialSettings: initialSettings,
headerBlockValidation: headerBlockValidation,
contentLengthValidation: contentLengthValidation,
maximumSequentialEmptyDataFrames: 1,
maximumBufferedControlFrames: 10000,
maximumSequentialContinuationFrames: NIOHTTP2Handler.defaultMaximumSequentialContinuationFrames,
maximumResetFrameCount: 200,
resetFrameCounterWindow: .seconds(30))
public convenience init(
mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled
) {
self.init(
mode: mode,
eventLoop: nil,
initialSettings: initialSettings,
headerBlockValidation: headerBlockValidation,
contentLengthValidation: contentLengthValidation,
maximumSequentialEmptyDataFrames: 1,
maximumBufferedControlFrames: 10000,
maximumSequentialContinuationFrames: NIOHTTP2Handler.defaultMaximumSequentialContinuationFrames,
maximumResetFrameCount: 200,
resetFrameCounterWindow: .seconds(30)
)
}
/// Constructs a ``NIOHTTP2Handler``.
@@ -235,62 +250,84 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
/// - maximumBufferedControlFrames: Controls the maximum buffer size of buffered outbound control frames. If we are unable to send control frames as
/// fast as we produce them we risk building up an unbounded buffer and exhausting our memory. To protect against this DoS vector, we put an
/// upper limit on the depth of this queue. Defaults to 10,000.
public convenience init(mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled,
maximumSequentialEmptyDataFrames: Int = 1,
maximumBufferedControlFrames: Int = 10000) {
self.init(mode: mode,
eventLoop: nil,
initialSettings: initialSettings,
headerBlockValidation: headerBlockValidation,
contentLengthValidation: contentLengthValidation,
maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames,
maximumBufferedControlFrames: maximumBufferedControlFrames,
maximumSequentialContinuationFrames: NIOHTTP2Handler.defaultMaximumSequentialContinuationFrames,
maximumResetFrameCount: 200,
resetFrameCounterWindow: .seconds(30))
public convenience init(
mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled,
maximumSequentialEmptyDataFrames: Int = 1,
maximumBufferedControlFrames: Int = 10000
) {
self.init(
mode: mode,
eventLoop: nil,
initialSettings: initialSettings,
headerBlockValidation: headerBlockValidation,
contentLengthValidation: contentLengthValidation,
maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames,
maximumBufferedControlFrames: maximumBufferedControlFrames,
maximumSequentialContinuationFrames: NIOHTTP2Handler.defaultMaximumSequentialContinuationFrames,
maximumResetFrameCount: 200,
resetFrameCounterWindow: .seconds(30)
)
}
/// Constructs a ``NIOHTTP2Handler``.
///
///
/// - Parameters:
/// - mode: The mode for this handler, client or server.
/// - connectionConfiguration: The settings that will be used when establishing the connection.
/// - streamConfiguration: The settings that will be used when establishing new streams.
public convenience init(mode: ParserMode,
connectionConfiguration: ConnectionConfiguration = .init(),
streamConfiguration: StreamConfiguration = .init()) {
self.init(mode: mode,
eventLoop: nil,
initialSettings: connectionConfiguration.initialSettings,
headerBlockValidation: connectionConfiguration.headerBlockValidation,
contentLengthValidation: connectionConfiguration.contentLengthValidation,
maximumSequentialEmptyDataFrames: connectionConfiguration.maximumSequentialEmptyDataFrames,
maximumBufferedControlFrames: connectionConfiguration.maximumBufferedControlFrames,
maximumSequentialContinuationFrames: connectionConfiguration.maximumSequentialContinuationFrames,
maximumResetFrameCount: streamConfiguration.streamResetFrameRateLimit.maximumCount,
resetFrameCounterWindow: streamConfiguration.streamResetFrameRateLimit.windowLength)
public convenience init(
mode: ParserMode,
connectionConfiguration: ConnectionConfiguration = .init(),
streamConfiguration: StreamConfiguration = .init()
) {
self.init(
mode: mode,
eventLoop: nil,
initialSettings: connectionConfiguration.initialSettings,
headerBlockValidation: connectionConfiguration.headerBlockValidation,
contentLengthValidation: connectionConfiguration.contentLengthValidation,
maximumSequentialEmptyDataFrames: connectionConfiguration.maximumSequentialEmptyDataFrames,
maximumBufferedControlFrames: connectionConfiguration.maximumBufferedControlFrames,
maximumSequentialContinuationFrames: connectionConfiguration.maximumSequentialContinuationFrames,
maximumResetFrameCount: streamConfiguration.streamResetFrameRateLimit.maximumCount,
resetFrameCounterWindow: streamConfiguration.streamResetFrameRateLimit.windowLength
)
}
private init(mode: ParserMode,
eventLoop: EventLoop?,
initialSettings: HTTP2Settings,
headerBlockValidation: ValidationState,
contentLengthValidation: ValidationState,
maximumSequentialEmptyDataFrames: Int,
maximumBufferedControlFrames: Int,
maximumSequentialContinuationFrames: Int,
maximumResetFrameCount: Int,
resetFrameCounterWindow: TimeAmount) {
private init(
mode: ParserMode,
eventLoop: EventLoop?,
initialSettings: HTTP2Settings,
headerBlockValidation: ValidationState,
contentLengthValidation: ValidationState,
maximumSequentialEmptyDataFrames: Int,
maximumBufferedControlFrames: Int,
maximumSequentialContinuationFrames: Int,
maximumResetFrameCount: Int,
resetFrameCounterWindow: TimeAmount
) {
self._eventLoop = eventLoop
self.stateMachine = HTTP2ConnectionStateMachine(role: .init(mode), headerBlockValidation: .init(headerBlockValidation), contentLengthValidation: .init(contentLengthValidation))
self.stateMachine = HTTP2ConnectionStateMachine(
role: .init(mode),
headerBlockValidation: .init(headerBlockValidation),
contentLengthValidation: .init(contentLengthValidation)
)
self.mode = mode
self.initialSettings = initialSettings
self.outboundBuffer = CompoundOutboundBuffer(mode: mode, initialMaxOutboundStreams: 100, maxBufferedControlFrames: maximumBufferedControlFrames)
self.denialOfServiceValidator = DOSHeuristics(maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames, maximumResetFrameCount: maximumResetFrameCount, resetFrameCounterWindow: resetFrameCounterWindow)
self.outboundBuffer = CompoundOutboundBuffer(
mode: mode,
initialMaxOutboundStreams: 100,
maxBufferedControlFrames: maximumBufferedControlFrames
)
self.denialOfServiceValidator = DOSHeuristics(
maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames,
maximumResetFrameCount: maximumResetFrameCount,
resetFrameCounterWindow: resetFrameCounterWindow
)
self.tolerateImpossibleStateTransitionsInDebugMode = false
self.inboundStreamMultiplexerState = .uninitializedLegacy
self.maximumSequentialContinuationFrames = maximumSequentialContinuationFrames
@@ -315,22 +352,36 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
/// against this DoS vector we put an upper limit on this rate. Defaults to 200.
/// - resetFrameCounterWindow: Controls the sliding window used to enforce the maximum permitted reset frames rate. Too many may exhaust CPU resources. To protect
/// against this DoS vector we put an upper limit on this rate. 30 seconds.
internal init(mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled,
maximumSequentialEmptyDataFrames: Int = 1,
maximumBufferedControlFrames: Int = 10000,
maximumSequentialContinuationFrames: Int = NIOHTTP2Handler.defaultMaximumSequentialContinuationFrames,
tolerateImpossibleStateTransitionsInDebugMode: Bool = false,
maximumResetFrameCount: Int = 200,
resetFrameCounterWindow: TimeAmount = .seconds(30)) {
self.stateMachine = HTTP2ConnectionStateMachine(role: .init(mode), headerBlockValidation: .init(headerBlockValidation), contentLengthValidation: .init(contentLengthValidation))
internal init(
mode: ParserMode,
initialSettings: HTTP2Settings = nioDefaultSettings,
headerBlockValidation: ValidationState = .enabled,
contentLengthValidation: ValidationState = .enabled,
maximumSequentialEmptyDataFrames: Int = 1,
maximumBufferedControlFrames: Int = 10000,
maximumSequentialContinuationFrames: Int = NIOHTTP2Handler.defaultMaximumSequentialContinuationFrames,
tolerateImpossibleStateTransitionsInDebugMode: Bool = false,
maximumResetFrameCount: Int = 200,
resetFrameCounterWindow: TimeAmount = .seconds(30)
) {
self.stateMachine = HTTP2ConnectionStateMachine(
role: .init(mode),
headerBlockValidation: .init(headerBlockValidation),
contentLengthValidation: .init(contentLengthValidation)
)
self.mode = mode
self._eventLoop = nil
self.initialSettings = initialSettings
self.outboundBuffer = CompoundOutboundBuffer(mode: mode, initialMaxOutboundStreams: 100, maxBufferedControlFrames: maximumBufferedControlFrames)
self.denialOfServiceValidator = DOSHeuristics(maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames, maximumResetFrameCount: maximumResetFrameCount, resetFrameCounterWindow: resetFrameCounterWindow)
self.outboundBuffer = CompoundOutboundBuffer(
mode: mode,
initialMaxOutboundStreams: 100,
maxBufferedControlFrames: maximumBufferedControlFrames
)
self.denialOfServiceValidator = DOSHeuristics(
maximumSequentialEmptyDataFrames: maximumSequentialEmptyDataFrames,
maximumResetFrameCount: maximumResetFrameCount,
resetFrameCounterWindow: resetFrameCounterWindow
)
self.tolerateImpossibleStateTransitionsInDebugMode = tolerateImpossibleStateTransitionsInDebugMode
self.inboundStreamMultiplexerState = .uninitializedLegacy
self.maximumSequentialContinuationFrames = maximumSequentialContinuationFrames
@@ -349,7 +400,11 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
if context.channel.isActive {
// We jump immediately to activated here, as channelActive has probably already passed.
if self.activationState != .idle {
self.impossibleActivationStateTransition(state: self.activationState, activating: true, context: context)
self.impossibleActivationStateTransition(
state: self.activationState,
activating: true,
context: context
)
}
self.activationState = .activated
self.writeAndFlushPreamble(context: context)
@@ -391,7 +446,9 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
//
// We'll throw an error and then close. In debug builds, we crash.
self.impossibleActivationStateTransition(
state: self.activationState, activating: true, context: context
state: self.activationState,
activating: true,
context: context
)
return
}
@@ -418,7 +475,9 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
// - .activated implies that the above code didn't prevent us re-entrantly getting to this point.
// - .inactive implies that somehow we hit channelInactive but didn't enter .inactiveWhileActivating.
self.impossibleActivationStateTransition(
state: self.activationState, activating: true, context: context
state: self.activationState,
activating: true,
context: context
)
}
}
@@ -493,7 +552,6 @@ public final class NIOHTTP2Handler: ChannelDuplexHandler {
}
}
/// Inbound frame handling.
extension NIOHTTP2Handler {
/// Spins over the frame decoder parsing frames and sending them down the channel pipeline.
@@ -510,16 +568,32 @@ extension NIOHTTP2Handler {
do {
return try self.frameDecoder.nextFrame()
} catch InternalError.codecError(let code) {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.unableToParseFrame(), reason: code)
self.inboundConnectionErrorTriggered(
context: context,
underlyingError: NIOHTTP2Errors.unableToParseFrame(),
reason: code
)
return nil
} catch is NIOHTTP2Errors.BadClientMagic {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.badClientMagic(), reason: .protocolError)
self.inboundConnectionErrorTriggered(
context: context,
underlyingError: NIOHTTP2Errors.badClientMagic(),
reason: .protocolError
)
return nil
} catch is NIOHTTP2Errors.ExcessivelyLargeHeaderBlock {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.excessivelyLargeHeaderBlock(), reason: .protocolError)
self.inboundConnectionErrorTriggered(
context: context,
underlyingError: NIOHTTP2Errors.excessivelyLargeHeaderBlock(),
reason: .protocolError
)
return nil
} catch is NIOHTTP2Errors.ExcessiveContinuationFrames {
self.inboundConnectionErrorTriggered(context: context, underlyingError: NIOHTTP2Errors.excessiveContinuationFrames(), reason: .enhanceYourCalm)
self.inboundConnectionErrorTriggered(
context: context,
underlyingError: NIOHTTP2Errors.excessiveContinuationFrames(),
reason: .enhanceYourCalm
)
return nil
} catch {
self.inboundConnectionErrorTriggered(context: context, underlyingError: error, reason: .internalError)
@@ -532,7 +606,11 @@ extension NIOHTTP2Handler {
case stop
}
private func processFrame(_ frame: HTTP2Frame, flowControlledLength: Int, context: ChannelHandlerContext) -> FrameProcessResult {
private func processFrame(
_ frame: HTTP2Frame,
flowControlledLength: Int,
context: ChannelHandlerContext
) -> FrameProcessResult {
// All frames have one basic processing step: do we send them on, or drop them?
// Some frames have further processing steps, regarding triggering user events or other operations.
// Here we centralise this processing.
@@ -544,18 +622,30 @@ extension NIOHTTP2Handler {
case .origin(let origins):
result = self.stateMachine.receiveOrigin(origins: origins)
case .data(let dataBody):
result = self.stateMachine.receiveData(streamID: frame.streamID, contentLength: dataBody.data.readableBytes, flowControlledBytes: flowControlledLength, isEndStreamSet: dataBody.endStream)
result = self.stateMachine.receiveData(
streamID: frame.streamID,
contentLength: dataBody.data.readableBytes,
flowControlledBytes: flowControlledLength,
isEndStreamSet: dataBody.endStream
)
case .goAway(let lastStreamID, _, _):
result = self.stateMachine.receiveGoaway(lastStreamID: lastStreamID)
case .headers(let headerBody):
result = self.stateMachine.receiveHeaders(streamID: frame.streamID, headers: headerBody.headers, isEndStreamSet: headerBody.endStream)
result = self.stateMachine.receiveHeaders(
streamID: frame.streamID,
headers: headerBody.headers,
isEndStreamSet: headerBody.endStream
)
// Apply a priority update if one is here. If this fails, it may cause a connection error.
if let priorityData = headerBody.priorityData {
do {
try self.outboundBuffer.priorityUpdate(streamID: frame.streamID, priorityData: priorityData)
} catch {
result = StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
result = StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
}
@@ -577,23 +667,36 @@ extension NIOHTTP2Handler {
do {
try self.outboundBuffer.priorityUpdate(streamID: frame.streamID, priorityData: priorityData)
} catch {
result = StateMachineResultWithEffect(result: .connectionError(underlyingError: error, type: .protocolError), effect: nil)
result = StateMachineResultWithEffect(
result: .connectionError(underlyingError: error, type: .protocolError),
effect: nil
)
}
case .pushPromise(let pushedStreamData):
result = self.stateMachine.receivePushPromise(originalStreamID: frame.streamID, childStreamID: pushedStreamData.pushedStreamID, headers: pushedStreamData.headers)
result = self.stateMachine.receivePushPromise(
originalStreamID: frame.streamID,
childStreamID: pushedStreamData.pushedStreamID,
headers: pushedStreamData.headers
)
case .rstStream(let reason):
result = self.stateMachine.receiveRstStream(streamID: frame.streamID, reason: reason)
case .settings(let newSettings):
let (stateMachineResult, postSettingsOperation) = self.stateMachine.receiveSettings(newSettings,
frameEncoder: &self.frameEncoder,
frameDecoder: &self.frameDecoder)
let (stateMachineResult, postSettingsOperation) = self.stateMachine.receiveSettings(
newSettings,
frameEncoder: &self.frameEncoder,
frameDecoder: &self.frameDecoder
)
result = stateMachineResult
switch postSettingsOperation {
case .nothing:
break
case .sendAck:
self.writeBufferedFrame(context: context, frame: HTTP2Frame(streamID: .rootStream, payload: .settings(.ack)), promise: nil)
self.writeBufferedFrame(
context: context,
frame: HTTP2Frame(streamID: .rootStream, payload: .settings(.ack)),
promise: nil
)
}
case .windowUpdate(let increment):
@@ -618,7 +721,12 @@ extension NIOHTTP2Handler {
returnValue = .stop
case .streamError(let streamID, let underlyingError, let errorCode):
// We can continue parsing on stream errors in most cases, the frame is just ignored.
self.inboundStreamErrorTriggered(context: context, streamID: streamID, underlyingError: underlyingError, reason: errorCode)
self.inboundStreamErrorTriggered(
context: context,
streamID: streamID,
underlyingError: underlyingError,
reason: errorCode
)
returnValue = .continue
}
@@ -630,19 +738,31 @@ extension NIOHTTP2Handler {
}
/// A connection error was hit while receiving a frame.
private func inboundConnectionErrorTriggered(context: ChannelHandlerContext, underlyingError: Error, reason: HTTP2ErrorCode) {
private func inboundConnectionErrorTriggered(
context: ChannelHandlerContext,
underlyingError: Error,
reason: HTTP2ErrorCode
) {
// A connection error brings the entire connection down. We attempt to write a GOAWAY frame, and then report this
// error. It's possible that we'll be unable to write the GOAWAY frame, but that also just logs the error.
// Because we don't know what data the user handled before we got this, we propose that they may have seen all of it.
// The user may choose to fire a more specific error if they wish.
let goAwayFrame = HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: .maxID, errorCode: reason, opaqueData: nil))
let goAwayFrame = HTTP2Frame(
streamID: .rootStream,
payload: .goAway(lastStreamID: .maxID, errorCode: reason, opaqueData: nil)
)
self.writeUnbufferedFrame(context: context, frame: goAwayFrame)
self.flushIfNecessary(context: context)
context.fireErrorCaught(underlyingError)
}
/// A stream error was hit while receiving a frame.
private func inboundStreamErrorTriggered(context: ChannelHandlerContext, streamID: HTTP2StreamID, underlyingError: Error, reason: HTTP2ErrorCode) {
private func inboundStreamErrorTriggered(
context: ChannelHandlerContext,
streamID: HTTP2StreamID,
underlyingError: Error,
reason: HTTP2ErrorCode
) {
// A stream error brings down a single stream, causing a RST_STREAM frame. We attempt to write this, and then report
// the error. It's possible that we'll be unable to write this, which will likely escalate this error, but that's
// the user's issue.
@@ -684,7 +804,6 @@ extension NIOHTTP2Handler.InboundStreamMultiplexer {
}
}
/// Outbound frame handling.
extension NIOHTTP2Handler {
/// Issues the preamble when necessary.
@@ -699,15 +818,23 @@ extension NIOHTTP2Handler {
context.write(self.wrapOutboundOut(.byteBuffer(self.writeBuffer)), promise: nil)
}
let initialSettingsFrame = HTTP2Frame(streamID: .rootStream, payload: .settings(.settings(self.initialSettings)))
let initialSettingsFrame = HTTP2Frame(
streamID: .rootStream,
payload: .settings(.settings(self.initialSettings))
)
self.writeUnbufferedFrame(context: context, frame: initialSettingsFrame)
self.flushIfNecessary(context: context)
}
/// Write a frame that is allowed to be buffered (that is, that participates in the outbound frame buffer).
private func writeBufferedFrame(context: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise<Void>?) {
private func writeBufferedFrame(context: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise<Void>?)
{
do {
switch try self.outboundBuffer.processOutboundFrame(frame, promise: promise, channelWritable: self.channelWritable) {
switch try self.outboundBuffer.processOutboundFrame(
frame,
promise: promise,
channelWritable: self.channelWritable
) {
case .nothing:
// Nothing to do, got buffered.
break
@@ -745,7 +872,11 @@ extension NIOHTTP2Handler {
self.processOutboundFrame(context: context, frame: frame, promise: nil)
}
private func processOutboundFrame(context: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise<Void>?) {
private func processOutboundFrame(
context: ChannelHandlerContext,
frame: HTTP2Frame,
promise: EventLoopPromise<Void>?
) {
let result: StateMachineResultWithEffect
switch frame.payload {
@@ -757,17 +888,30 @@ extension NIOHTTP2Handler {
self.stateMachine.sendOrigin(origins: origins)
case .data(let data):
// TODO(cory): Correctly account for padding data.
result = self.stateMachine.sendData(streamID: frame.streamID, contentLength: data.data.readableBytes, flowControlledBytes: data.data.readableBytes, isEndStreamSet: data.endStream)
result = self.stateMachine.sendData(
streamID: frame.streamID,
contentLength: data.data.readableBytes,
flowControlledBytes: data.data.readableBytes,
isEndStreamSet: data.endStream
)
case .goAway(let lastStreamID, _, _):
result = self.stateMachine.sendGoaway(lastStreamID: lastStreamID)
case .headers(let headerContent):
result = self.stateMachine.sendHeaders(streamID: frame.streamID, headers: headerContent.headers, isEndStreamSet: headerContent.endStream)
result = self.stateMachine.sendHeaders(
streamID: frame.streamID,
headers: headerContent.headers,
isEndStreamSet: headerContent.endStream
)
case .ping:
result = self.stateMachine.sendPing()
case .priority:
result = self.stateMachine.sendPriority()
case .pushPromise(let pushedContent):
result = self.stateMachine.sendPushPromise(originalStreamID: frame.streamID, childStreamID: pushedContent.pushedStreamID, headers: pushedContent.headers)
result = self.stateMachine.sendPushPromise(
originalStreamID: frame.streamID,
childStreamID: pushedContent.pushedStreamID,
headers: pushedContent.headers
)
case .rstStream(let reason):
result = self.stateMachine.sendRstStream(streamID: frame.streamID, reason: reason)
case .settings(.settings(let newSettings)):
@@ -792,8 +936,13 @@ extension NIOHTTP2Handler {
case .connectionError(let underlyingError, _):
self.outboundConnectionErrorTriggered(context: context, promise: promise, underlyingError: underlyingError)
return
case .streamError(streamID: let streamID, underlyingError: let underlyingError, _):
self.outboundStreamErrorTriggered(context: context, promise: promise, streamID: streamID, underlyingError: underlyingError)
case .streamError(let streamID, let underlyingError, _):
self.outboundStreamErrorTriggered(
context: context,
promise: promise,
streamID: streamID,
underlyingError: underlyingError
)
return
case .succeed:
self.writeBuffer.clear()
@@ -805,13 +954,21 @@ extension NIOHTTP2Handler {
}
/// Encodes a frame and writes it to the network.
private func encodeAndWriteFrame(context: ChannelHandlerContext, frame: HTTP2Frame, promise: EventLoopPromise<Void>?) {
private func encodeAndWriteFrame(
context: ChannelHandlerContext,
frame: HTTP2Frame,
promise: EventLoopPromise<Void>?
) {
let extraFrameData: IOData?
do {
extraFrameData = try self.frameEncoder.encode(frame: frame, to: &self.writeBuffer)
} catch InternalError.codecError {
self.outboundConnectionErrorTriggered(context: context, promise: promise, underlyingError: NIOHTTP2Errors.unableToSerializeFrame())
self.outboundConnectionErrorTriggered(
context: context,
promise: promise,
underlyingError: NIOHTTP2Errors.unableToSerializeFrame()
)
return
} catch {
self.outboundConnectionErrorTriggered(context: context, promise: promise, underlyingError: error)
@@ -830,19 +987,27 @@ extension NIOHTTP2Handler {
}
/// A connection error was hit while attempting to send a frame.
private func outboundConnectionErrorTriggered(context: ChannelHandlerContext, promise: EventLoopPromise<Void>?, underlyingError: Error) {
private func outboundConnectionErrorTriggered(
context: ChannelHandlerContext,
promise: EventLoopPromise<Void>?,
underlyingError: Error
) {
promise?.fail(underlyingError)
context.fireErrorCaught(underlyingError)
}
/// A stream error was hit while attempting to send a frame.
private func outboundStreamErrorTriggered(context: ChannelHandlerContext, promise: EventLoopPromise<Void>?, streamID: HTTP2StreamID, underlyingError: Error) {
private func outboundStreamErrorTriggered(
context: ChannelHandlerContext,
promise: EventLoopPromise<Void>?,
streamID: HTTP2StreamID,
underlyingError: Error
) {
promise?.fail(underlyingError)
self.inboundStreamMultiplexer?.streamError(streamID: streamID, error: underlyingError)
}
}
// MARK:- Helpers
extension NIOHTTP2Handler {
private func processStateChange(_ stateChange: NIOHTTP2ConnectionStateChange?) {
@@ -853,17 +1018,35 @@ extension NIOHTTP2Handler {
switch stateChange {
case .streamClosed(let streamClosedData):
self.outboundBuffer.connectionWindowSize = streamClosedData.localConnectionWindowSize
self.inboundEventBuffer.pendingUserEvent(StreamClosedEvent(streamID: streamClosedData.streamID, reason: streamClosedData.reason))
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2WindowUpdatedEvent(streamID: .rootStream, inboundWindowSize: streamClosedData.remoteConnectionWindowSize, outboundWindowSize: streamClosedData.localConnectionWindowSize))
self.inboundEventBuffer.pendingUserEvent(
StreamClosedEvent(streamID: streamClosedData.streamID, reason: streamClosedData.reason)
)
self.inboundEventBuffer.pendingUserEvent(
NIOHTTP2WindowUpdatedEvent(
streamID: .rootStream,
inboundWindowSize: streamClosedData.remoteConnectionWindowSize,
outboundWindowSize: streamClosedData.localConnectionWindowSize
)
)
let droppedPromises = self.outboundBuffer.streamClosed(streamClosedData.streamID)
self.failDroppedPromises(droppedPromises, streamID: streamClosedData.streamID, errorCode: streamClosedData.reason ?? .cancel)
self.failDroppedPromises(
droppedPromises,
streamID: streamClosedData.streamID,
errorCode: streamClosedData.reason ?? .cancel
)
case .streamCreated(let streamCreatedData):
self.outboundBuffer.streamCreated(streamCreatedData.streamID, initialWindowSize: streamCreatedData.localStreamWindowSize.map(UInt32.init) ?? 0)
self.outboundBuffer.streamCreated(
streamCreatedData.streamID,
initialWindowSize: streamCreatedData.localStreamWindowSize.map(UInt32.init) ?? 0
)
self.inboundEventBuffer.pendingUserEvent(
NIOHTTP2StreamCreatedEvent(streamID: streamCreatedData.streamID,
localInitialWindowSize: streamCreatedData.localStreamWindowSize.map(UInt32.init),
remoteInitialWindowSize: streamCreatedData.remoteStreamWindowSize.map(UInt32.init)))
NIOHTTP2StreamCreatedEvent(
streamID: streamCreatedData.streamID,
localInitialWindowSize: streamCreatedData.localStreamWindowSize.map(UInt32.init),
remoteInitialWindowSize: streamCreatedData.remoteStreamWindowSize.map(UInt32.init)
)
)
case .bulkStreamClosure(let streamClosureData):
for droppedStream in streamClosureData.closedStreams {
self.inboundEventBuffer.pendingUserEvent(StreamClosedEvent(streamID: droppedStream, reason: .cancel))
@@ -873,10 +1056,25 @@ extension NIOHTTP2Handler {
}
case .flowControlChange(let change):
self.outboundBuffer.connectionWindowSize = change.localConnectionWindowSize
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2WindowUpdatedEvent(streamID: .rootStream, inboundWindowSize: change.remoteConnectionWindowSize, outboundWindowSize: change.localConnectionWindowSize))
self.inboundEventBuffer.pendingUserEvent(
NIOHTTP2WindowUpdatedEvent(
streamID: .rootStream,
inboundWindowSize: change.remoteConnectionWindowSize,
outboundWindowSize: change.localConnectionWindowSize
)
)
if let streamSize = change.localStreamWindowSize {
self.outboundBuffer.updateStreamWindow(streamSize.streamID, newSize: streamSize.localStreamWindowSize.map(Int32.init) ?? 0)
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2WindowUpdatedEvent(streamID: streamSize.streamID, inboundWindowSize: streamSize.remoteStreamWindowSize, outboundWindowSize: streamSize.localStreamWindowSize))
self.outboundBuffer.updateStreamWindow(
streamSize.streamID,
newSize: streamSize.localStreamWindowSize.map(Int32.init) ?? 0
)
self.inboundEventBuffer.pendingUserEvent(
NIOHTTP2WindowUpdatedEvent(
streamID: streamSize.streamID,
inboundWindowSize: streamSize.remoteStreamWindowSize,
outboundWindowSize: streamSize.localStreamWindowSize
)
)
}
case .streamCreatedAndClosed(let cAndCData):
self.outboundBuffer.streamCreated(cAndCData.streamID, initialWindowSize: 0)
@@ -895,7 +1093,9 @@ extension NIOHTTP2Handler {
}
case .localSettingsChanged(let settingsChange):
if settingsChange.streamWindowSizeChange != 0 {
self.inboundEventBuffer.pendingUserEvent(NIOHTTP2BulkStreamWindowChangeEvent(delta: settingsChange.streamWindowSizeChange))
self.inboundEventBuffer.pendingUserEvent(
NIOHTTP2BulkStreamWindowChangeEvent(delta: settingsChange.streamWindowSizeChange)
)
}
if let newMaxFrameSize = settingsChange.newMaxFrameSize {
self.frameDecoder.maxFrameSize = newMaxFrameSize
@@ -938,10 +1138,13 @@ extension NIOHTTP2Handler {
}
/// Fails any promises in the given collection with a 'StreamClosed' error.
private func failDroppedPromises(_ promises: CompoundOutboundBuffer.DroppedPromisesCollection,
streamID: HTTP2StreamID,
errorCode: HTTP2ErrorCode,
file: String = #fileID, line: UInt = #line) {
private func failDroppedPromises(
_ promises: CompoundOutboundBuffer.DroppedPromisesCollection,
streamID: HTTP2StreamID,
errorCode: HTTP2ErrorCode,
file: String = #fileID,
line: UInt = #line
) {
// 'NIOHTTP2Errors.streamClosed' always allocates, if there are no promises then there's no
// need to create the error.
guard promises.contains(where: { $0 != nil }) else {
@@ -955,9 +1158,8 @@ extension NIOHTTP2Handler {
}
}
private extension HTTP2ConnectionStateMachine.ConnectionRole {
init(_ role: NIOHTTP2Handler.ParserMode) {
extension HTTP2ConnectionStateMachine.ConnectionRole {
fileprivate init(_ role: NIOHTTP2Handler.ParserMode) {
switch role {
case .client:
self = .client
@@ -967,7 +1169,6 @@ private extension HTTP2ConnectionStateMachine.ConnectionRole {
}
}
extension HTTP2ConnectionStateMachine.ValidationState {
init(_ state: NIOHTTP2Handler.ValidationState) {
switch state {
@@ -979,37 +1180,34 @@ extension HTTP2ConnectionStateMachine.ValidationState {
}
}
extension NIOHTTP2Handler: CustomStringConvertible {
public var description: String {
return "NIOHTTP2Handler(mode: \(String(describing: self.mode)))"
"NIOHTTP2Handler(mode: \(String(describing: self.mode)))"
}
}
extension NIOHTTP2Handler: CustomDebugStringConvertible {
public var debugDescription: String {
return """
NIOHTTP2Handler(
stateMachine: \(String(describing: self.stateMachine)),
frameDecoder: \(String(describing: self.frameDecoder)),
frameEncoder: \(String(describing: self.frameEncoder)),
writeBuffer: \(String(describing: self.writeBuffer)),
inboundEventBuffer: \(String(describing: self.inboundEventBuffer)),
outboundBuffer: \(String(describing: self.outboundBuffer)),
wroteFrame: \(String(describing: self.wroteFrame)),
denialOfServiceValidator: \(String(describing: self.denialOfServiceValidator)),
mode: \(String(describing: self.mode)),
initialSettings: \(String(describing: self.initialSettings)),
channelClosed: \(String(describing: self.channelClosed)),
channelWritable: \(String(describing: self.channelWritable)),
activationState: \(String(describing: self.activationState))
)
"""
"""
NIOHTTP2Handler(
stateMachine: \(String(describing: self.stateMachine)),
frameDecoder: \(String(describing: self.frameDecoder)),
frameEncoder: \(String(describing: self.frameEncoder)),
writeBuffer: \(String(describing: self.writeBuffer)),
inboundEventBuffer: \(String(describing: self.inboundEventBuffer)),
outboundBuffer: \(String(describing: self.outboundBuffer)),
wroteFrame: \(String(describing: self.wroteFrame)),
denialOfServiceValidator: \(String(describing: self.denialOfServiceValidator)),
mode: \(String(describing: self.mode)),
initialSettings: \(String(describing: self.initialSettings)),
channelClosed: \(String(describing: self.channelClosed)),
channelWritable: \(String(describing: self.channelWritable)),
activationState: \(String(describing: self.activationState))
)
"""
}
}
extension NIOHTTP2Handler {
/// Tracks the state of activation of the handler.
enum ActivationState {
@@ -1031,7 +1229,9 @@ extension NIOHTTP2Handler {
}
fileprivate func impossibleActivationStateTransition(
state: ActivationState, activating: Bool, context: ChannelHandlerContext
state: ActivationState,
activating: Bool,
context: ChannelHandlerContext
) {
assert(self.tolerateImpossibleStateTransitionsInDebugMode, "Unexpected channelActive in state \(state)")
context.fireErrorCaught(NIOHTTP2Errors.ActivationError(state: state, activating: activating))
@@ -1039,7 +1239,6 @@ extension NIOHTTP2Handler {
}
}
extension NIOHTTP2Handler {
/// Exposes restricted API for use by the multiplexer
internal struct OutboundView {
@@ -1059,7 +1258,6 @@ extension NIOHTTP2Handler {
}
}
extension NIOHTTP2Handler {
/// The type of all `inboundStreamInitializer` callbacks which do not need to return data.
public typealias StreamInitializer = NIOChannelInitializer
@@ -1100,7 +1298,11 @@ extension NIOHTTP2Handler {
resetFrameCounterWindow: streamConfiguration.streamResetFrameRateLimit.windowLength
)
self.inboundStreamMultiplexerState = .uninitializedInline(streamConfiguration, inboundStreamInitializer, streamDelegate)
self.inboundStreamMultiplexerState = .uninitializedInline(
streamConfiguration,
inboundStreamInitializer,
streamDelegate
)
}
@usableFromInline
@@ -1124,7 +1326,11 @@ extension NIOHTTP2Handler {
maximumResetFrameCount: streamConfiguration.streamResetFrameRateLimit.maximumCount,
resetFrameCounterWindow: streamConfiguration.streamResetFrameRateLimit.windowLength
)
self.inboundStreamMultiplexerState = .uninitializedAsync(streamConfiguration, inboundStreamInitializerWithAnyOutput, streamDelegate)
self.inboundStreamMultiplexerState = .uninitializedAsync(
streamConfiguration,
inboundStreamInitializerWithAnyOutput,
streamDelegate
)
}
/// Connection-level configuration.
@@ -1151,7 +1357,7 @@ extension NIOHTTP2Handler {
public var streamResetFrameRateLimit: StreamResetFrameRateLimitConfiguration = .init()
public init() {}
}
/// Stream reset frame rate limit configuration.
///
/// The settings that control the maximum permitted reset frames within a given time window. Too many may exhaust CPU resources.
@@ -1189,12 +1395,12 @@ extension NIOHTTP2Handler {
// We need to return a future here so that we can synchronize access on the underlying `self.inboundStreamMultiplexer`
if self._eventLoop!.inEventLoop {
return self._eventLoop!.makeCompletedFuture {
return try self.syncMultiplexer()
try self.syncMultiplexer()
}
} else {
let unsafeSelf = UnsafeTransfer(self)
return self._eventLoop!.submit {
return try unsafeSelf.wrappedValue.syncMultiplexer()
try unsafeSelf.wrappedValue.syncMultiplexer()
}
}
}
@@ -1220,7 +1426,10 @@ extension NIOHTTP2Handler {
@inlinable
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
internal func syncAsyncStreamMultiplexer<Output: Sendable>(continuation: any AnyContinuation, inboundStreamChannels: NIOHTTP2AsyncSequence<Output>) throws -> AsyncStreamMultiplexer<Output> {
internal func syncAsyncStreamMultiplexer<Output: Sendable>(
continuation: any AnyContinuation,
inboundStreamChannels: NIOHTTP2AsyncSequence<Output>
) throws -> AsyncStreamMultiplexer<Output> {
self._eventLoop!.preconditionInEventLoop()
guard let inboundStreamMultiplexer = self.inboundStreamMultiplexer else {
throw NIOHTTP2Errors.missingMultiplexer()
@@ -1228,7 +1437,12 @@ extension NIOHTTP2Handler {
switch inboundStreamMultiplexer {
case let .inline(multiplexer):
return AsyncStreamMultiplexer(multiplexer, eventLoop: self._eventLoop!, continuation: continuation, inboundStreamChannels: inboundStreamChannels)
return AsyncStreamMultiplexer(
multiplexer,
eventLoop: self._eventLoop!,
continuation: continuation,
inboundStreamChannels: inboundStreamChannels
)
case .legacy:
throw NIOHTTP2Errors.missingMultiplexer()
}
@@ -68,7 +68,11 @@ internal class HTTP2CommonInboundStreamMultiplexer {
// MARK:- inbound multiplexer functions
// note this is intentionally not bound to `HTTP2InboundStreamMultiplexer` to allow for freedom in modifying the shared driver function signatures
extension HTTP2CommonInboundStreamMultiplexer {
func receivedFrame(_ frame: HTTP2Frame, context: ChannelHandlerContext, multiplexer: HTTP2StreamChannel.OutboundStreamMultiplexer) {
func receivedFrame(
_ frame: HTTP2Frame,
context: ChannelHandlerContext,
multiplexer: HTTP2StreamChannel.OutboundStreamMultiplexer
) {
self._channel.eventLoop.preconditionInEventLoop()
self.isReading = true
@@ -333,7 +337,7 @@ extension HTTP2CommonInboundStreamMultiplexer {
self._pendingStreams[channel.channelID] = channel
let anyInitializer: NIOChannelInitializerWithOutput<any Sendable> = { channel in
streamStateInitializer(channel).map { return $0 }
streamStateInitializer(channel).map { $0 }
}
let anyPromise: EventLoopPromise<(any Sendable)>?
@@ -390,13 +394,18 @@ extension HTTP2CommonInboundStreamMultiplexer {
internal func createStreamChannel(
multiplexer: HTTP2StreamChannel.OutboundStreamMultiplexer,
_ streamStateInitializer: @escaping NIOChannelInitializer) -> EventLoopFuture<Channel> {
_ streamStateInitializer: @escaping NIOChannelInitializer
) -> EventLoopFuture<Channel> {
let promise = self._channel.eventLoop.makePromise(of: Channel.self)
self.createStreamChannel(multiplexer: multiplexer, promise: promise, streamStateInitializer)
return promise.futureResult
}
@available(*, deprecated, message: "The signature of 'streamStateInitializer' has changed to '(Channel) -> EventLoopFuture<Void>'")
@available(
*,
deprecated,
message: "The signature of 'streamStateInitializer' has changed to '(Channel) -> EventLoopFuture<Void>'"
)
internal func createStreamChannel(
multiplexer: HTTP2StreamChannel.OutboundStreamMultiplexer,
promise: EventLoopPromise<Channel>?,
@@ -453,7 +462,6 @@ internal protocol AnyContinuation: Sendable {
func finish(throwing error: Error)
}
/// `NIOHTTP2AsyncSequence` is an implementation of the `AsyncSequence` protocol which allows iteration over a generic
/// element type `Output`.
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
@@ -504,18 +512,20 @@ extension NIOHTTP2AsyncSequence {
@usableFromInline
func yield(any: Any) {
let yieldResult = self.continuation.yield(any as! Output)
switch yieldResult {
case .enqueued:
break // success, nothing to do
case .terminated:
// this can happen if the task has been cancelled
// we can't do better than dropping the message at the moment
break
case .dropped:
preconditionFailure("Attempted to yield when AsyncThrowingStream is over capacity. This shouldn't be possible for an unbounded stream.")
default:
preconditionFailure("Attempt to yield to AsyncThrowingStream failed for unhandled reason.")
}
switch yieldResult {
case .enqueued:
break // success, nothing to do
case .terminated:
// this can happen if the task has been cancelled
// we can't do better than dropping the message at the moment
break
case .dropped:
preconditionFailure(
"Attempted to yield when AsyncThrowingStream is over capacity. This shouldn't be possible for an unbounded stream."
)
default:
preconditionFailure("Attempt to yield to AsyncThrowingStream failed for unhandled reason.")
}
}
/// `finish` marks the continuation as finished.
@@ -531,7 +541,6 @@ extension NIOHTTP2AsyncSequence {
}
}
/// `initialize` creates a new `Continuation` object and returns it along with its backing ``NIOHTTP2AsyncSequence``.
/// The `Continuation` provides the ability to yield to the backing .``NIOHTTP2AsyncSequence``.
///
@@ -539,7 +548,9 @@ extension NIOHTTP2AsyncSequence {
/// - inboundStreamInitializerOutput: The type which is returned by the initializer operating on the inbound
/// (remotely-initiated) HTTP/2 streams.
@usableFromInline
static func initialize(inboundStreamInitializerOutput: Output.Type = Output.self) -> (NIOHTTP2AsyncSequence<Output>, Continuation) {
static func initialize(
inboundStreamInitializerOutput: Output.Type = Output.self
) -> (NIOHTTP2AsyncSequence<Output>, Continuation) {
let (stream, continuation) = AsyncThrowingStream.makeStream(of: Output.self)
return (.init(stream), Continuation(wrapping: continuation))
}
@@ -73,7 +73,12 @@ internal enum NIOHTTP2ConnectionStateChange: Hashable {
internal var reason: HTTP2ErrorCode?
internal init(streamID: HTTP2StreamID, localConnectionWindowSize: Int, remoteConnectionWindowSize: Int, reason: HTTP2ErrorCode?) {
internal init(
streamID: HTTP2StreamID,
localConnectionWindowSize: Int,
remoteConnectionWindowSize: Int,
reason: HTTP2ErrorCode?
) {
self.streamID = streamID
self.localConnectionWindowSize = localConnectionWindowSize
self.remoteConnectionWindowSize = remoteConnectionWindowSize
@@ -124,7 +129,11 @@ internal enum NIOHTTP2ConnectionStateChange: Hashable {
}
}
internal init(localConnectionWindowSize: Int, remoteConnectionWindowSize: Int, localStreamWindowSize: StreamWindowSizeChange?) {
internal init(
localConnectionWindowSize: Int,
remoteConnectionWindowSize: Int,
localStreamWindowSize: StreamWindowSizeChange?
) {
self.localConnectionWindowSize = localConnectionWindowSize
self.remoteConnectionWindowSize = remoteConnectionWindowSize
self.localStreamWindowSize = localStreamWindowSize
@@ -169,7 +178,6 @@ internal enum NIOHTTP2ConnectionStateChange: Hashable {
}
}
/// A representation of a state change at the level of a single stream.
///
/// While the NIOHTTP2ConnectionStateChange is an object that affects an entire connection,
@@ -195,23 +203,34 @@ internal enum StreamStateChange: Hashable {
}
}
internal extension NIOHTTP2ConnectionStateChange {
init(_ streamChange: StreamStateChange, inboundFlowControlWindow: HTTP2FlowControlWindow, outboundFlowControlWindow: HTTP2FlowControlWindow) {
extension NIOHTTP2ConnectionStateChange {
init(
_ streamChange: StreamStateChange,
inboundFlowControlWindow: HTTP2FlowControlWindow,
outboundFlowControlWindow: HTTP2FlowControlWindow
) {
switch streamChange {
case .streamClosed(let streamClosedState):
self = .streamClosed(.init(streamID: streamClosedState.streamID,
localConnectionWindowSize: Int(outboundFlowControlWindow),
remoteConnectionWindowSize: Int(inboundFlowControlWindow),
reason: streamClosedState.reason))
self = .streamClosed(
.init(
streamID: streamClosedState.streamID,
localConnectionWindowSize: Int(outboundFlowControlWindow),
remoteConnectionWindowSize: Int(inboundFlowControlWindow),
reason: streamClosedState.reason
)
)
case .streamCreated(let streamCreated):
self = .streamCreated(streamCreated)
case .streamCreatedAndClosed(let streamCreatedAndClosed):
self = .streamCreatedAndClosed(streamCreatedAndClosed)
case .windowSizeChange(let streamSizeChange):
self = .flowControlChange(.init(localConnectionWindowSize: Int(outboundFlowControlWindow),
remoteConnectionWindowSize: Int(inboundFlowControlWindow),
localStreamWindowSize: streamSizeChange))
self = .flowControlChange(
.init(
localConnectionWindowSize: Int(outboundFlowControlWindow),
remoteConnectionWindowSize: Int(inboundFlowControlWindow),
localStreamWindowSize: streamSizeChange
)
)
}
}
}
File diff suppressed because it is too large. Load diff
+10 -10
View File
@@ -22,7 +22,7 @@ public struct HTTP2ErrorCode: Sendable {
/// The underlying network representation of the error code.
public var networkCode: Int {
get {
return Int(self._networkCode)
Int(self._networkCode)
}
set {
self._networkCode = UInt32(newValue)
@@ -96,9 +96,9 @@ public struct HTTP2ErrorCode: Sendable {
public static let http11Required = HTTP2ErrorCode(networkCode: 0x0d)
}
extension HTTP2ErrorCode: Equatable { }
extension HTTP2ErrorCode: Equatable {}
extension HTTP2ErrorCode: Hashable { }
extension HTTP2ErrorCode: Hashable {}
extension HTTP2ErrorCode: CustomDebugStringConvertible {
public var debugDescription: String {
@@ -140,28 +140,28 @@ extension HTTP2ErrorCode: CustomDebugStringConvertible {
}
}
public extension UInt32 {
extension UInt32 {
/// Create a 32-bit integer corresponding to the given ``HTTP2ErrorCode``.
init(http2ErrorCode code: HTTP2ErrorCode) {
public init(http2ErrorCode code: HTTP2ErrorCode) {
self = code._networkCode
}
}
public extension Int {
extension Int {
/// Create an integer corresponding to the given ``HTTP2ErrorCode``.
init(http2ErrorCode code: HTTP2ErrorCode) {
public init(http2ErrorCode code: HTTP2ErrorCode) {
self = code.networkCode
}
}
public extension ByteBuffer {
extension ByteBuffer {
/// Serializes a ``HTTP2ErrorCode`` into a `ByteBuffer` in the appropriate endianness
/// for use in HTTP/2.
///
/// - Parameters:
/// - code: The ``HTTP2ErrorCode`` to serialize.
/// - Returns: The number of bytes written.
mutating func write(http2ErrorCode code: HTTP2ErrorCode) -> Int {
return self.writeInteger(UInt32(http2ErrorCode: code))
public mutating func write(http2ErrorCode code: HTTP2ErrorCode) -> Int {
self.writeInteger(UInt32(http2ErrorCode: code))
}
}
+27 -15
View File
@@ -55,16 +55,20 @@ struct HTTP2FlowControlWindow {
init(initialValue: Int) {
precondition(initialValue >= 0, "Flow control windows may not begin negative")
precondition(initialValue <= HTTP2FlowControlWindow.maxSize,
"Flow control windows may not exceed \(HTTP2FlowControlWindow.maxSize) bytes")
precondition(
initialValue <= HTTP2FlowControlWindow.maxSize,
"Flow control windows may not exceed \(HTTP2FlowControlWindow.maxSize) bytes"
)
self.windowSize = Int32(initialValue)
}
init(initialValue: Int32) {
precondition(initialValue >= 0, "Flow control windows may not begin negative")
precondition(initialValue <= HTTP2FlowControlWindow.maxSize,
"Flow control windows may not exceed \(HTTP2FlowControlWindow.maxSize) bytes")
precondition(
initialValue <= HTTP2FlowControlWindow.maxSize,
"Flow control windows may not exceed \(HTTP2FlowControlWindow.maxSize) bytes"
)
self.windowSize = initialValue
}
@@ -94,7 +98,10 @@ struct HTTP2FlowControlWindow {
// subtraction to avoid integer overflow. Note that if the current window size is negative then all window
// update increments are valid.
guard (self.windowSize < 0) || (HTTP2FlowControlWindow.maxSize - self.windowSize >= amount) else {
throw NIOHTTP2Errors.invalidFlowControlWindowSize(delta: Int(amount), currentWindowSize: Int(self.windowSize))
throw NIOHTTP2Errors.invalidFlowControlWindowSize(
delta: Int(amount),
currentWindowSize: Int(self.windowSize)
)
}
self.windowSize += Int32(amount)
@@ -116,7 +123,10 @@ struct HTTP2FlowControlWindow {
assert(amount >= -(Int32.max))
guard (self.windowSize < 0) || (HTTP2FlowControlWindow.maxSize - self.windowSize >= amount) else {
throw NIOHTTP2Errors.invalidFlowControlWindowSize(delta: Int(amount), currentWindowSize: Int(self.windowSize))
throw NIOHTTP2Errors.invalidFlowControlWindowSize(
delta: Int(amount),
currentWindowSize: Int(self.windowSize)
)
}
self.windowSize += amount
@@ -146,8 +156,10 @@ extension HTTP2FlowControlWindow: ExpressibleByIntegerLiteral {
init(integerLiteral initialValue: Int32) {
precondition(initialValue >= 0, "Flow control windows may not begin negative")
precondition(initialValue <= HTTP2FlowControlWindow.maxSize,
"Flow control windows may not exceed \(HTTP2FlowControlWindow.maxSize) bytes")
precondition(
initialValue <= HTTP2FlowControlWindow.maxSize,
"Flow control windows may not exceed \(HTTP2FlowControlWindow.maxSize) bytes"
)
self.windowSize = initialValue
}
@@ -155,29 +167,29 @@ extension HTTP2FlowControlWindow: ExpressibleByIntegerLiteral {
extension HTTP2FlowControlWindow: CustomStringConvertible {
var description: String {
return self.windowSize.description
self.windowSize.description
}
}
extension HTTP2FlowControlWindow: Equatable { }
extension HTTP2FlowControlWindow: Equatable {}
extension HTTP2FlowControlWindow: Hashable { }
extension HTTP2FlowControlWindow: Hashable {}
extension HTTP2FlowControlWindow: Comparable {
static func < (lhs: HTTP2FlowControlWindow, rhs: HTTP2FlowControlWindow) -> Bool {
return lhs.windowSize < rhs.windowSize
lhs.windowSize < rhs.windowSize
}
static func > (lhs: HTTP2FlowControlWindow, rhs: HTTP2FlowControlWindow) -> Bool {
return lhs.windowSize > rhs.windowSize
lhs.windowSize > rhs.windowSize
}
static func <= (lhs: HTTP2FlowControlWindow, rhs: HTTP2FlowControlWindow) -> Bool {
return lhs.windowSize <= rhs.windowSize
lhs.windowSize <= rhs.windowSize
}
static func >= (lhs: HTTP2FlowControlWindow, rhs: HTTP2FlowControlWindow) -> Bool {
return lhs.windowSize >= rhs.windowSize
lhs.windowSize >= rhs.windowSize
}
}
+46 -33
View File
@@ -13,8 +13,8 @@
//===----------------------------------------------------------------------===//
import NIOCore
import NIOHTTP1
import NIOHPACK
import NIOHTTP1
/// A representation of a single HTTP/2 frame.
public struct HTTP2Frame: Sendable {
@@ -44,7 +44,7 @@ public struct HTTP2Frame: Sendable {
///
/// See [RFC 7540 § 6.1](https://httpwg.org/specs/rfc7540.html#rfc.section.6.1).
case data(FramePayload.Data)
/// A `HEADERS` frame, containing all headers or trailers associated with a request
/// or response.
///
@@ -53,25 +53,25 @@ public struct HTTP2Frame: Sendable {
///
/// See [RFC 7540 § 6.2](https://httpwg.org/specs/rfc7540.html#rfc.section.6.2).
case headers(Headers)
/// A `PRIORITY` frame, used to change priority and dependency ordering among
/// streams.
///
/// See [RFC 7540 § 6.3](https://httpwg.org/specs/rfc7540.html#rfc.section.6.3).
case priority(StreamPriorityData)
/// A `RST_STREAM` (reset stream) frame, sent when a stream has encountered an error
/// condition and needs to be terminated as a result.
///
/// See [RFC 7540 § 6.4](https://httpwg.org/specs/rfc7540.html#rfc.section.6.4).
case rstStream(HTTP2ErrorCode)
/// A `SETTINGS` frame, containing various connection--level settings and their
/// desired values.
///
/// See [RFC 7540 § 6.5](https://httpwg.org/specs/rfc7540.html#rfc.section.6.5).
case settings(Settings)
/// A `PUSH_PROMISE` frame, used to notify a peer in advance of streams that a sender
/// intends to initiate. It performs much like a request's `HEADERS` frame, informing
/// a peer that the response for a theoretical request like the one in the promise
@@ -86,12 +86,12 @@ public struct HTTP2Frame: Sendable {
/// For more information on server push in HTTP/2, see
/// [RFC 7540 § 8.2](https://httpwg.org/specs/rfc7540.html#rfc.section.8.2).
indirect case pushPromise(PushPromise)
/// A `PING` frame, used to measure round-trip time between endpoints.
///
/// See [RFC 7540 § 6.7](https://httpwg.org/specs/rfc7540.html#rfc.section.6.7).
case ping(HTTP2PingData, ack: Bool)
/// A `GOAWAY` frame, used to request that a peer immediately cease communication with
/// the sender. It contains a stream ID indicating the last stream that will be processed
/// by the sender, an error code (if the shutdown was caused by an error), and optionally
@@ -99,14 +99,14 @@ public struct HTTP2Frame: Sendable {
///
/// See [RFC 7540 § 6.8](https://httpwg.org/specs/rfc7540.html#rfc.section.6.8).
indirect case goAway(lastStreamID: HTTP2StreamID, errorCode: HTTP2ErrorCode, opaqueData: ByteBuffer?)
/// A `WINDOW_UPDATE` frame. This is used to implement flow control of DATA frames,
/// allowing peers to advertise and update the amount of data they are prepared to
/// process at any given moment.
///
/// See [RFC 7540 § 6.9](https://httpwg.org/specs/rfc7540.html#rfc.section.6.9).
case windowUpdate(windowSizeIncrement: Int)
/// An `ALTSVC` frame. This is sent by an HTTP server to indicate alternative origin
/// locations for accessing the same resource, for instance via another protocol,
/// or over TLS. It consists of an origin and a list of alternate protocols and
@@ -117,7 +117,7 @@ public struct HTTP2Frame: Sendable {
/// - Important: ALTSVC frames are not currently supported. Any received ALTSVC frames will
/// be ignored. Attempting to send an ALTSVC frame will result in a fatal error.
indirect case alternativeService(origin: String?, field: ByteBuffer?)
/// An `ORIGIN` frame. This allows servers which allow access to multiple origins
/// via the same socket connection to identify which origins may be accessed in
/// this manner.
@@ -134,7 +134,7 @@ public struct HTTP2Frame: Sendable {
@inlinable
public var data: IOData {
get {
return self._backing.data
self._backing.data
}
set {
self._copyIfNeeded()
@@ -146,7 +146,7 @@ public struct HTTP2Frame: Sendable {
@inlinable
public var endStream: Bool {
get {
return self._backing.endStream
self._backing.endStream
}
set {
self._copyIfNeeded()
@@ -158,12 +158,15 @@ public struct HTTP2Frame: Sendable {
@inlinable
public var paddingBytes: Int? {
get {
return self._backing.paddingBytes.map { Int($0) }
self._backing.paddingBytes.map { Int($0) }
}
set {
self._copyIfNeeded()
if let newValue = newValue {
precondition(newValue >= 0 && newValue <= Int(UInt8.max), "Invalid padding byte length: \(newValue)")
precondition(
newValue >= 0 && newValue <= Int(UInt8.max),
"Invalid padding byte length: \(newValue)"
)
self._backing.paddingBytes = UInt8(newValue)
} else {
self._backing.paddingBytes = nil
@@ -211,7 +214,7 @@ public struct HTTP2Frame: Sendable {
@inlinable
func copy() -> _Backing {
return _Backing(data: self.data, endStream: self.endStream, paddingBytes: self.paddingBytes)
_Backing(data: self.data, endStream: self.endStream, paddingBytes: self.paddingBytes)
}
}
}
@@ -303,7 +306,10 @@ public struct HTTP2Frame: Sendable {
}
set {
if let newValue = newValue {
precondition(newValue >= 0 && newValue <= Int(UInt8.max), "Invalid padding byte length: \(newValue)")
precondition(
newValue >= 0 && newValue <= Int(UInt8.max),
"Invalid padding byte length: \(newValue)"
)
self._paddingBytes = UInt8(newValue)
self.booleans.insert(.paddingPresent)
} else {
@@ -312,7 +318,12 @@ public struct HTTP2Frame: Sendable {
}
}
public init(headers: HPACKHeaders, priorityData: StreamPriorityData? = nil, endStream: Bool = false, paddingBytes: Int? = nil) {
public init(
headers: HPACKHeaders,
priorityData: StreamPriorityData? = nil,
endStream: Bool = false,
paddingBytes: Int? = nil
) {
self.headers = headers
self.booleans = .init(rawValue: 0)
self._paddingBytes = 0
@@ -347,11 +358,14 @@ public struct HTTP2Frame: Sendable {
/// The number of padding bytes sent in this frame. If nil, this frame was not padded.
public var paddingBytes: Int? {
get {
return self._paddingBytes.map { Int($0) }
self._paddingBytes.map { Int($0) }
}
set {
if let newValue = newValue {
precondition(newValue >= 0 && newValue <= Int(UInt8.max), "Invalid padding byte length: \(newValue)")
precondition(
newValue >= 0 && newValue <= Int(UInt8.max),
"Invalid padding byte length: \(newValue)"
)
self._paddingBytes = UInt8(newValue)
} else {
self._paddingBytes = nil
@@ -365,21 +379,21 @@ public struct HTTP2Frame: Sendable {
self.paddingBytes = paddingBytes
}
}
/// The one-byte identifier used to indicate the type of a frame on the wire.
var code: UInt8 {
switch self {
case .data: return 0x0
case .headers: return 0x1
case .priority: return 0x2
case .rstStream: return 0x3
case .settings: return 0x4
case .pushPromise: return 0x5
case .ping: return 0x6
case .goAway: return 0x7
case .windowUpdate: return 0x8
case .alternativeService: return 0xa
case .origin: return 0xc
case .data: return 0x0
case .headers: return 0x1
case .priority: return 0x2
case .rstStream: return 0x3
case .settings: return 0x4
case .pushPromise: return 0x5
case .ping: return 0x6
case .goAway: return 0x7
case .windowUpdate: return 0x8
case .alternativeService: return 0xa
case .origin: return 0xc
}
}
}
@@ -417,4 +431,3 @@ extension HTTP2Frame.FramePayload {
/// Marking them non-Sendable would sadly be API breaking.
extension HTTP2Frame.FramePayload: @unchecked Sendable {}
extension HTTP2Frame.FramePayload.Data: @unchecked Sendable {}
+12 -13
View File
@@ -19,7 +19,7 @@ struct HTTP2FrameEncoder {
var headerEncoder: HPACKEncoder
// RFC 7540 § 6.5.2 puts the initial value of SETTINGS_MAX_FRAME_SIZE at 2**14 octets
var maxFrameSize: UInt32 = 1<<14
var maxFrameSize: UInt32 = 1 << 14
init(allocator: ByteBufferAllocator) {
self.headerEncoder = HPACKEncoder(allocator: allocator)
@@ -46,15 +46,15 @@ struct HTTP2FrameEncoder {
// note our starting point
let start = buf.writerIndex
// +-----------------------------------------------+
// | Length (24) |
// +---------------+---------------+---------------+
// | Type (8) | Flags (8) |
// +-+-------------+---------------+-------------------------------+
// |R| Stream Identifier (31) |
// +=+=============================================================+
// | Frame Payload (0...) ...
// +---------------------------------------------------------------+
// +-----------------------------------------------+
// | Length (24) |
// +---------------+---------------+---------------+
// | Type (8) | Flags (8) |
// +-+-------------+---------------+-------------------------------+
// |R| Stream Identifier (31) |
// +=+=============================================================+
// | Frame Payload (0...) ...
// +---------------------------------------------------------------+
// skip 24-bit length for now, we'll fill that in later
buf.moveWriterIndex(forwardBy: 3)
@@ -240,11 +240,10 @@ extension ByteBuffer {
// Yes, this performs better than running a UInt8 through the generic write(integer:) three times.
var bytes: (UInt8, UInt8, UInt8)
bytes.0 = UInt8((size & 0xff_00_00) >> 16)
bytes.1 = UInt8((size & 0x00_ff_00) >> 8)
bytes.2 = UInt8( size & 0x00_00_ff)
bytes.1 = UInt8((size & 0x00_ff_00) >> 8)
bytes.2 = UInt8(size & 0x00_00_ff)
withUnsafeBytes(of: bytes) { ptr in
_ = self.setBytes(ptr, at: location)
}
}
}
+170 -76
View File
@@ -39,7 +39,8 @@ struct HTTP2FrameDecoder {
mutating func process() throws -> AccumulatingFrameHeaderParserState? {
// The client magic is 24 octets long: If we don't have it, keep waiting.
guard var pendingBytes = self.pendingBytes,
let clientMagic = pendingBytes.readSlice(length: 24) else {
let clientMagic = pendingBytes.readSlice(length: 24)
else {
return nil
}
@@ -232,7 +233,12 @@ struct HTTP2FrameDecoder {
enum ProcessResult {
case accumulateHeaderBlockFragments(AccumulatingHeaderBlockFragmentsParserState)
case parseFrame(header: FrameHeader, payloadBytes: ByteBuffer, paddingBytes: Int?, nextState: AccumulatingFrameHeaderParserState)
case parseFrame(
header: FrameHeader,
payloadBytes: ByteBuffer,
paddingBytes: Int?,
nextState: AccumulatingFrameHeaderParserState
)
}
/// Process a frame.
@@ -289,7 +295,10 @@ struct HTTP2FrameDecoder {
let nextState = AccumulatingFrameHeaderParserState(unusedBytes: self.accumulatedBytes)
return .parseFrame(
header: syntheticHeader, payloadBytes: payloadBytes, paddingBytes: expectedPadding, nextState: nextState
header: syntheticHeader,
payloadBytes: payloadBytes,
paddingBytes: expectedPadding,
nextState: nextState
)
}
@@ -426,14 +435,14 @@ struct HTTP2FrameDecoder {
// branch.
precondition(self.payload.readableBytes > 0)
let frameBytes = self.payload // entire thing
let frameBytes = self.payload // entire thing
self.remainingByteCount -= frameBytes.readableBytes
self.payload.quietlyReset()
return HTTP2Frame.FramePayload.Data(
data: .byteBuffer(frameBytes),
endStream: false, // We're synthesising frames, there can't be END_STREAM here.
paddingBytes: nil // We put all the padding on the last frame in the sequence.
endStream: false, // We're synthesising frames, there can't be END_STREAM here.
paddingBytes: nil // We put all the padding on the last frame in the sequence.
)
}
@@ -452,7 +461,11 @@ struct HTTP2FrameDecoder {
private var excessBytes: ByteBuffer
private var expectedPadding: Int
init(fromSimulatingDataFramesState state: SimulatingDataFramesParserState, excessBytes: ByteBuffer, expectedPadding: Int) {
init(
fromSimulatingDataFramesState state: SimulatingDataFramesParserState,
excessBytes: ByteBuffer,
expectedPadding: Int
) {
precondition(expectedPadding > 0)
self.header = state.header
@@ -460,7 +473,11 @@ struct HTTP2FrameDecoder {
self.expectedPadding = expectedPadding
}
init(fromAccumulatingPayloadState state: AccumulatingPayloadParserState, excessBytes: ByteBuffer, expectedPadding: Int) {
init(
fromAccumulatingPayloadState state: AccumulatingPayloadParserState,
excessBytes: ByteBuffer,
expectedPadding: Int
) {
precondition(expectedPadding > 0)
self.header = state.header
@@ -507,8 +524,10 @@ struct HTTP2FrameDecoder {
private var originalPaddingBytes: Int?
private var continuationSequenceCount: Int
init(fromAccumulatingHeaderBlockFragments acc: AccumulatingHeaderBlockFragmentsParserState,
continuationHeader: FrameHeader) {
init(
fromAccumulatingHeaderBlockFragments acc: AccumulatingHeaderBlockFragmentsParserState,
continuationHeader: FrameHeader
) {
precondition(acc.header.beginsContinuationSequence)
precondition(continuationHeader.type == .continuation)
@@ -524,11 +543,17 @@ struct HTTP2FrameDecoder {
/// or we continue accumulating
enum ProcessResult {
case accumulateContinuationHeader(AccumulatingHeaderBlockFragmentsParserState)
case parseFrame(header: FrameHeader, payloadBytes: ByteBuffer, paddingBytes: Int?, nextState: AccumulatingFrameHeaderParserState)
case parseFrame(
header: FrameHeader,
payloadBytes: ByteBuffer,
paddingBytes: Int?,
nextState: AccumulatingFrameHeaderParserState
)
}
mutating func process() throws -> ProcessResult? {
guard let continuationPayload = self.continuationPayload.readSlice(length: self.continuationHeader.length) else {
guard let continuationPayload = self.continuationPayload.readSlice(length: self.continuationHeader.length)
else {
return nil
}
@@ -568,7 +593,12 @@ struct HTTP2FrameDecoder {
// It is! Let's add .endHeaders to our fake frame.
header.flags.formUnion(.endHeaders)
let nextState = AccumulatingFrameHeaderParserState(unusedBytes: self.continuationPayload)
return .parseFrame(header: header, payloadBytes: payload, paddingBytes: self.originalPaddingBytes, nextState: nextState)
return .parseFrame(
header: header,
payloadBytes: payload,
paddingBytes: self.originalPaddingBytes,
nextState: nextState
)
}
mutating func accumulate(bytes: ByteBuffer) {
@@ -634,7 +664,10 @@ struct HTTP2FrameDecoder {
throw NIOHTTP2Errors.excessiveContinuationFrames()
}
return AccumulatingContinuationPayloadParserState(fromAccumulatingHeaderBlockFragments: self, continuationHeader: header)
return AccumulatingContinuationPayloadParserState(
fromAccumulatingHeaderBlockFragments: self,
continuationHeader: header
)
}
mutating func accumulate(bytes: ByteBuffer) {
@@ -721,7 +754,7 @@ struct HTTP2FrameDecoder {
private let maximumSequentialContinuationFrames: Int
// RFC 7540 § 6.5.2 puts the initial value of SETTINGS_MAX_FRAME_SIZE at 2**14 octets
internal var maxFrameSize: UInt32 = 1<<14
internal var maxFrameSize: UInt32 = 1 << 14
/// Creates a new HTTP2 frame decoder.
///
@@ -809,7 +842,7 @@ struct HTTP2FrameDecoder {
mutating func nextFrame() throws -> (HTTP2Frame, flowControlledLength: Int)? {
// Start running through our state machine until we run out of bytes or we emit a frame.
while true {
switch (try self.processNextState()) {
switch try self.processNextState() {
case .needMoreData:
return nil
case .frame(let frame, let flowControlledLength):
@@ -826,7 +859,7 @@ struct HTTP2FrameDecoder {
case needMoreData
case parseFrame(header: FrameHeader, payloadBytes: ByteBuffer, paddingBytes: Int?)
}
private mutating func processNextState() throws -> ParseResult {
switch self.state {
case .awaitingClientMagic(var state):
@@ -847,7 +880,7 @@ struct HTTP2FrameDecoder {
throw error
}
}
case .initialized:
// no bytes, no frame
return .needMoreData
@@ -857,15 +890,20 @@ struct HTTP2FrameDecoder {
let maxHeaderListSize = self.headerDecoder.maxHeaderListSize
return try self.avoidingParserCoW { newState in
let result = Result<ParseResult, Error> {
guard let targetState = try state.process(maxFrameSize: maxFrameSize, maxHeaderListSize: maxHeaderListSize) else {
guard
let targetState = try state.process(
maxFrameSize: maxFrameSize,
maxHeaderListSize: maxHeaderListSize
)
else {
newState = .accumulatingFrameHeader(state)
return .needMoreData
}
newState = .init(targetState)
return .continue
}
switch result {
case let .success(processResult):
return processResult
@@ -874,7 +912,7 @@ struct HTTP2FrameDecoder {
throw error
}
}
case .awaitingPaddingLengthByte(var state):
return try self.avoidingParserCoW { newState in
let result = Result<ParseResult, Error> {
@@ -882,7 +920,7 @@ struct HTTP2FrameDecoder {
newState = .awaitingPaddingLengthByte(state)
return .needMoreData
}
newState = .init(targetState)
return .continue
}
@@ -894,7 +932,7 @@ struct HTTP2FrameDecoder {
throw error
}
}
case .accumulatingPayload(var state):
let accumulatingResult: ProcessAccumulatingResult = try self.avoidingParserCoW { newState in
let result = Result<ProcessAccumulatingResult, Error> {
@@ -902,18 +940,18 @@ struct HTTP2FrameDecoder {
newState = .accumulatingPayload(state)
return .needMoreData
}
switch processResult {
case .accumulateHeaderBlockFragments(let nextState):
newState = .accumulatingHeaderBlockFragments(nextState)
return .continue
case .parseFrame(header: let header, payloadBytes: let payloadBytes, paddingBytes: let paddingBytes, nextState: let nextState):
case .parseFrame(let header, let payloadBytes, let paddingBytes, let nextState):
// Save off the state first, because if the frame payload parse throws we want to be able to skip over the frame.
newState = .accumulatingFrameHeader(nextState)
return .parseFrame(header: header, payloadBytes: payloadBytes, paddingBytes: paddingBytes)
}
}
switch result {
case let .success(processAccumulatingResult):
return processAccumulatingResult
@@ -922,9 +960,9 @@ struct HTTP2FrameDecoder {
throw error
}
}
switch accumulatingResult {
case .parseFrame(header: let header, payloadBytes: var payloadBytes, paddingBytes: let paddingBytes):
case .parseFrame(let header, var payloadBytes, let paddingBytes):
// an entire frame's data, including HEADERS/PUSH_PROMISE with the END_HEADERS flag set
// this may legitimately return nil if we ignore the frame
let result = try self.readFrame(withHeader: header, from: &payloadBytes, paddingBytes: paddingBytes)
@@ -971,7 +1009,7 @@ struct HTTP2FrameDecoder {
newState = .strippingTrailingPadding(state)
return .needMoreData
}
newState = .init(nextState)
return .continue
}
@@ -987,7 +1025,7 @@ struct HTTP2FrameDecoder {
case .accumulateContinuationHeader(let nextState):
newState = .accumulatingHeaderBlockFragments(nextState)
return .continue
case .parseFrame(header: let header, payloadBytes: let payloadBytes, paddingBytes: let paddingBytes, nextState: let nextState):
case .parseFrame(let header, let payloadBytes, let paddingBytes, let nextState):
// Save off the state first, because if the frame payload parse throws we want to be able to skip over the frame.
newState = .accumulatingFrameHeader(nextState)
return .parseFrame(header: header, payloadBytes: payloadBytes, paddingBytes: paddingBytes)
@@ -1001,9 +1039,9 @@ struct HTTP2FrameDecoder {
throw error
}
}
switch accumulatingResult {
case .parseFrame(header: let header, payloadBytes: var payloadBytes, paddingBytes: let paddingBytes):
case .parseFrame(let header, var payloadBytes, let paddingBytes):
// an entire frame's data, including HEADERS/PUSH_PROMISE with the END_HEADERS flag set
// this may legitimately return nil if we ignore the frame
let result = try self.readFrame(withHeader: header, from: &payloadBytes, paddingBytes: paddingBytes)
@@ -1032,10 +1070,12 @@ struct HTTP2FrameDecoder {
return try self.avoidingParserCoW { newState in
let result = Result<ParseResult, Error> {
guard let processResult = try state.process(
maxHeaderListSize: maxHeaderListSize,
maximumSequentialContinuationFrames: maximumSequentialContinuationFrames
) else {
guard
let processResult = try state.process(
maxHeaderListSize: maxHeaderListSize,
maximumSequentialContinuationFrames: maximumSequentialContinuationFrames
)
else {
newState = .accumulatingHeaderBlockFragments(state)
return .needMoreData
}
@@ -1050,13 +1090,17 @@ struct HTTP2FrameDecoder {
throw error
}
}
case .appending:
preconditionFailure("Attempting to process in appending state")
}
}
private mutating func readFrame(withHeader header: FrameHeader, from bytes: inout ByteBuffer, paddingBytes: Int?) throws -> HTTP2Frame? {
private mutating func readFrame(
withHeader header: FrameHeader,
from bytes: inout ByteBuffer,
paddingBytes: Int?
) throws -> HTTP2Frame? {
assert(bytes.readableBytes == header.length, "Buffer should contain exactly \(header.length) bytes.")
let flags = header.flags
@@ -1119,7 +1163,12 @@ struct HTTP2FrameDecoder {
return HTTP2Frame(streamID: streamID, payload: payload)
}
private mutating func parseHeadersFramePayload(streamID: HTTP2StreamID, flags: FrameFlags, bytes: inout ByteBuffer, paddingBytes: Int?) throws -> HTTP2Frame.FramePayload {
private mutating func parseHeadersFramePayload(
streamID: HTTP2StreamID,
flags: FrameFlags,
bytes: inout ByteBuffer,
paddingBytes: Int?
) throws -> HTTP2Frame.FramePayload {
// HEADERS frame : RFC 7540 § 6.2
guard streamID != .rootStream else {
// HEADERS frames MUST be associated with a stream. If a HEADERS frame is received whose
@@ -1135,23 +1184,30 @@ struct HTTP2FrameDecoder {
throw InternalError.codecError(code: .protocolError)
}
priorityData = HTTP2Frame.StreamPriorityData(exclusive: (rawStreamID & 0x8000_0000 != 0),
dependency: HTTP2StreamID(networkID: rawStreamID),
weight: weight)
priorityData = HTTP2Frame.StreamPriorityData(
exclusive: (rawStreamID & 0x8000_0000 != 0),
dependency: HTTP2StreamID(networkID: rawStreamID),
weight: weight
)
} else {
priorityData = nil
}
let headers = try self.headerDecoder.decodeHeaders(from: &bytes)
let headersPayload = HTTP2Frame.FramePayload.Headers(headers: headers,
priorityData: priorityData,
endStream: flags.contains(.endStream),
paddingBytes: paddingBytes)
let headersPayload = HTTP2Frame.FramePayload.Headers(
headers: headers,
priorityData: priorityData,
endStream: flags.contains(.endStream),
paddingBytes: paddingBytes
)
return .headers(headersPayload)
}
private func parsePriorityFramePayload(streamID: HTTP2StreamID, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parsePriorityFramePayload(
streamID: HTTP2StreamID,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// PRIORITY frame : RFC 7540 § 6.3
guard streamID != .rootStream else {
// The PRIORITY frame always identifies a stream. If a PRIORITY frame is received
@@ -1165,13 +1221,18 @@ struct HTTP2FrameDecoder {
throw InternalError.codecError(code: .frameSizeError)
}
let priorityData = HTTP2Frame.StreamPriorityData(exclusive: rawStreamID & 0x8000_0000 != 0,
dependency: HTTP2StreamID(networkID: rawStreamID),
weight: weight)
let priorityData = HTTP2Frame.StreamPriorityData(
exclusive: rawStreamID & 0x8000_0000 != 0,
dependency: HTTP2StreamID(networkID: rawStreamID),
weight: weight
)
return .priority(priorityData)
}
private func parseRstStreamFramePayload(streamID: HTTP2StreamID, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parseRstStreamFramePayload(
streamID: HTTP2StreamID,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// RST_STREAM frame : RFC 7540 § 6.4
guard streamID != .rootStream else {
// RST_STREAM frames MUST be associated with a stream. If a RST_STREAM frame is
@@ -1188,7 +1249,11 @@ struct HTTP2FrameDecoder {
return .rstStream(HTTP2ErrorCode(errorCode))
}
private func parseSettingsFramePayload(streamID: HTTP2StreamID, flags: FrameFlags, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parseSettingsFramePayload(
streamID: HTTP2StreamID,
flags: FrameFlags,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// SETTINGS frame : RFC 7540 § 6.5
guard streamID == .rootStream else {
// SETTINGS frames always apply to a connection, never a single stream. The stream
@@ -1232,7 +1297,12 @@ struct HTTP2FrameDecoder {
return .settings(.settings(settings))
}
private mutating func parsePushPromiseFramePayload(streamID: HTTP2StreamID, flags: FrameFlags, bytes: inout ByteBuffer, paddingBytes: Int?) throws -> HTTP2Frame.FramePayload {
private mutating func parsePushPromiseFramePayload(
streamID: HTTP2StreamID,
flags: FrameFlags,
bytes: inout ByteBuffer,
paddingBytes: Int?
) throws -> HTTP2Frame.FramePayload {
// PUSH_PROMISE frame : RFC 7540 § 6.6
guard streamID != .rootStream else {
// The stream identifier of a PUSH_PROMISE frame indicates the stream it is associated with.
@@ -1253,13 +1323,22 @@ struct HTTP2FrameDecoder {
let headers = try self.headerDecoder.decodeHeaders(from: &bytes)
let pushPromiseContent = HTTP2Frame.FramePayload.PushPromise(pushedStreamID: promisedStreamID, headers: headers, paddingBytes: paddingBytes)
let pushPromiseContent = HTTP2Frame.FramePayload.PushPromise(
pushedStreamID: promisedStreamID,
headers: headers,
paddingBytes: paddingBytes
)
return .pushPromise(pushPromiseContent)
}
private func parsePingFramePayload(streamID: HTTP2StreamID, flags: FrameFlags, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parsePingFramePayload(
streamID: HTTP2StreamID,
flags: FrameFlags,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// PING frame : RFC 7540 § 6.7
guard let tuple = bytes.readMultipleIntegers(as: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8).self) else {
guard let tuple = bytes.readMultipleIntegers(as: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8).self)
else {
// Receipt of a PING frame with a length field value other than 8 MUST be treated
// as a connection error (Section 5.4.1) of type FRAME_SIZE_ERROR.
throw InternalError.codecError(code: .frameSizeError)
@@ -1274,7 +1353,10 @@ struct HTTP2FrameDecoder {
return .ping(HTTP2PingData(withTuple: tuple), ack: flags.contains(.ack))
}
private func parseGoAwayFramePayload(streamID: HTTP2StreamID, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parseGoAwayFramePayload(
streamID: HTTP2StreamID,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// GOAWAY frame : RFC 7540 § 6.8
guard streamID == .rootStream else {
// The GOAWAY frame applies to the connection, not a specific stream. An endpoint
@@ -1292,9 +1374,11 @@ struct HTTP2FrameDecoder {
let debugData = bytes.readableBytes > 0 ? bytes : nil
bytes.moveReaderIndex(to: bytes.writerIndex)
return .goAway(lastStreamID: HTTP2StreamID(networkID: rawLastStreamID),
errorCode: HTTP2ErrorCode(errorCode),
opaqueData: debugData)
return .goAway(
lastStreamID: HTTP2StreamID(networkID: rawLastStreamID),
errorCode: HTTP2ErrorCode(errorCode),
opaqueData: debugData
)
}
private func parseWindowUpdateFramePayload(bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
@@ -1308,7 +1392,10 @@ struct HTTP2FrameDecoder {
return .windowUpdate(windowSizeIncrement: Int(rawWindowSizeIncrement & ~0x8000_0000))
}
private func parseAltSvcFramePayload(streamID: HTTP2StreamID, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parseAltSvcFramePayload(
streamID: HTTP2StreamID,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// ALTSVC frame : RFC 7838 § 4
guard let originLen = bytes.readInteger(as: UInt16.self) else {
// Must be at least two bytes, to contain the length of the optional 'Origin' field.
@@ -1351,7 +1438,10 @@ struct HTTP2FrameDecoder {
return .alternativeService(origin: origin, field: value)
}
private func parseOriginFramePayload(streamID: HTTP2StreamID, bytes: inout ByteBuffer) throws -> HTTP2Frame.FramePayload {
private func parseOriginFramePayload(
streamID: HTTP2StreamID,
bytes: inout ByteBuffer
) throws -> HTTP2Frame.FramePayload {
// ORIGIN frame : RFC 8336 § 2
var origins: [String] = []
while let originLen = bytes.readInteger(as: UInt16.self) {
@@ -1377,8 +1467,8 @@ struct HTTP2FrameDecoder {
}
}
fileprivate struct FrameHeader {
var length: Int // actually 24-bits
private struct FrameHeader {
var length: Int // actually 24-bits
var type: FrameType
var flags: FrameFlags
var streamID: HTTP2StreamID
@@ -1404,18 +1494,22 @@ fileprivate struct FrameHeader {
static let origin = FrameType(12)
var supportsPadding: Bool {
return self == .data || self == .headers || self == .pushPromise
self == .data || self == .headers || self == .pushPromise
}
}
var beginsContinuationSequence: Bool {
return (self.type == .headers || self.type == .pushPromise) && !self.flags.contains(.endHeaders)
(self.type == .headers || self.type == .pushPromise) && !self.flags.contains(.endHeaders)
}
}
extension ByteBuffer {
fileprivate mutating func readFrameHeader() -> FrameHeader? {
guard let (lenHigh, lenLow, type, flags, rawStreamID) = self.readMultipleIntegers(as: (UInt16, UInt8, UInt8, UInt8, UInt32).self) else {
guard
let (lenHigh, lenLow, type, flags, rawStreamID) = self.readMultipleIntegers(
as: (UInt16, UInt8, UInt8, UInt8, UInt32).self
)
else {
return nil
}
@@ -1433,7 +1527,6 @@ extension ByteBuffer {
}
}
/// The flags supported by the frame types understood by this protocol.
struct FrameFlags: OptionSet, CustomStringConvertible {
internal typealias RawValue = UInt8
@@ -1445,22 +1538,22 @@ struct FrameFlags: OptionSet, CustomStringConvertible {
}
/// END_STREAM flag. Valid on DATA and HEADERS frames.
internal static let endStream = FrameFlags(rawValue: 0x01)
internal static let endStream = FrameFlags(rawValue: 0x01)
/// ACK flag. Valid on SETTINGS and PING frames.
internal static let ack = FrameFlags(rawValue: 0x01)
internal static let ack = FrameFlags(rawValue: 0x01)
/// END_HEADERS flag. Valid on HEADERS, CONTINUATION, and PUSH_PROMISE frames.
internal static let endHeaders = FrameFlags(rawValue: 0x04)
internal static let endHeaders = FrameFlags(rawValue: 0x04)
/// PADDED flag. Valid on DATA, HEADERS, CONTINUATION, and PUSH_PROMISE frames.
///
/// NB: swift-nio-http2 does not automatically pad outgoing frames.
internal static let padded = FrameFlags(rawValue: 0x08)
internal static let padded = FrameFlags(rawValue: 0x08)
/// PRIORITY flag. Valid on HEADERS frames, specifically as the first frame sent
/// on a new stream.
internal static let priority = FrameFlags(rawValue: 0x20)
internal static let priority = FrameFlags(rawValue: 0x20)
// useful for test cases
internal static var allFlags: FrameFlags = [.endStream, .endHeaders, .padded, .priority]
@@ -1477,7 +1570,6 @@ struct FrameFlags: OptionSet, CustomStringConvertible {
}
}
// MARK: CoW helpers
extension HTTP2FrameDecoder {
/// So, uh...this function needs some explaining.
@@ -1495,7 +1587,9 @@ extension HTTP2FrameDecoder {
/// Sadly, because it's generic and has a closure, we need to force it to be inlined at all call sites, which is
/// not ideal.
@inline(__always)
private mutating func avoidingParserCoW<ReturnType>(_ body: (inout ParserState) throws -> ReturnType) rethrows -> ReturnType {
private mutating func avoidingParserCoW<ReturnType>(
_ body: (inout ParserState) throws -> ReturnType
) rethrows -> ReturnType {
self.state = .appending
defer {
assert(!self.isAppending)
+7 -12
View File
@@ -69,14 +69,14 @@ public struct HTTP2PingData: Sendable {
extension HTTP2PingData: RandomAccessCollection, MutableCollection {
public typealias Index = Int
public typealias Element = UInt8
public typealias Element = UInt8
public var startIndex: Index {
return 0
0
}
public var endIndex: Index {
return 7
7
}
public subscript(_ index: Index) -> Element {
@@ -128,15 +128,10 @@ extension HTTP2PingData: RandomAccessCollection, MutableCollection {
}
extension HTTP2PingData: Equatable {
public static func ==(lhs: HTTP2PingData, rhs: HTTP2PingData) -> Bool {
return lhs.bytes.0 == rhs.bytes.0 &&
lhs.bytes.1 == rhs.bytes.1 &&
lhs.bytes.2 == rhs.bytes.2 &&
lhs.bytes.3 == rhs.bytes.3 &&
lhs.bytes.4 == rhs.bytes.4 &&
lhs.bytes.5 == rhs.bytes.5 &&
lhs.bytes.6 == rhs.bytes.6 &&
lhs.bytes.7 == rhs.bytes.7
public static func == (lhs: HTTP2PingData, rhs: HTTP2PingData) -> Bool {
lhs.bytes.0 == rhs.bytes.0 && lhs.bytes.1 == rhs.bytes.1 && lhs.bytes.2 == rhs.bytes.2
&& lhs.bytes.3 == rhs.bytes.3 && lhs.bytes.4 == rhs.bytes.4 && lhs.bytes.5 == rhs.bytes.5
&& lhs.bytes.6 == rhs.bytes.6 && lhs.bytes.7 == rhs.bytes.7
}
}
+159 -75
View File
@@ -66,9 +66,15 @@ extension ChannelPipeline {
/// negotiated, or if no protocol was negotiated. Must return a future that completes when the
/// pipeline has been fully mutated.
/// - Returns: An `EventLoopFuture<Void>` that completes when the pipeline is ready to negotiate.
@available(*, deprecated, renamed: "Channel.configureHTTP2SecureUpgrade(h2ChannelConfigurator:http1ChannelConfigurator:)")
public func configureHTTP2SecureUpgrade(h2PipelineConfigurator: @escaping (ChannelPipeline) -> EventLoopFuture<Void>,
http1PipelineConfigurator: @escaping (ChannelPipeline) -> EventLoopFuture<Void>) -> EventLoopFuture<Void> {
@available(
*,
deprecated,
renamed: "Channel.configureHTTP2SecureUpgrade(h2ChannelConfigurator:http1ChannelConfigurator:)"
)
public func configureHTTP2SecureUpgrade(
h2PipelineConfigurator: @escaping (ChannelPipeline) -> EventLoopFuture<Void>,
http1PipelineConfigurator: @escaping (ChannelPipeline) -> EventLoopFuture<Void>
) -> EventLoopFuture<Void> {
let alpnHandler = ApplicationProtocolNegotiationHandler { result in
switch result {
case .negotiated("h2"):
@@ -103,12 +109,24 @@ extension Channel {
/// - inboundStreamStateInitializer: A closure that will be called whenever the remote peer initiates a new stream. This should almost always
/// be provided, especially on servers.
/// - Returns: An `EventLoopFuture` containing the `HTTP2StreamMultiplexer` inserted into this pipeline, which can be used to initiate new streams.
@available(*, deprecated, renamed: "configureHTTP2Pipeline(mode:initialLocalSettings:position:targetWindowSize:inboundStreamInitializer:)")
public func configureHTTP2Pipeline(mode: NIOHTTP2Handler.ParserMode,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil) -> EventLoopFuture<HTTP2StreamMultiplexer> {
return self.configureHTTP2Pipeline(mode: mode, initialLocalSettings: initialLocalSettings, position: position, targetWindowSize: 65535, inboundStreamStateInitializer: inboundStreamStateInitializer)
@available(
*,
deprecated,
renamed: "configureHTTP2Pipeline(mode:initialLocalSettings:position:targetWindowSize:inboundStreamInitializer:)"
)
public func configureHTTP2Pipeline(
mode: NIOHTTP2Handler.ParserMode,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil
) -> EventLoopFuture<HTTP2StreamMultiplexer> {
self.configureHTTP2Pipeline(
mode: mode,
initialLocalSettings: initialLocalSettings,
position: position,
targetWindowSize: 65535,
inboundStreamStateInitializer: inboundStreamStateInitializer
)
}
/// Configures a `ChannelPipeline` to speak HTTP/2.
@@ -126,18 +144,33 @@ extension Channel {
/// - inboundStreamStateInitializer: A closure that will be called whenever the remote peer initiates a new stream. This should almost always
/// be provided, especially on servers.
/// - Returns: An `EventLoopFuture` containing the `HTTP2StreamMultiplexer` inserted into this pipeline, which can be used to initiate new streams.
@available(*, deprecated, renamed: "configureHTTP2Pipeline(mode:initialLocalSettings:position:targetWindowSize:inboundStreamInitializer:)")
public func configureHTTP2Pipeline(mode: NIOHTTP2Handler.ParserMode,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
targetWindowSize: Int,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil) -> EventLoopFuture<HTTP2StreamMultiplexer> {
@available(
*,
deprecated,
renamed: "configureHTTP2Pipeline(mode:initialLocalSettings:position:targetWindowSize:inboundStreamInitializer:)"
)
public func configureHTTP2Pipeline(
mode: NIOHTTP2Handler.ParserMode,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
targetWindowSize: Int,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil
) -> EventLoopFuture<HTTP2StreamMultiplexer> {
var handlers = [ChannelHandler]()
handlers.reserveCapacity(2) // Update this if we need to add more handlers, to avoid unnecessary reallocation.
handlers.append(NIOHTTP2Handler(mode: mode, initialSettings: initialLocalSettings))
handlers.append(HTTP2StreamMultiplexer(mode: mode, channel: self, targetWindowSize: targetWindowSize, inboundStreamStateInitializer: inboundStreamStateInitializer))
handlers.append(
HTTP2StreamMultiplexer(
mode: mode,
channel: self,
targetWindowSize: targetWindowSize,
inboundStreamStateInitializer: inboundStreamStateInitializer
)
)
return self.pipeline.addHandlers(handlers, position: position).flatMap { self.pipeline.handler(type: HTTP2StreamMultiplexer.self) }
return self.pipeline.addHandlers(handlers, position: position).flatMap {
self.pipeline.handler(type: HTTP2StreamMultiplexer.self)
}
}
/// Configures a `ChannelPipeline` to speak HTTP/2.
@@ -155,11 +188,13 @@ extension Channel {
/// - inboundStreamInitializer: A closure that will be called whenever the remote peer initiates a new stream. This should almost always
/// be provided, especially on servers.
/// - Returns: An `EventLoopFuture` containing the `HTTP2StreamMultiplexer` inserted into this pipeline, which can be used to initiate new streams.
public func configureHTTP2Pipeline(mode: NIOHTTP2Handler.ParserMode,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
targetWindowSize: Int = 65535,
inboundStreamInitializer: NIOChannelInitializer?) -> EventLoopFuture<HTTP2StreamMultiplexer> {
public func configureHTTP2Pipeline(
mode: NIOHTTP2Handler.ParserMode,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
targetWindowSize: Int = 65535,
inboundStreamInitializer: NIOChannelInitializer?
) -> EventLoopFuture<HTTP2StreamMultiplexer> {
if self.eventLoop.inEventLoop {
return self.eventLoop.makeCompletedFuture {
@@ -201,15 +236,17 @@ extension Channel {
/// - position: The position in the pipeline into which to insert this handler.
/// - inboundStreamInitializer: A closure that will be called whenever the remote peer initiates a new stream.
/// - Returns: An `EventLoopFuture` containing the `StreamMultiplexer` inserted into this pipeline, which can be used to initiate new streams.
public func configureHTTP2Pipeline(mode: NIOHTTP2Handler.ParserMode,
connectionConfiguration: NIOHTTP2Handler.ConnectionConfiguration,
streamConfiguration: NIOHTTP2Handler.StreamConfiguration,
streamDelegate: NIOHTTP2StreamDelegate? = nil,
position: ChannelPipeline.Position = .last,
inboundStreamInitializer: @escaping NIOChannelInitializer) -> EventLoopFuture<NIOHTTP2Handler.StreamMultiplexer> {
public func configureHTTP2Pipeline(
mode: NIOHTTP2Handler.ParserMode,
connectionConfiguration: NIOHTTP2Handler.ConnectionConfiguration,
streamConfiguration: NIOHTTP2Handler.StreamConfiguration,
streamDelegate: NIOHTTP2StreamDelegate? = nil,
position: ChannelPipeline.Position = .last,
inboundStreamInitializer: @escaping NIOChannelInitializer
) -> EventLoopFuture<NIOHTTP2Handler.StreamMultiplexer> {
if self.eventLoop.inEventLoop {
return self.eventLoop.makeCompletedFuture {
return try self.pipeline.syncOperations.configureHTTP2Pipeline(
try self.pipeline.syncOperations.configureHTTP2Pipeline(
mode: mode,
connectionConfiguration: connectionConfiguration,
streamConfiguration: streamConfiguration,
@@ -220,7 +257,7 @@ extension Channel {
}
} else {
return self.eventLoop.submit {
return try self.pipeline.syncOperations.configureHTTP2Pipeline(
try self.pipeline.syncOperations.configureHTTP2Pipeline(
mode: mode,
connectionConfiguration: connectionConfiguration,
streamConfiguration: streamConfiguration,
@@ -257,8 +294,10 @@ extension Channel {
/// negotiated, or if no protocol was negotiated. Must return a future that completes when the
/// channel has been fully mutated.
/// - Returns: An `EventLoopFuture<Void>` that completes when the channel is ready to negotiate.
public func configureHTTP2SecureUpgrade(h2ChannelConfigurator: @escaping NIOChannelInitializer,
http1ChannelConfigurator: @escaping NIOChannelInitializer) -> EventLoopFuture<Void> {
public func configureHTTP2SecureUpgrade(
h2ChannelConfigurator: @escaping NIOChannelInitializer,
http1ChannelConfigurator: @escaping NIOChannelInitializer
) -> EventLoopFuture<Void> {
let alpnHandler = ApplicationProtocolNegotiationHandler { result in
switch result {
case .negotiated("h2"):
@@ -297,7 +336,11 @@ extension Channel {
h2ConnectionChannelConfigurator: NIOChannelInitializer? = nil,
_ configurator: @escaping NIOChannelInitializer
) -> EventLoopFuture<Void> {
return self.configureCommonHTTPServerPipeline(h2ConnectionChannelConfigurator: h2ConnectionChannelConfigurator, targetWindowSize: 65535, configurator)
self.configureCommonHTTPServerPipeline(
h2ConnectionChannelConfigurator: h2ConnectionChannelConfigurator,
targetWindowSize: 65535,
configurator
)
}
/// Configures a `ChannelPipeline` to speak either HTTP/1.1 or HTTP/2 according to what can be negotiated with the client.
@@ -322,9 +365,14 @@ extension Channel {
targetWindowSize: Int,
_ configurator: @escaping NIOChannelInitializer
) -> EventLoopFuture<Void> {
return self._commonHTTPServerPipeline(configurator: configurator, h2ConnectionChannelConfigurator: h2ConnectionChannelConfigurator) { channel in
channel.configureHTTP2Pipeline(mode: .server, targetWindowSize: targetWindowSize) { streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap { () -> EventLoopFuture<Void> in
self._commonHTTPServerPipeline(
configurator: configurator,
h2ConnectionChannelConfigurator: h2ConnectionChannelConfigurator
) { channel in
channel.configureHTTP2Pipeline(mode: .server, targetWindowSize: targetWindowSize) {
streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap {
() -> EventLoopFuture<Void> in
configurator(streamChannel)
}
}.map { _ in () }
@@ -358,14 +406,18 @@ extension Channel {
h2ConnectionChannelConfigurator: NIOChannelInitializer? = nil,
configurator: @escaping NIOChannelInitializer
) -> EventLoopFuture<Void> {
return self._commonHTTPServerPipeline(configurator: configurator, h2ConnectionChannelConfigurator: h2ConnectionChannelConfigurator) { channel in
self._commonHTTPServerPipeline(
configurator: configurator,
h2ConnectionChannelConfigurator: h2ConnectionChannelConfigurator
) { channel in
channel.configureHTTP2Pipeline(
mode: .server,
connectionConfiguration: connectionConfiguration,
streamConfiguration: streamConfiguration,
streamDelegate: streamDelegate
) { streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap { () -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap {
() -> EventLoopFuture<Void> in
configurator(streamChannel)
}
}.map { _ in () }
@@ -391,8 +443,10 @@ extension Channel {
configurator(channel)
}
}
return self.configureHTTP2SecureUpgrade(h2ChannelConfigurator: h2ChannelConfigurator,
http1ChannelConfigurator: http1ChannelConfigurator)
return self.configureHTTP2SecureUpgrade(
h2ChannelConfigurator: h2ChannelConfigurator,
http1ChannelConfigurator: http1ChannelConfigurator
)
}
}
@@ -414,12 +468,14 @@ extension ChannelPipeline.SynchronousOperations {
/// - position: The position in the pipeline into which to insert this handler.
/// - inboundStreamInitializer: A closure that will be called whenever the remote peer initiates a new stream.
/// - Returns: The `StreamMultiplexer` inserted into this pipeline, which can be used to initiate new streams.
public func configureHTTP2Pipeline(mode: NIOHTTP2Handler.ParserMode,
connectionConfiguration: NIOHTTP2Handler.ConnectionConfiguration,
streamConfiguration: NIOHTTP2Handler.StreamConfiguration,
streamDelegate: NIOHTTP2StreamDelegate? = nil,
position: ChannelPipeline.Position = .last,
inboundStreamInitializer: @escaping NIOChannelInitializer) throws -> NIOHTTP2Handler.StreamMultiplexer {
public func configureHTTP2Pipeline(
mode: NIOHTTP2Handler.ParserMode,
connectionConfiguration: NIOHTTP2Handler.ConnectionConfiguration,
streamConfiguration: NIOHTTP2Handler.StreamConfiguration,
streamDelegate: NIOHTTP2StreamDelegate? = nil,
position: ChannelPipeline.Position = .last,
inboundStreamInitializer: @escaping NIOChannelInitializer
) throws -> NIOHTTP2Handler.StreamMultiplexer {
let handler = NIOHTTP2Handler(
mode: mode,
eventLoop: self.eventLoop,
@@ -451,15 +507,22 @@ extension ChannelPipeline.SynchronousOperations {
/// - inboundStreamInitializer: A closure that will be called whenever the remote peer initiates a new stream. This should almost always
/// be provided, especially on servers.
/// - Returns: An `EventLoopFuture` containing the `HTTP2StreamMultiplexer` inserted into this pipeline, which can be used to initiate new streams.
internal func configureHTTP2Pipeline(mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
targetWindowSize: Int = 65535,
inboundStreamInitializer: NIOChannelInitializer?) throws -> HTTP2StreamMultiplexer {
internal func configureHTTP2Pipeline(
mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
initialLocalSettings: [HTTP2Setting] = nioDefaultSettings,
position: ChannelPipeline.Position = .last,
targetWindowSize: Int = 65535,
inboundStreamInitializer: NIOChannelInitializer?
) throws -> HTTP2StreamMultiplexer {
let http2Handler = NIOHTTP2Handler(mode: mode, initialSettings: initialLocalSettings)
let multiplexer = HTTP2StreamMultiplexer(mode: mode, channel: channel, targetWindowSize: targetWindowSize, inboundStreamInitializer: inboundStreamInitializer)
let multiplexer = HTTP2StreamMultiplexer(
mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
inboundStreamInitializer: inboundStreamInitializer
)
try self.addHandler(http2Handler, position: position)
try self.addHandler(multiplexer, position: .after(http2Handler))
@@ -524,7 +587,7 @@ extension Channel {
) -> EventLoopFuture<NIOHTTP2Handler.AsyncStreamMultiplexer<Output>> {
if self.eventLoop.inEventLoop {
return self.eventLoop.makeCompletedFuture {
return try self.pipeline.syncOperations.configureAsyncHTTP2Pipeline(
try self.pipeline.syncOperations.configureAsyncHTTP2Pipeline(
mode: mode,
streamDelegate: streamDelegate,
configuration: configuration,
@@ -533,7 +596,7 @@ extension Channel {
}
} else {
return self.eventLoop.submit {
return try self.pipeline.syncOperations.configureAsyncHTTP2Pipeline(
try self.pipeline.syncOperations.configureAsyncHTTP2Pipeline(
mode: mode,
streamDelegate: streamDelegate,
configuration: configuration,
@@ -543,7 +606,6 @@ extension Channel {
}
}
/// Configures a channel to perform an HTTP/2 secure upgrade with typed negotiation results.
///
/// HTTP/2 secure upgrade uses the Application Layer Protocol Negotiation TLS extension to
@@ -576,7 +638,9 @@ extension Channel {
http2ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP2Output>
) -> EventLoopFuture<EventLoopFuture<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>> {
let alpnHandler = NIOTypedApplicationProtocolNegotiationHandler<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>() { result in
let alpnHandler = NIOTypedApplicationProtocolNegotiationHandler<
NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>
> { result in
switch result {
case .negotiated("h2"):
// Successful upgrade to HTTP/2. Let the user configure the pipeline.
@@ -594,7 +658,11 @@ extension Channel {
return self.pipeline
.addHandler(alpnHandler)
.flatMap { _ in
self.pipeline.handler(type: NIOTypedApplicationProtocolNegotiationHandler<NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>>.self).map { alpnHandler in
self.pipeline.handler(
type: NIOTypedApplicationProtocolNegotiationHandler<
NIONegotiatedHTTPVersion<HTTP1Output, HTTP2Output>
>.self
).map { alpnHandler in
alpnHandler.protocolNegotiationResult
}
}
@@ -621,26 +689,37 @@ extension Channel {
/// be waited on to retrieve the result of the negotiation.
@inlinable
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public func configureAsyncHTTPServerPipeline<HTTP1ConnectionOutput: Sendable, HTTP2ConnectionOutput: Sendable, HTTP2StreamOutput: Sendable>(
public func configureAsyncHTTPServerPipeline<
HTTP1ConnectionOutput: Sendable,
HTTP2ConnectionOutput: Sendable,
HTTP2StreamOutput: Sendable
>(
http2Configuration: NIOHTTP2Handler.Configuration = .init(),
http1ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP1ConnectionOutput>,
http2ConnectionInitializer: @escaping NIOChannelInitializerWithOutput<HTTP2ConnectionOutput>,
http2StreamInitializer: @escaping NIOChannelInitializerWithOutput<HTTP2StreamOutput>
) -> EventLoopFuture<EventLoopFuture<NIONegotiatedHTTPVersion<
HTTP1ConnectionOutput,
(HTTP2ConnectionOutput, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP2StreamOutput>)
>>> {
let http2ConnectionInitializer: NIOChannelInitializerWithOutput<(HTTP2ConnectionOutput, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP2StreamOutput>)> = { channel in
channel.configureAsyncHTTP2Pipeline(
mode: .server,
configuration: http2Configuration,
streamInitializer: http2StreamInitializer
).flatMap { multiplexer in
return http2ConnectionInitializer(channel).map { connectionChannel in
(connectionChannel, multiplexer)
) -> EventLoopFuture<
EventLoopFuture<
NIONegotiatedHTTPVersion<
HTTP1ConnectionOutput,
(HTTP2ConnectionOutput, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP2StreamOutput>)
>
>
> {
let http2ConnectionInitializer:
NIOChannelInitializerWithOutput<
(HTTP2ConnectionOutput, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP2StreamOutput>)
> = { channel in
channel.configureAsyncHTTP2Pipeline(
mode: .server,
configuration: http2Configuration,
streamInitializer: http2StreamInitializer
).flatMap { multiplexer in
http2ConnectionInitializer(channel).map { connectionChannel in
(connectionChannel, multiplexer)
}
}
}
}
let http1ConnectionInitializer: NIOChannelInitializerWithOutput<HTTP1ConnectionOutput> = { channel in
channel.pipeline.configureHTTPServerPipeline().flatMap { _ in
http1ConnectionInitializer(channel)
@@ -717,15 +796,20 @@ extension ChannelPipeline.SynchronousOperations {
streamConfiguration: configuration.stream,
streamDelegate: streamDelegate,
inboundStreamInitializerWithAnyOutput: { channel in
streamInitializer(channel).map { return $0 }
streamInitializer(channel).map { $0 }
}
)
try self.addHandler(handler)
let (inboundStreamChannels, continuation) = NIOHTTP2AsyncSequence.initialize(inboundStreamInitializerOutput: Output.self)
let (inboundStreamChannels, continuation) = NIOHTTP2AsyncSequence.initialize(
inboundStreamInitializerOutput: Output.self
)
return try handler.syncAsyncStreamMultiplexer(continuation: continuation, inboundStreamChannels: inboundStreamChannels)
return try handler.syncAsyncStreamMultiplexer(
continuation: continuation,
inboundStreamChannels: inboundStreamChannels
)
}
}
+7 -7
View File
@@ -35,7 +35,7 @@ public struct HTTP2SettingsParameter: Sendable {
internal init(fromNetwork value: Int32) {
self.networkRepresentation = UInt16(value)
}
/// Initialize a ``HTTP2SettingsParameter`` from a network `UInt16`.
internal init(fromPayload value: UInt16) {
self.networkRepresentation = value
@@ -63,14 +63,14 @@ public struct HTTP2SettingsParameter: Sendable {
/// Corresponds to `SETTINGS_MAX_HEADER_LIST_SIZE`
public static let maxHeaderListSize = HTTP2SettingsParameter(6)
/// Corresponds to `SETTINGS_ENABLE_CONNECT_PROTOCOL` from RFC 8441.
public static let enableConnectProtocol = HTTP2SettingsParameter(8)
}
extension HTTP2SettingsParameter: Equatable { }
extension HTTP2SettingsParameter: Equatable {}
extension HTTP2SettingsParameter: Hashable { }
extension HTTP2SettingsParameter: Hashable {}
/// A single setting for HTTP/2, a combination of a ``HTTP2SettingsParameter`` and its value.
public struct HTTP2Setting: Sendable {
@@ -80,7 +80,7 @@ public struct HTTP2Setting: Sendable {
/// The value of the settings parameter. This must be a 32-bit number.
public var value: Int {
get {
return Int(self._value)
Int(self._value)
}
set {
self._value = UInt32(newValue)
@@ -100,8 +100,8 @@ public struct HTTP2Setting: Sendable {
}
extension HTTP2Setting: Equatable {
public static func ==(lhs: HTTP2Setting, rhs: HTTP2Setting) -> Bool {
return lhs.parameter == rhs.parameter && lhs._value == rhs._value
public static func == (lhs: HTTP2Setting, rhs: HTTP2Setting) -> Bool {
lhs.parameter == rhs.parameter && lhs._value == rhs._value
}
}
+7 -7
View File
@@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//
import NIOHTTP1
import NIOHPACK
import NIOHTTP1
extension HPACKHeaders {
/// Whether this `HTTPHeaders` corresponds to a final response or not.
@@ -21,7 +21,7 @@ extension HPACKHeaders {
/// This function is only valid if called on a response header block. If the :status header
/// is not present, this will throw.
fileprivate func isInformationalResponse() throws -> Bool {
return try self.peekPseudoHeader(name: ":status").first! == "1"
try self.peekPseudoHeader(name: ":status").first! == "1"
}
}
@@ -65,23 +65,23 @@ struct HTTP2HeadersStateMachine {
// The first header block received on a server mode stream must be a request block.
newType = .requestHead
case (.client, .none),
(.client, .some(.informationalResponseHead)):
(.client, .some(.informationalResponseHead)):
// The first header block received on a client mode stream may be either informational or final,
// depending on the value of the :status pseudo-header. Alternatively, if the previous
// header block was informational, the same possibilities apply.
newType = try block.isInformationalResponse() ? .informationalResponseHead : .finalResponseHead
case (.server, .some(.requestHead)),
(.client, .some(.finalResponseHead)):
(.client, .some(.finalResponseHead)):
// If the sevrer has already received a request head, or the client has already received a final response,
// this is a trailer block.
newType = .trailer
case (.server, .some(.informationalResponseHead)),
(.server, .some(.finalResponseHead)),
(.client, .some(.requestHead)):
(.server, .some(.finalResponseHead)),
(.client, .some(.requestHead)):
// These states should not be reachable!
preconditionFailure("Invalid internal state!")
case (.server, .some(.trailer)),
(.client, .some(.trailer)):
(.client, .some(.trailer)):
// TODO(cory): This should probably throw, as this can happen in malformed programs without the world ending.
preconditionFailure("Sending too many header blocks.")
}
@@ -16,25 +16,25 @@ import NIOCore
/// Multiplexes outbound HTTP/2 frames from an HTTP/2 stream into an HTTP/2
/// connection.
internal protocol HTTP2OutboundStreamMultiplexer {
/// Write the frame into the HTTP/2 connection. Stream ID is included in the
/// frame.
func writeFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?)
/// Write the frame into the HTTP/2 connection. Stream ID is included in the
/// frame.
func writeFrame(_ frame: HTTP2Frame, promise: EventLoopPromise<Void>?)
/// Flush the stream with the given ID.
func flushStream(_ id: HTTP2StreamID)
/// Flush the stream with the given ID.
func flushStream(_ id: HTTP2StreamID)
/// Request a stream ID for the given channel.
///
/// Required to lazily assign a stream ID to a channel on first write.
func requestStreamID(forChannel: Channel) -> HTTP2StreamID
/// Request a stream ID for the given channel.
///
/// Required to lazily assign a stream ID to a channel on first write.
func requestStreamID(forChannel: Channel) -> HTTP2StreamID
/// Notify the multiplexer that the channel with the given ID closed.
///
/// Required as a channel may not have a stream ID when it closes.
func streamClosed(channelID: ObjectIdentifier)
/// Notify the multiplexer that the channel with the given ID closed.
///
/// Required as a channel may not have a stream ID when it closes.
func streamClosed(channelID: ObjectIdentifier)
/// Notify the multiplexer that the stream with the given ID closed.
func streamClosed(id: HTTP2StreamID)
/// Notify the multiplexer that the stream with the given ID closed.
func streamClosed(id: HTTP2StreamID)
}
extension HTTP2StreamChannel {
@@ -122,4 +122,3 @@ extension LegacyOutboundStreamMultiplexer: HTTP2OutboundStreamMultiplexer {
self.multiplexer.childChannelClosed(streamID: id)
}
}
+47 -30
View File
@@ -13,9 +13,8 @@
//===----------------------------------------------------------------------===//
import Atomics
import NIOCore
import NIOConcurrencyHelpers
import NIOCore
/// The various channel options specific to `HTTP2StreamChannel`s.
///
@@ -46,7 +45,7 @@ extension HTTP2StreamChannelOptions.Types {
public struct StreamIDOption: ChannelOption {
public typealias Value = HTTP2StreamID
public init() { }
public init() {}
}
}
@@ -152,14 +151,16 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
/// The stream data type of the channel.
private let streamDataType: HTTP2StreamDataType
internal init(allocator: ByteBufferAllocator,
parent: Channel,
multiplexer: OutboundStreamMultiplexer,
streamID: HTTP2StreamID?,
targetWindowSize: Int32,
outboundBytesHighWatermark: Int,
outboundBytesLowWatermark: Int,
streamDataType: HTTP2StreamDataType) {
internal init(
allocator: ByteBufferAllocator,
parent: Channel,
multiplexer: OutboundStreamMultiplexer,
streamID: HTTP2StreamID?,
targetWindowSize: Int32,
outboundBytesHighWatermark: Int,
outboundBytesLowWatermark: Int,
streamDataType: HTTP2StreamDataType
) {
self.allocator = allocator
self.closePromise = parent.eventLoop.makePromise()
self.localAddress = parent.localAddress
@@ -172,9 +173,11 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
self.flags = NIOLockedValueBox(Flags(isActive: false, isWritable: true))
self.state = .idle
self.streamDataType = streamDataType
self.writabilityManager = StreamChannelFlowController(highWatermark: outboundBytesHighWatermark,
lowWatermark: outboundBytesLowWatermark,
parentIsWritable: parent.isWritable)
self.writabilityManager = StreamChannelFlowController(
highWatermark: outboundBytesHighWatermark,
lowWatermark: outboundBytesLowWatermark,
parentIsWritable: parent.isWritable
)
// To begin with we initialize autoRead to false, but we are going to fetch it from our parent before we
// go much further.
@@ -182,7 +185,10 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
self._pipeline = ChannelPipeline(channel: self)
}
internal func configure(initializer: NIOChannelInitializerWithStreamID?, userPromise promise: EventLoopPromise<Channel>?) {
internal func configure(
initializer: NIOChannelInitializerWithStreamID?,
userPromise promise: EventLoopPromise<Channel>?
) {
assert(self.streamDataType == .frame)
// We need to configure this channel. This involves doing four things:
// 1. Setting our autoRead state from the parent
@@ -235,7 +241,10 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
// This variant is used in the async stream case.
// It uses `Any`s because when called from `configureInboundStream` it is passed the initializer stored on the handler
// which can't be a typed generic without changing the handler API.
internal func configure(initializer: @escaping NIOChannelInitializerWithOutput<any Sendable>, userPromise promise: EventLoopPromise<any Sendable>?) {
internal func configure(
initializer: @escaping NIOChannelInitializerWithOutput<any Sendable>,
userPromise promise: EventLoopPromise<any Sendable>?
) {
assert(self.streamDataType == .framePayload)
// We need to configure this channel. This involves doing four things:
// 1. Setting our autoRead state from the parent
@@ -255,7 +264,10 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
}
}
func onInitializationResult<Output: Sendable>(_ initializerResult: Result<Output, Error>, promise: EventLoopPromise<Output>?) {
func onInitializationResult<Output: Sendable>(
_ initializerResult: Result<Output, Error>,
promise: EventLoopPromise<Output>?
) {
switch initializerResult {
case .success(let output):
self.postInitializerActivate(output: output, promise: promise)
@@ -364,11 +376,11 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
private let multiplexer: OutboundStreamMultiplexer
public var closeFuture: EventLoopFuture<Void> {
return self.closePromise.futureResult
self.closePromise.futureResult
}
public var pipeline: ChannelPipeline {
return self._pipeline
self._pipeline
}
public let localAddress: SocketAddress?
@@ -452,7 +464,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
}
private var _isActive: Bool {
return self.state == .active || self.state == .closing || self.state == .localActive
self.state == .active || self.state == .closing || self.state == .localActive
}
public var isActive: Bool {
@@ -460,7 +472,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
}
public var _channelCore: ChannelCore {
return self
self
}
public let eventLoop: EventLoop
@@ -494,7 +506,9 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
///
/// To correctly respect flushes, we deliberately withold frames from the parent channel until this
/// stream is flushed, at which time we deliver them all. This buffer holds the pending ones.
private var pendingWrites: MarkedCircularBuffer<(HTTP2StreamData, EventLoopPromise<Void>?)> = MarkedCircularBuffer(initialCapacity: 8)
private var pendingWrites: MarkedCircularBuffer<(HTTP2StreamData, EventLoopPromise<Void>?)> = MarkedCircularBuffer(
initialCapacity: 8
)
/// A list node used to hold stream channels.
internal var streamChannelListNode: StreamChannelListNode = StreamChannelListNode()
@@ -549,7 +563,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
switch self.multiplexer {
case .inline:
promise = userPromise
break // do nothing at this stage, we'll be called back
break // do nothing at this stage, we'll be called back
case .legacy:
promise = userPromise ?? self.eventLoop.makePromise()
promise!.futureResult.hop(to: self.eventLoop).whenComplete { (_: Result<Void, Error>) in
@@ -734,7 +748,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore, @unchecked Sendable {
}
// MARK:- Functions used to manage pending reads and writes.
private extension HTTP2StreamChannel {
extension HTTP2StreamChannel {
/// Drop all pending reads.
private func dropPendingReads() {
/// We don't need to report the dropped reads, just remove them all.
@@ -816,7 +830,7 @@ private extension HTTP2StreamChannel {
}
// MARK:- Functions used to communicate between the `HTTP2StreamMultiplexer` and the `HTTP2StreamChannel`.
internal extension HTTP2StreamChannel {
extension HTTP2StreamChannel {
/// Called when a frame is received from the network.
///
/// - Parameters:
@@ -915,7 +929,8 @@ internal extension HTTP2StreamChannel {
// This is because in this case we really want user code to send a frame as soon as possible to avoid
// issues with their stream ID becoming out of date. Once the state transitions we can update
// the writability if needed.
guard case .changed(newValue: let localValue) = self.writabilityManager.parentWritabilityChanged(newValue) else {
guard case .changed(newValue: let localValue) = self.writabilityManager.parentWritabilityChanged(newValue)
else {
return
}
@@ -938,7 +953,9 @@ internal extension HTTP2StreamChannel {
extension HTTP2StreamChannel {
// A helper function used to ensure that state modification leads to changes in the channel active atomic.
private func modifyingState<ReturnType>(_ closure: (inout StreamChannelState) throws -> ReturnType) rethrows -> ReturnType {
private func modifyingState<ReturnType>(
_ closure: (inout StreamChannelState) throws -> ReturnType
) rethrows -> ReturnType {
defer {
self.flags.withLockedValue { $0.isActive = self._isActive }
}
@@ -949,7 +966,7 @@ extension HTTP2StreamChannel {
// MARK: Custom String Convertible
extension HTTP2StreamChannel {
public var description: String {
return "HTTP2StreamChannel(streamID: \(String(describing: self.streamID)), isActive: \(self.isActive), isWritable: \(self.isWritable))"
"HTTP2StreamChannel(streamID: \(String(describing: self.streamID)), isActive: \(self.isActive), isWritable: \(self.isWritable))"
}
}
@@ -972,12 +989,12 @@ extension HTTP2StreamChannel {
///
/// - Important: Must be called on the `EventLoop` the `Channel` is running on.
public func getOption<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
return try self.channel.getOption0(option)
try self.channel.getOption0(option)
}
}
/// Returns a view of the `Channel` exposing synchronous versions of `setOption` and `getOption`.
public var syncOptions: NIOSynchronousChannelOptions? {
return SynchronousOptions(channel: self)
SynchronousOptions(channel: self)
}
}
+21 -29
View File
@@ -41,13 +41,13 @@ public struct HTTP2StreamID: Sendable {
/// Returns a boolean indicating whether this stream ID relates to a client initiated stream.
public var isClientInitiated: Bool {
return self.networkStreamID % 2 == 1
self.networkStreamID % 2 == 1
}
/// Returns a boolean indicating whether this stream ID relates to a server initiated stream.
public var isServerInitiated: Bool {
// Noone may initiate the root stream.
return self.networkStreamID % 2 == 0 && self != .rootStream
self.networkStreamID % 2 == 0 && self != .rootStream
}
/// Create a ``HTTP2StreamID`` for a specific integer value.
@@ -61,7 +61,7 @@ public struct HTTP2StreamID: Sendable {
precondition(integerID >= 0, "\(integerID) is not a valid HTTP/2 stream ID value")
self.networkStreamID = integerID
}
/// Create a ``HTTP2StreamID`` from a 32-bit value received as part of a frame.
///
/// This will ignore the most significant bit of the provided value.
@@ -71,41 +71,37 @@ public struct HTTP2StreamID: Sendable {
}
// MARK:- Equatable conformance for HTTP2StreamID
extension HTTP2StreamID: Equatable { }
extension HTTP2StreamID: Equatable {}
// MARK:- Hashable conformance for HTTP2StreamID
extension HTTP2StreamID: Hashable { }
extension HTTP2StreamID: Hashable {}
// MARK:- Comparable conformance for HTTP2StreamID
extension HTTP2StreamID: Comparable {
public static func <(lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
return lhs.networkStreamID < rhs.networkStreamID
public static func < (lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
lhs.networkStreamID < rhs.networkStreamID
}
public static func >(lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
return lhs.networkStreamID > rhs.networkStreamID
public static func > (lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
lhs.networkStreamID > rhs.networkStreamID
}
public static func <=(lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
return lhs.networkStreamID <= rhs.networkStreamID
public static func <= (lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
lhs.networkStreamID <= rhs.networkStreamID
}
public static func >=(lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
return lhs.networkStreamID >= rhs.networkStreamID
public static func >= (lhs: HTTP2StreamID, rhs: HTTP2StreamID) -> Bool {
lhs.networkStreamID >= rhs.networkStreamID
}
}
// MARK:- CustomStringConvertible conformance for HTTP2StreamID
extension HTTP2StreamID: CustomStringConvertible {
public var description: String {
return "HTTP2StreamID(\(String(describing: self.networkStreamID)))"
"HTTP2StreamID(\(String(describing: self.networkStreamID)))"
}
}
// MARK:- ExpressibleByIntegerLiteral conformance for HTTP2StreamID
extension HTTP2StreamID: ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = Int32
@@ -116,39 +112,35 @@ extension HTTP2StreamID: ExpressibleByIntegerLiteral {
}
}
// MARK:- Strideable conformance for HTTP2StreamID
extension HTTP2StreamID: Strideable {
public typealias Stride = Int
public func advanced(by n: Stride) -> HTTP2StreamID {
return HTTP2StreamID(self.networkStreamID + Int32(n))
HTTP2StreamID(self.networkStreamID + Int32(n))
}
public func distance(to other: HTTP2StreamID) -> Stride {
return Int(other.networkStreamID - self.networkStreamID)
Int(other.networkStreamID - self.networkStreamID)
}
}
// MARK:- Helper initializers for integer conversion.
public extension Int {
extension Int {
/// Create an Int holding the integer value of this streamID.
init(_ http2StreamID: HTTP2StreamID) {
public init(_ http2StreamID: HTTP2StreamID) {
self = Int(http2StreamID.networkStreamID)
}
}
public extension Int32 {
extension Int32 {
/// Create an Int32 holding the integer value of this streamID.
init(_ http2StreamID: HTTP2StreamID) {
public init(_ http2StreamID: HTTP2StreamID) {
self = http2StreamID.networkStreamID
}
}
internal extension UInt32 {
extension UInt32 {
/// Create a UInt32 holding the integer value of this streamID.
init(_ http2StreamID: HTTP2StreamID) {
self = UInt32(http2StreamID.networkStreamID)
+117 -55
View File
@@ -47,7 +47,11 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let frame = self.unwrapInboundIn(data)
self.commonStreamMultiplexer.receivedFrame(frame, context: context, multiplexer: .legacy(LegacyOutboundStreamMultiplexer(multiplexer: self)))
self.commonStreamMultiplexer.receivedFrame(
frame,
context: context,
multiplexer: .legacy(LegacyOutboundStreamMultiplexer(multiplexer: self))
)
}
public func channelReadComplete(context: ChannelHandlerContext) {
@@ -66,12 +70,12 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
case .proceed:
context.flush()
case .waitForReadsToComplete:
break // flush will be executed on `channelReadComplete`
break // flush will be executed on `channelReadComplete`
}
}
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
/* for now just forward */
// for now just forward
context.write(data, promise: promise)
}
@@ -142,16 +146,28 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
/// receiving a `HEADERS` frame from a client. For clients, these are channels created by
/// receiving a `PUSH_PROMISE` frame from a server. To initiate a new outbound channel, use
/// ``createStreamChannel(promise:_:)-1jk0q``.
@available(*, deprecated, renamed: "init(mode:channel:targetWindowSize:outboundBufferSizeHighWatermark:outboundBufferSizeLowWatermark:inboundStreamInitializer:)")
public convenience init(mode: NIOHTTP2Handler.ParserMode, channel: Channel, targetWindowSize: Int = 65535, inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil) {
@available(
*,
deprecated,
renamed:
"init(mode:channel:targetWindowSize:outboundBufferSizeHighWatermark:outboundBufferSizeLowWatermark:inboundStreamInitializer:)"
)
public convenience init(
mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil
) {
// We default to an 8kB outbound buffer size: this is a good trade off for avoiding excessive buffering while ensuring that decent
// throughput can be maintained. We use 4kB as the low water mark.
self.init(mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
outboundBufferSizeHighWatermark: 8192,
outboundBufferSizeLowWatermark: 4096,
inboundStreamStateInitializer: .includesStreamID(inboundStreamStateInitializer))
self.init(
mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
outboundBufferSizeHighWatermark: 8192,
outboundBufferSizeLowWatermark: 4096,
inboundStreamStateInitializer: .includesStreamID(inboundStreamStateInitializer)
)
}
/// Create a new ``HTTP2StreamMultiplexer``.
@@ -171,18 +187,22 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
/// receiving a `HEADERS` frame from a client. For clients, these are channels created by
/// receiving a `PUSH_PROMISE` frame from a server. To initiate a new outbound channel, use
/// ``createStreamChannel(promise:_:)-1jk0q``.
public convenience init(mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
outboundBufferSizeHighWatermark: Int = 8196,
outboundBufferSizeLowWatermark: Int = 4092,
inboundStreamInitializer: NIOChannelInitializer?) {
self.init(mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
outboundBufferSizeHighWatermark: outboundBufferSizeHighWatermark,
outboundBufferSizeLowWatermark: outboundBufferSizeLowWatermark,
inboundStreamStateInitializer: .excludesStreamID(inboundStreamInitializer))
public convenience init(
mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
outboundBufferSizeHighWatermark: Int = 8196,
outboundBufferSizeLowWatermark: Int = 4092,
inboundStreamInitializer: NIOChannelInitializer?
) {
self.init(
mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
outboundBufferSizeHighWatermark: outboundBufferSizeHighWatermark,
outboundBufferSizeLowWatermark: outboundBufferSizeLowWatermark,
inboundStreamStateInitializer: .excludesStreamID(inboundStreamInitializer)
)
}
/// Create a new ``HTTP2StreamMultiplexer``.
@@ -200,27 +220,38 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
/// receiving a `HEADERS` frame from a client. For clients, these are channels created by
/// receiving a `PUSH_PROMISE` frame from a server. To initiate a new outbound channel, use
/// ``createStreamChannel(promise:_:)-1jk0q``.
@available(*, deprecated, renamed: "init(mode:channel:targetWindowSize:outboundBufferSizeHighWatermark:outboundBufferSizeLowWatermark:inboundStreamInitializer:)")
public convenience init(mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
outboundBufferSizeHighWatermark: Int,
outboundBufferSizeLowWatermark: Int,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil) {
self.init(mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
outboundBufferSizeHighWatermark: outboundBufferSizeHighWatermark,
outboundBufferSizeLowWatermark: outboundBufferSizeLowWatermark,
inboundStreamStateInitializer: .includesStreamID(inboundStreamStateInitializer))
@available(
*,
deprecated,
renamed:
"init(mode:channel:targetWindowSize:outboundBufferSizeHighWatermark:outboundBufferSizeLowWatermark:inboundStreamInitializer:)"
)
public convenience init(
mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
outboundBufferSizeHighWatermark: Int,
outboundBufferSizeLowWatermark: Int,
inboundStreamStateInitializer: NIOChannelInitializerWithStreamID? = nil
) {
self.init(
mode: mode,
channel: channel,
targetWindowSize: targetWindowSize,
outboundBufferSizeHighWatermark: outboundBufferSizeHighWatermark,
outboundBufferSizeLowWatermark: outboundBufferSizeLowWatermark,
inboundStreamStateInitializer: .includesStreamID(inboundStreamStateInitializer)
)
}
private init(mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
outboundBufferSizeHighWatermark: Int,
outboundBufferSizeLowWatermark: Int,
inboundStreamStateInitializer: MultiplexerAbstractChannel.InboundStreamStateInitializer) {
private init(
mode: NIOHTTP2Handler.ParserMode,
channel: Channel,
targetWindowSize: Int = 65535,
outboundBufferSizeHighWatermark: Int,
outboundBufferSizeLowWatermark: Int,
inboundStreamStateInitializer: MultiplexerAbstractChannel.InboundStreamStateInitializer
) {
self.channel = channel
self.commonStreamMultiplexer = HTTP2CommonInboundStreamMultiplexer(
@@ -234,8 +265,6 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun
}
}
extension HTTP2StreamMultiplexer {
/// Create a new `Channel` for a new stream initiated by this peer.
///
@@ -249,8 +278,14 @@ extension HTTP2StreamMultiplexer {
/// failed if an error occurs.
/// - streamStateInitializer: A callback that will be invoked to allow you to configure the
/// `ChannelPipeline` for the newly created channel.
public func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOChannelInitializer) {
let sendableView = HTTP2StreamMultiplexer.SendableView(http2StreamMultiplexer: self, eventLoop: self.channel.eventLoop)
public func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOChannelInitializer
) {
let sendableView = HTTP2StreamMultiplexer.SendableView(
http2StreamMultiplexer: self,
eventLoop: self.channel.eventLoop
)
if self.channel.eventLoop.inEventLoop {
sendableView.createStreamChannel(promise: promise, streamStateInitializer)
} else {
@@ -289,9 +324,19 @@ extension HTTP2StreamMultiplexer {
/// failed if an error occurs.
/// - streamStateInitializer: A callback that will be invoked to allow you to configure the
/// `ChannelPipeline` for the newly created channel.
@available(*, deprecated, message: "The signature of 'streamStateInitializer' has changed to '(Channel) -> EventLoopFuture<Void>'")
public func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOChannelInitializerWithStreamID) {
let sendableView = HTTP2StreamMultiplexer.SendableView(http2StreamMultiplexer: self, eventLoop: self.channel.eventLoop)
@available(
*,
deprecated,
message: "The signature of 'streamStateInitializer' has changed to '(Channel) -> EventLoopFuture<Void>'"
)
public func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOChannelInitializerWithStreamID
) {
let sendableView = HTTP2StreamMultiplexer.SendableView(
http2StreamMultiplexer: self,
eventLoop: self.channel.eventLoop
)
sendableView.createStreamChannel(promise: promise, streamStateInitializer)
}
}
@@ -305,28 +350,45 @@ extension HTTP2StreamMultiplexer {
let http2StreamMultiplexer: HTTP2StreamMultiplexer
let eventLoop: EventLoop
func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOChannelInitializer) {
func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOChannelInitializer
) {
// Always create streams channels on the next event loop tick. This avoids re-entrancy
// issues where handlers interposed between the two HTTP/2 handlers could create streams
// in channel active which become activated twice.
self.eventLoop.execute {
self.http2StreamMultiplexer.commonStreamMultiplexer.createStreamChannel(multiplexer: .legacy(LegacyOutboundStreamMultiplexer(multiplexer: self.http2StreamMultiplexer)), promise: promise, streamStateInitializer)
self.http2StreamMultiplexer.commonStreamMultiplexer.createStreamChannel(
multiplexer: .legacy(LegacyOutboundStreamMultiplexer(multiplexer: self.http2StreamMultiplexer)),
promise: promise,
streamStateInitializer
)
}
}
@available(*, deprecated, message: "The signature of 'streamStateInitializer' has changed to '(Channel) -> EventLoopFuture<Void>'")
func createStreamChannel(promise: EventLoopPromise<Channel>?, _ streamStateInitializer: @escaping NIOChannelInitializerWithStreamID) {
@available(
*,
deprecated,
message: "The signature of 'streamStateInitializer' has changed to '(Channel) -> EventLoopFuture<Void>'"
)
func createStreamChannel(
promise: EventLoopPromise<Channel>?,
_ streamStateInitializer: @escaping NIOChannelInitializerWithStreamID
) {
// Always create streams channels on the next event loop tick. This avoids re-entrancy
// issues where handlers interposed between the two HTTP/2 handlers could create streams
// in channel active which become activated twice.
self.eventLoop.execute {
self.http2StreamMultiplexer.commonStreamMultiplexer.createStreamChannel(multiplexer: .legacy(LegacyOutboundStreamMultiplexer(multiplexer: self.http2StreamMultiplexer)), promise: promise, streamStateInitializer)
self.http2StreamMultiplexer.commonStreamMultiplexer.createStreamChannel(
multiplexer: .legacy(LegacyOutboundStreamMultiplexer(multiplexer: self.http2StreamMultiplexer)),
promise: promise,
streamStateInitializer
)
}
}
}
}
// MARK:- Child to parent calls
extension HTTP2StreamMultiplexer {
internal func childChannelClosed(streamID: HTTP2StreamID) {
+81 -40
View File
@@ -13,17 +13,17 @@
//===----------------------------------------------------------------------===//
import NIOCore
import NIOHTTP1
import NIOHPACK
import NIOHTTP1
// MARK: - Client
fileprivate struct BaseClientCodec {
private struct BaseClientCodec {
private let protocolString: String
private let normalizeHTTPHeaders: Bool
private var headerStateMachine: HTTP2HeadersStateMachine = HTTP2HeadersStateMachine(mode: .client)
private var outgoingHTTP1RequestHead: HTTPRequestHead?
/// Initializes a `BaseClientCodec`.
@@ -46,13 +46,15 @@ fileprivate struct BaseClientCodec {
}
}
mutating func processInboundData(_ data: HTTP2Frame.FramePayload) throws -> (first: HTTPClientResponsePart?, second: HTTPClientResponsePart?) {
mutating func processInboundData(
_ data: HTTP2Frame.FramePayload
) throws -> (first: HTTPClientResponsePart?, second: HTTPClientResponsePart?) {
switch data {
case .headers(let headerContent):
switch try self.headerStateMachine.newHeaders(block: headerContent.headers) {
case .trailer:
return (first: .end(HTTPHeaders(regularHeadersFrom: headerContent.headers)), second: nil)
case .informationalResponseHead:
return (first: .head(try HTTPResponseHead(http2HeaderBlock: headerContent.headers)), second: nil)
@@ -72,7 +74,7 @@ fileprivate struct BaseClientCodec {
second = .end(nil)
}
return (first: first, second: second)
case .requestHead:
preconditionFailure("A client can not receive request heads")
}
@@ -91,28 +93,42 @@ fileprivate struct BaseClientCodec {
}
}
return (first: first, second: second)
case .alternativeService, .rstStream, .priority, .windowUpdate, .settings, .pushPromise, .ping, .goAway, .origin:
case .alternativeService, .rstStream, .priority, .windowUpdate, .settings, .pushPromise, .ping, .goAway,
.origin:
// These don't have an HTTP/1 equivalent, so let's drop them.
return (first: nil, second: nil)
}
}
mutating func processOutboundData(_ data: HTTPClientRequestPart, allocator: ByteBufferAllocator) throws -> HTTP2Frame.FramePayload {
mutating func processOutboundData(
_ data: HTTPClientRequestPart,
allocator: ByteBufferAllocator
) throws -> HTTP2Frame.FramePayload {
switch data {
case .head(let head):
precondition(self.outgoingHTTP1RequestHead == nil, "Only a single HTTP request allowed per HTTP2 stream")
let h1Headers = try HTTPHeaders(requestHead: head, protocolString: self.protocolString)
self.outgoingHTTP1RequestHead = head
let headerContent = HTTP2Frame.FramePayload.Headers(headers: HPACKHeaders(httpHeaders: h1Headers,
normalizeHTTPHeaders: self.normalizeHTTPHeaders))
let headerContent = HTTP2Frame.FramePayload.Headers(
headers: HPACKHeaders(
httpHeaders: h1Headers,
normalizeHTTPHeaders: self.normalizeHTTPHeaders
)
)
return .headers(headerContent)
case .body(let body):
return .data(HTTP2Frame.FramePayload.Data(data: body))
case .end(let trailers):
if let trailers = trailers {
return .headers(.init(headers: HPACKHeaders(httpHeaders: trailers,
normalizeHTTPHeaders: self.normalizeHTTPHeaders),
endStream: true))
return .headers(
.init(
headers: HPACKHeaders(
httpHeaders: trailers,
normalizeHTTPHeaders: self.normalizeHTTPHeaders
),
endStream: true
)
)
} else {
return .data(.init(data: .byteBuffer(allocator.buffer(capacity: 0)), endStream: true))
}
@@ -183,7 +199,10 @@ public final class HTTP2ToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutbou
let responsePart = self.unwrapOutboundIn(data)
do {
let transformedPayload = try self.baseCodec.processOutboundData(responsePart, allocator: context.channel.allocator)
let transformedPayload = try self.baseCodec.processOutboundData(
responsePart,
allocator: context.channel.allocator
)
let part = HTTP2Frame(streamID: self.streamID, payload: transformedPayload)
context.write(self.wrapOutboundOut(part), promise: promise)
} catch {
@@ -248,7 +267,10 @@ public final class HTTP2FramePayloadToHTTP1ClientCodec: ChannelInboundHandler, C
let requestPart = self.unwrapOutboundIn(data)
do {
let transformedPayload = try self.baseCodec.processOutboundData(requestPart, allocator: context.channel.allocator)
let transformedPayload = try self.baseCodec.processOutboundData(
requestPart,
allocator: context.channel.allocator
)
context.write(self.wrapOutboundOut(transformedPayload), promise: promise)
} catch {
promise?.fail(error)
@@ -259,7 +281,7 @@ public final class HTTP2FramePayloadToHTTP1ClientCodec: ChannelInboundHandler, C
// MARK: - Server
fileprivate struct BaseServerCodec {
private struct BaseServerCodec {
private let normalizeHTTPHeaders: Bool
private var headerStateMachine: HTTP2HeadersStateMachine = HTTP2HeadersStateMachine(mode: .server)
@@ -267,13 +289,18 @@ fileprivate struct BaseServerCodec {
self.normalizeHTTPHeaders = normalizeHTTPHeaders
}
mutating func processInboundData(_ data: HTTP2Frame.FramePayload) throws -> (first: HTTPServerRequestPart?, second: HTTPServerRequestPart?) {
mutating func processInboundData(
_ data: HTTP2Frame.FramePayload
) throws -> (first: HTTPServerRequestPart?, second: HTTPServerRequestPart?) {
switch data {
case .headers(let headerContent):
if case .trailer = try self.headerStateMachine.newHeaders(block: headerContent.headers) {
return (first: .end(HTTPHeaders(regularHeadersFrom: headerContent.headers)), second: nil)
} else {
let reqHead = try HTTPRequestHead(http2HeaderBlock: headerContent.headers, isEndStream: headerContent.endStream)
let reqHead = try HTTPRequestHead(
http2HeaderBlock: headerContent.headers,
isEndStream: headerContent.endStream
)
let first = HTTPServerRequestPart.head(reqHead)
var second: HTTPServerRequestPart? = nil
@@ -302,21 +329,34 @@ fileprivate struct BaseServerCodec {
}
}
mutating func processOutboundData(_ data: HTTPServerResponsePart, allocator: ByteBufferAllocator) -> HTTP2Frame.FramePayload {
mutating func processOutboundData(
_ data: HTTPServerResponsePart,
allocator: ByteBufferAllocator
) -> HTTP2Frame.FramePayload {
switch data {
case .head(let head):
let h1 = HTTPHeaders(responseHead: head)
let payload = HTTP2Frame.FramePayload.Headers(headers: HPACKHeaders(httpHeaders: h1,
normalizeHTTPHeaders: self.normalizeHTTPHeaders))
let payload = HTTP2Frame.FramePayload.Headers(
headers: HPACKHeaders(
httpHeaders: h1,
normalizeHTTPHeaders: self.normalizeHTTPHeaders
)
)
return .headers(payload)
case .body(let body):
let payload = HTTP2Frame.FramePayload.Data(data: body)
return .data(payload)
case .end(let trailers):
if let trailers = trailers {
return .headers(.init(headers: HPACKHeaders(httpHeaders: trailers,
normalizeHTTPHeaders: self.normalizeHTTPHeaders),
endStream: true))
return .headers(
.init(
headers: HPACKHeaders(
httpHeaders: trailers,
normalizeHTTPHeaders: self.normalizeHTTPHeaders
),
endStream: true
)
)
} else {
return .data(.init(data: .byteBuffer(allocator.buffer(capacity: 0)), endStream: true))
}
@@ -324,7 +364,6 @@ fileprivate struct BaseServerCodec {
}
}
/// A simple channel handler that translates HTTP/2 concepts into HTTP/1 data types,
/// and vice versa, for use on the server side.
///
@@ -436,9 +475,9 @@ public final class HTTP2FramePayloadToHTTP1ServerCodec: ChannelInboundHandler, C
}
}
private extension HTTPMethod {
extension HTTPMethod {
/// Create a `HTTPMethod` from the string representation of that method.
init(methodString: String) {
fileprivate init(methodString: String) {
switch methodString {
case "GET":
self = .GET
@@ -514,7 +553,7 @@ private extension HTTPMethod {
// MARK:- Methods for creating `HTTPRequestHead`/`HTTPResponseHead` objects from
// header blocks generated by the HTTP/2 layer.
internal extension HTTPRequestHead {
extension HTTPRequestHead {
/// Create a `HTTPRequestHead` from the header block.
init(http2HeaderBlock headers: HPACKHeaders, isEndStream: Bool) throws {
// A request head should have only up to four psuedo-headers.
@@ -567,8 +606,7 @@ internal extension HTTPRequestHead {
}
}
internal extension HTTPResponseHead {
extension HTTPResponseHead {
/// Create a `HTTPResponseHead` from the header block. Use this for informational responses.
init(http2HeaderBlock headers: HPACKHeaders) throws {
// A response head should have only one psuedo-header. We strip it off.
@@ -579,15 +617,18 @@ internal extension HTTPResponseHead {
let status = HTTPResponseStatus(statusCode: integerStatus)
self.init(version: .init(major: 2, minor: 0), status: status, headers: HTTPHeaders(regularHeadersFrom: headers))
}
/// Create a `HTTPResponseHead` from the header block. Use this for non-informational responses.
init(http2HeaderBlock headers: HPACKHeaders, requestHead: HTTPRequestHead, endStream: Bool) throws {
try self.init(http2HeaderBlock: headers)
// NOTE: We don't need to create the header array here ourselves. The HTTP/1 response
// headers, will not contain a :status field, but may contain a "Transfer-Encoding"
// field. For this reason the default allocation works great for us.
if self.shouldAddContentLengthOrTransferEncodingHeaderToResponse(hpackHeaders: headers, requestHead: requestHead) {
if self.shouldAddContentLengthOrTransferEncodingHeaderToResponse(
hpackHeaders: headers,
requestHead: requestHead
) {
if endStream {
self.headers.add(name: "content-length", value: "0")
} else {
@@ -595,9 +636,10 @@ internal extension HTTPResponseHead {
}
}
}
private func shouldAddContentLengthOrTransferEncodingHeaderToResponse(
hpackHeaders: HPACKHeaders, requestHead: HTTPRequestHead
hpackHeaders: HPACKHeaders,
requestHead: HTTPRequestHead
) -> Bool {
let statusCode = self.status.code
return !(100..<200).contains(statusCode)
@@ -605,11 +647,10 @@ internal extension HTTPResponseHead {
&& statusCode != 304
&& requestHead.method != .HEAD
&& requestHead.method != .CONNECT
&& !hpackHeaders.contains(name: "content-length") // compare on h2 headers - for speed
&& !hpackHeaders.contains(name: "content-length") // compare on h2 headers - for speed
}
}
extension HPACKHeaders {
/// Grabs a pseudo-header from a header block. Does not remove it.
///
@@ -638,7 +679,6 @@ extension HPACKHeaders {
}
}
extension HTTPHeaders {
fileprivate init(requestHead: HTTPRequestHead, protocolString: String) throws {
// To avoid too much allocation we create an array first, and then initialize the HTTPHeaders from it.
@@ -686,7 +726,9 @@ extension HTTPHeaders {
var newHeaders: [(String, String)] = []
newHeaders.reserveCapacity(responseHead.headers.count + 1)
newHeaders.append((":status", String(responseHead.status.code)))
responseHead.headers.forEach { newHeaders.append(($0.name, $0.value)) }
for responseHeader in responseHead.headers {
newHeaders.append((responseHeader.name, responseHeader.value))
}
self.init(newHeaders)
}
@@ -700,7 +742,6 @@ extension HTTPHeaders {
}
}
extension Array where Element == (String, String) {
mutating func appendRegularHeaders(from headers: HPACKHeaders) {
for (name, value, _) in headers {
+6 -8
View File
@@ -35,8 +35,7 @@ public struct StreamClosedEvent: Sendable {
}
}
extension StreamClosedEvent: Hashable { }
extension StreamClosedEvent: Hashable {}
/// A ``NIOHTTP2WindowUpdatedEvent`` is fired whenever a flow control window is changed.
/// This includes changes on the connection flow control window, which is signalled by
@@ -49,7 +48,7 @@ public struct NIOHTTP2WindowUpdatedEvent {
/// The new inbound window size for this stream, if any. May be nil if this stream is half-closed.
public var inboundWindowSize: Int? {
get {
return self._inboundWindowSize.map { Int($0) }
self._inboundWindowSize.map { Int($0) }
}
set {
self._inboundWindowSize = newValue.map { Int32($0) }
@@ -59,7 +58,7 @@ public struct NIOHTTP2WindowUpdatedEvent {
/// The new outbound window size for this stream, if any. May be nil if this stream is half-closed.
public var outboundWindowSize: Int? {
get {
return self._outboundWindowSize.map { Int($0) }
self._outboundWindowSize.map { Int($0) }
}
set {
self._outboundWindowSize = newValue.map { Int32($0) }
@@ -83,8 +82,7 @@ public struct NIOHTTP2WindowUpdatedEvent {
}
}
extension NIOHTTP2WindowUpdatedEvent: Hashable { }
extension NIOHTTP2WindowUpdatedEvent: Hashable {}
/// A ``NIOHTTP2StreamCreatedEvent`` is fired whenever a HTTP/2 stream is created.
public struct NIOHTTP2StreamCreatedEvent {
@@ -110,7 +108,7 @@ public struct NIOHTTP2StreamCreatedEvent {
}
}
extension NIOHTTP2StreamCreatedEvent: Hashable { }
extension NIOHTTP2StreamCreatedEvent: Hashable {}
/// A ``NIOHTTP2BulkStreamWindowChangeEvent`` is fired whenever all of the remote flow control windows for a given stream have been changed.
///
@@ -125,4 +123,4 @@ public struct NIOHTTP2BulkStreamWindowChangeEvent {
}
}
extension NIOHTTP2BulkStreamWindowChangeEvent: Hashable { }
extension NIOHTTP2BulkStreamWindowChangeEvent: Hashable {}
+2 -4
View File
@@ -53,13 +53,12 @@ class InboundEventBuffer {
}
}
// MARK:- Sequence conformance
extension InboundEventBuffer: Sequence {
typealias Element = BufferedHTTP2UserEvent
func makeIterator() -> InboundEventBufferIterator {
return InboundEventBufferIterator(self)
InboundEventBufferIterator(self)
}
struct InboundEventBufferIterator: IteratorProtocol {
@@ -81,10 +80,9 @@ extension InboundEventBuffer: Sequence {
}
}
// MARK:- CustomStringConvertible conformance
extension InboundEventBuffer: CustomStringConvertible {
var description: String {
return "InboundEventBuffer { \(self.buffer) }"
"InboundEventBuffer { \(self.buffer) }"
}
}
@@ -28,35 +28,41 @@ struct MultiplexerAbstractChannel {
@usableFromInline private(set) var baseChannel: HTTP2StreamChannel
@usableFromInline
init(allocator: ByteBufferAllocator,
parent: Channel,
multiplexer: HTTP2StreamChannel.OutboundStreamMultiplexer,
streamID: HTTP2StreamID?,
targetWindowSize: Int32,
outboundBytesHighWatermark: Int,
outboundBytesLowWatermark: Int,
inboundStreamStateInitializer: InboundStreamStateInitializer) {
init(
allocator: ByteBufferAllocator,
parent: Channel,
multiplexer: HTTP2StreamChannel.OutboundStreamMultiplexer,
streamID: HTTP2StreamID?,
targetWindowSize: Int32,
outboundBytesHighWatermark: Int,
outboundBytesLowWatermark: Int,
inboundStreamStateInitializer: InboundStreamStateInitializer
) {
switch inboundStreamStateInitializer {
case .includesStreamID:
assert(streamID != nil)
self.baseChannel = .init(allocator: allocator,
parent: parent,
multiplexer: multiplexer,
streamID: streamID,
targetWindowSize: targetWindowSize,
outboundBytesHighWatermark: outboundBytesHighWatermark,
outboundBytesLowWatermark: outboundBytesLowWatermark,
streamDataType: .frame)
self.baseChannel = .init(
allocator: allocator,
parent: parent,
multiplexer: multiplexer,
streamID: streamID,
targetWindowSize: targetWindowSize,
outboundBytesHighWatermark: outboundBytesHighWatermark,
outboundBytesLowWatermark: outboundBytesLowWatermark,
streamDataType: .frame
)
case .excludesStreamID, .returnsAny:
self.baseChannel = .init(allocator: allocator,
parent: parent,
multiplexer: multiplexer,
streamID: streamID,
targetWindowSize: targetWindowSize,
outboundBytesHighWatermark: outboundBytesHighWatermark,
outboundBytesLowWatermark: outboundBytesLowWatermark,
streamDataType: .framePayload)
self.baseChannel = .init(
allocator: allocator,
parent: parent,
multiplexer: multiplexer,
streamID: streamID,
targetWindowSize: targetWindowSize,
outboundBytesHighWatermark: outboundBytesHighWatermark,
outboundBytesLowWatermark: outboundBytesLowWatermark,
streamDataType: .framePayload
)
}
}
}
@@ -73,21 +79,21 @@ extension MultiplexerAbstractChannel {
// MARK: API for HTTP2StreamMultiplexer
extension MultiplexerAbstractChannel {
var streamID: HTTP2StreamID? {
return self.baseChannel.streamID
self.baseChannel.streamID
}
@usableFromInline
var channelID: ObjectIdentifier {
return ObjectIdentifier(self.baseChannel)
ObjectIdentifier(self.baseChannel)
}
var inList: Bool {
return self.baseChannel.inList
self.baseChannel.inList
}
var streamChannelListNode: StreamChannelListNode {
get {
return self.baseChannel.streamChannelListNode
self.baseChannel.streamChannelListNode
}
nonmutating set {
self.baseChannel.streamChannelListNode = newValue
@@ -108,7 +114,9 @@ extension MultiplexerAbstractChannel {
func configureInboundStream(initializer: InboundStreamStateInitializer, promise: EventLoopPromise<any Sendable>?) {
switch initializer {
case .includesStreamID, .excludesStreamID:
preconditionFailure("Configuration with a supplied `Any` promise is not supported with this initializer type.")
preconditionFailure(
"Configuration with a supplied `Any` promise is not supported with this initializer type."
)
case .returnsAny(let initializer):
self.baseChannel.configure(initializer: initializer, userPromise: promise)
}
@@ -126,7 +134,10 @@ extension MultiplexerAbstractChannel {
// used for async multiplexer
@usableFromInline
func configure(initializer: @escaping NIOChannelInitializerWithOutput<any Sendable>, userPromise promise: EventLoopPromise<any Sendable>?) {
func configure(
initializer: @escaping NIOChannelInitializerWithOutput<any Sendable>,
userPromise promise: EventLoopPromise<any Sendable>?
) {
self.baseChannel.configure(initializer: initializer, userPromise: promise)
}
@@ -173,8 +184,8 @@ extension MultiplexerAbstractChannel {
extension MultiplexerAbstractChannel: Equatable {
@inlinable
static func ==(lhs: MultiplexerAbstractChannel, rhs: MultiplexerAbstractChannel) -> Bool {
return lhs.baseChannel === rhs.baseChannel
static func == (lhs: MultiplexerAbstractChannel, rhs: MultiplexerAbstractChannel) -> Bool {
lhs.baseChannel === rhs.baseChannel
}
}
@@ -41,15 +41,13 @@ struct StreamChannelFlowController {
}
}
extension StreamChannelFlowController {
/// Whether the `HTTP2StreamChannel` should be writable.
var isWritable: Bool {
return self.watermarkedController.isWritable && self.parentIsWritable
self.watermarkedController.isWritable && self.parentIsWritable
}
}
extension StreamChannelFlowController {
/// A value representing a change in writability.
enum WritabilityChange: Hashable {
@@ -61,29 +59,29 @@ extension StreamChannelFlowController {
}
}
extension StreamChannelFlowController {
/// Notifies the flow controller that we have queued some bytes for writing to the network.
mutating func bufferedBytes(_ bufferedBytes: Int) -> WritabilityChange {
return self.mayChangeWritability {
self.mayChangeWritability {
$0.watermarkedController.bufferedBytes(bufferedBytes)
}
}
/// Notifies the flow controller that we have successfully written some bytes to the network.
mutating func wroteBytes(_ writtenBytes: Int) -> WritabilityChange {
return self.mayChangeWritability {
self.mayChangeWritability {
$0.watermarkedController.wroteBytes(writtenBytes)
}
}
mutating func parentWritabilityChanged(_ newWritability: Bool) -> WritabilityChange {
return self.mayChangeWritability {
self.mayChangeWritability {
$0.parentIsWritable = newWritability
}
}
private mutating func mayChangeWritability(_ body: (inout StreamChannelFlowController) -> Void) -> WritabilityChange {
private mutating func mayChangeWritability(_ body: (inout StreamChannelFlowController) -> Void) -> WritabilityChange
{
let wasWritable = self.isWritable
body(&self)
let isWritable = self.isWritable
@@ -96,12 +94,10 @@ extension StreamChannelFlowController {
}
}
extension StreamChannelFlowController: Equatable { }
extension StreamChannelFlowController: Equatable {}
extension StreamChannelFlowController: CustomDebugStringConvertible {
var debugDescription: String {
return "StreamChannelFlowController(parentIsWritable: \(self.parentIsWritable), watermarkedController: \(self.watermarkedController.debugDescription))"
"StreamChannelFlowController(parentIsWritable: \(self.parentIsWritable), watermarkedController: \(self.watermarkedController.debugDescription))"
}
}
+2 -5
View File
@@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//
/// A linked list for storing MultiplexerAbstractChannels.
///
/// Note that while this object *could* conform to `Sequence`, there is minimal value in doing
@@ -34,10 +33,9 @@ struct StreamChannelListNode {
fileprivate var state: ListState = .notInList
internal init() { }
internal init() {}
}
extension StreamChannelList {
/// Append an element to the linked list.
mutating func append(_ element: MultiplexerAbstractChannel) {
@@ -80,11 +78,10 @@ extension StreamChannelList {
}
mutating func removeAll() {
while self.removeFirst() != nil { }
while self.removeFirst() != nil {}
}
}
// MARK:- IntrusiveLinkedListElement helpers.
extension HTTP2StreamChannel {
/// Whether this element is currently in a list.
+29 -15
View File
@@ -14,7 +14,7 @@
import NIOCore
fileprivate let binarySearchThreshold = 200
private let binarySearchThreshold = 200
/// StreamMap is a specialized data structure built to optimise lookups of per-stream state.
///
@@ -75,7 +75,10 @@ struct StreamMap<Element: PerStreamData> {
}
/// Calls the closure with the given element, if it is present. Returns nil if the element is not present.
mutating func modify<ReturnType>(streamID: HTTP2StreamID, _ body: (inout Element) throws -> ReturnType) rethrows -> ReturnType? {
mutating func modify<ReturnType>(
streamID: HTTP2StreamID,
_ body: (inout Element) throws -> ReturnType
) rethrows -> ReturnType? {
if streamID.mayBeInitiatedBy(.client) {
guard let index = self.clientInitiated.findIndexForStreamID(streamID) else {
return nil
@@ -139,9 +142,11 @@ struct StreamMap<Element: PerStreamData> {
///
/// This helper can turn a complex operation that involves repeated resizing of the base objects into a much faster one that also avoids
/// allocation.
mutating func dropDataWithStreamIDGreaterThan(_ streamID: HTTP2StreamID,
initiatedBy role: HTTP2ConnectionStateMachine.ConnectionRole,
_ block: (CircularBuffer<Element>.SubSequence) -> Void) {
mutating func dropDataWithStreamIDGreaterThan(
_ streamID: HTTP2StreamID,
initiatedBy role: HTTP2ConnectionStateMachine.ConnectionRole,
_ block: (CircularBuffer<Element>.SubSequence) -> Void
) {
switch role {
case .client:
let index = self.clientInitiated.findIndexForFirstStreamIDLargerThan(streamID)
@@ -155,7 +160,7 @@ struct StreamMap<Element: PerStreamData> {
}
}
internal extension StreamMap where Element == HTTP2StreamStateMachine {
extension StreamMap where Element == HTTP2StreamStateMachine {
// This function exists as a performance optimisation: we can keep track of the index and where we are, and
// thereby avoid searching the array twice.
//
@@ -173,12 +178,13 @@ internal extension StreamMap where Element == HTTP2StreamStateMachine {
}
}
// This function exists as a performance optimisation: we can keep track of the index and where we are, and
// thereby avoid searching the array twice.
mutating func transformOrCreateAutoClose<T>(streamID: HTTP2StreamID,
_ creator: () throws -> Element,
_ transformer: (inout Element) -> T) rethrows -> T? {
mutating func transformOrCreateAutoClose<T>(
streamID: HTTP2StreamID,
_ creator: () throws -> Element,
_ transformer: (inout Element) -> T
) rethrows -> T? {
if streamID.mayBeInitiatedBy(.client) {
return try self.clientInitiated.transformOrCreateAutoClose(streamID: streamID, creator, transformer)
} else {
@@ -299,7 +305,10 @@ extension CircularBuffer where Element == HTTP2StreamStateMachine {
/// - modifier: A block that will modify the contained value in the
/// map, if there is one present.
/// - Returns: The return value of the block or `nil` if the element was not in the map.
mutating func autoClosingTransform<ResultType>(streamID: HTTP2StreamID, _ modifier: (inout Element) -> ResultType) -> ResultType? {
mutating func autoClosingTransform<ResultType>(
streamID: HTTP2StreamID,
_ modifier: (inout Element) -> ResultType
) -> ResultType? {
guard let index = self.findIndexForStreamID(streamID) else {
return nil
}
@@ -309,9 +318,11 @@ extension CircularBuffer where Element == HTTP2StreamStateMachine {
// This function exists as a performance optimisation: we can keep track of the index and where we are, and
// thereby avoid searching the array twice.
mutating func transformOrCreateAutoClose<ResultType>(streamID: HTTP2StreamID,
_ creator: () throws -> Element,
_ transformer: (inout Element) -> ResultType) rethrows -> ResultType? {
mutating func transformOrCreateAutoClose<ResultType>(
streamID: HTTP2StreamID,
_ creator: () throws -> Element,
_ transformer: (inout Element) -> ResultType
) rethrows -> ResultType? {
if let index = self.findIndexForStreamID(streamID) {
return self.modifyAutoClose(index: index, transformer)
} else {
@@ -321,7 +332,10 @@ extension CircularBuffer where Element == HTTP2StreamStateMachine {
}
}
private mutating func modifyAutoClose<ResultType>(index: Index, _ modifier: (inout Element) -> ResultType) -> ResultType {
private mutating func modifyAutoClose<ResultType>(
index: Index,
_ modifier: (inout Element) -> ResultType
) -> ResultType {
let (result, closed): (ResultType, HTTP2StreamStateMachine.StreamClosureState) = self.modify(index) {
let result = modifier(&$0)
return (result, $0.closed)
File diff suppressed because it is too large. Load diff
@@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//
/// Keeps track of whether or not a `Channel` should be able to write based on watermarks.
///
/// A `WatermarkedFlowController` is a straightforward object that keeps track of
@@ -49,7 +48,10 @@ struct WatermarkedFlowController {
internal private(set) var isWritable: Bool
internal init(highWatermark: Int, lowWatermark: Int) {
precondition(lowWatermark < highWatermark, "Low watermark \(lowWatermark) exceeds or meets High watermark \(highWatermark)")
precondition(
lowWatermark < highWatermark,
"Low watermark \(lowWatermark) exceeds or meets High watermark \(highWatermark)"
)
precondition(lowWatermark >= 0, "Low watermark \(lowWatermark) is negative")
self.highWatermark = highWatermark
@@ -59,7 +61,6 @@ struct WatermarkedFlowController {
}
}
extension WatermarkedFlowController {
/// Notifies the flow controller that we have buffered some bytes to send to the network.
mutating func bufferedBytes(_ bufferedBytes: Int) {
@@ -91,12 +92,10 @@ extension WatermarkedFlowController {
}
}
extension WatermarkedFlowController: Equatable { }
extension WatermarkedFlowController: Equatable {}
extension WatermarkedFlowController: CustomDebugStringConvertible {
var debugDescription: String {
return "WatermarkedFlowController(highWatermark: \(self.highWatermark), lowWatermark: \(self.lowWatermark), pendingBytes: \(self.pendingBytes), isWritable: \(self.isWritable))"
"WatermarkedFlowController(highWatermark: \(self.highWatermark), lowWatermark: \(self.lowWatermark), pendingBytes: \(self.pendingBytes), isWritable: \(self.isWritable))"
}
}
@@ -13,9 +13,9 @@
//===----------------------------------------------------------------------===//
import NIOCore
import NIOPosix
import NIOHTTP1
import NIOHTTP2
import NIOPosix
final class Bench1Conn10kRequests: Benchmark {
var group: MultiThreadedEventLoopGroup!
@@ -40,7 +40,7 @@ final class Bench1Conn10kRequests: Benchmark {
func run() throws -> Int {
var bodyByteCount = 0
for _ in 0 ..< 10_000 {
for _ in 0..<10_000 {
bodyByteCount += try sendOneRequest(channel: self.client, multiplexer: self.clientMultiplexer)
}
return bodyByteCount
@@ -55,14 +55,15 @@ func setupServer(group: EventLoopGroup) throws -> Channel {
// Set the handlers that are applied to the accepted Channels
.childChannelInitializer { channel in
return channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap { () -> EventLoopFuture<Void> in
channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap {
() -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP1TestServer())
}.flatMap { () -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(ErrorHandler())
}
}.flatMap { (_: HTTP2StreamMultiplexer) in
return channel.pipeline.addHandler(ErrorHandler())
channel.pipeline.addHandler(ErrorHandler())
}
}
@@ -77,30 +78,39 @@ func setupServer(group: EventLoopGroup) throws -> Channel {
func sendOneRequest(channel: Channel, multiplexer: HTTP2StreamMultiplexer) throws -> Int {
let responseReceivedPromise = channel.eventLoop.makePromise(of: Int.self)
let requestStreamInitializer: NIOChannelInitializer = { channel in
return channel.pipeline.addHandlers([HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https),
SendRequestHandler(host: "127.0.0.1",
request: .init(version: .init(major: 2, minor: 0),
method: .GET,
uri: "/", headers: ["host": "localhost"]),
responseReceivedPromise: responseReceivedPromise),
ErrorHandler()],
position: .last)
channel.pipeline.addHandlers(
[
HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https),
SendRequestHandler(
host: "127.0.0.1",
request: .init(
version: .init(major: 2, minor: 0),
method: .GET,
uri: "/",
headers: ["host": "localhost"]
),
responseReceivedPromise: responseReceivedPromise
),
ErrorHandler(),
],
position: .last
)
}
channel.pipeline.handler(type: HTTP2StreamMultiplexer.self).whenSuccess { multiplexer in
multiplexer.createStreamChannel(promise: nil, requestStreamInitializer)
multiplexer.createStreamChannel(promise: nil, requestStreamInitializer)
}
return try responseReceivedPromise.futureResult.wait()
}
func setupClient(group: EventLoopGroup, address: SocketAddress) throws -> (Channel, HTTP2StreamMultiplexer) {
return try ClientBootstrap(group: group)
try ClientBootstrap(group: group)
.channelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)
.channelInitializer { channel in
channel.pipeline.addHandler(ErrorHandler())
}
.connect(to: address).flatMap { channel in
channel.configureHTTP2Pipeline(mode: .client, position: .first) { channel -> EventLoopFuture<Void> in
return channel.eventLoop.makeSucceededFuture(())
channel.eventLoop.makeSucceededFuture(())
}.map { (channel, $0) }
}.wait()
}
@@ -125,11 +135,21 @@ final class HTTP1TestServer: ChannelInboundHandler {
var headers = HTTPHeaders()
headers.add(name: "content-length", value: "5")
headers.add(name: "x-stream-id", value: String(Int(streamID)))
channel.write(loopBoundSelf.value.wrapOutboundOut(HTTPServerResponsePart.head(HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers))), promise: nil)
channel.write(
loopBoundSelf.value.wrapOutboundOut(
HTTPServerResponsePart.head(
HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers)
)
),
promise: nil
)
var buffer = channel.allocator.buffer(capacity: 12)
buffer.writeStaticString("hello")
channel.write(loopBoundSelf.value.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil)
channel.write(
loopBoundSelf.value.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))),
promise: nil
)
return channel.writeAndFlush(loopBoundSelf.value.wrapOutboundOut(HTTPServerResponsePart.end(nil)))
}.whenComplete { _ in
loopBoundContext.value.close(promise: nil)
@@ -39,10 +39,9 @@ final class HPACKHeaderCanonicalFormBenchmark {
}
}
extension HPACKHeaderCanonicalFormBenchmark: Benchmark {
func setUp() throws { }
func tearDown() { }
func setUp() throws {}
func tearDown() {}
func run() throws -> Int {
var count = 0
@@ -15,7 +15,6 @@
import NIOCore
import NIOHPACK
final class HPACKHeaderDecodingBenchmark {
private var headers: ByteBuffer
private let loopCount: Int
@@ -28,11 +27,10 @@ final class HPACKHeaderDecodingBenchmark {
}
}
extension HPACKHeaderDecodingBenchmark: Benchmark {
func setUp() throws { }
func setUp() throws {}
func tearDown() { }
func tearDown() {}
func run() throws -> Int {
for _ in 0..<self.loopCount {
@@ -44,7 +42,6 @@ extension HPACKHeaderDecodingBenchmark: Benchmark {
}
}
extension HPACKHeaders {
fileprivate var encoded: ByteBuffer {
var encoder = HPACKEncoder(allocator: .init())
@@ -15,7 +15,6 @@
import NIOCore
import NIOHPACK
final class HPACKHeaderEncodingBenchmark {
private let headers: HPACKHeaders
private let loopCount: Int
@@ -30,11 +29,10 @@ final class HPACKHeaderEncodingBenchmark {
}
}
extension HPACKHeaderEncodingBenchmark: Benchmark {
func setUp() throws { }
func setUp() throws {}
func tearDown() { }
func tearDown() {}
func run() throws -> Int {
for _ in 0..<self.loopCount {
@@ -46,26 +44,37 @@ extension HPACKHeaderEncodingBenchmark: Benchmark {
}
}
extension HPACKHeaders {
static let indexable: HPACKHeaders = {
var headers = HPACKHeaders()
headers.add(contentsOf: [(":method", "GET"), (":path", "/"), (":scheme", "https"), (":authority", "localhost"), ("foo", "bar")],
indexing: .indexable)
headers.add(
contentsOf: [
(":method", "GET"), (":path", "/"), (":scheme", "https"), (":authority", "localhost"), ("foo", "bar"),
],
indexing: .indexable
)
return headers
}()
static let nonIndexable: HPACKHeaders = {
var headers = HPACKHeaders()
headers.add(contentsOf: [(":method", "GET"), (":path", "/"), (":scheme", "https"), (":authority", "localhost"), ("foo", "bar")],
indexing: .nonIndexable)
headers.add(
contentsOf: [
(":method", "GET"), (":path", "/"), (":scheme", "https"), (":authority", "localhost"), ("foo", "bar"),
],
indexing: .nonIndexable
)
return headers
}()
static let neverIndexed: HPACKHeaders = {
var headers = HPACKHeaders()
headers.add(contentsOf: [(":method", "GET"), (":path", "/"), (":scheme", "https"), (":authority", "localhost"), ("foo", "bar")],
indexing: .neverIndexed)
headers.add(
contentsOf: [
(":method", "GET"), (":path", "/"), (":scheme", "https"), (":authority", "localhost"), ("foo", "bar"),
],
indexing: .neverIndexed
)
return headers
}()
}
@@ -31,7 +31,6 @@ final class HPACKHeadersNormalizationOfHTTPHeadersBenchmark {
}
}
extension HPACKHeadersNormalizationOfHTTPHeadersBenchmark: Benchmark {
func setUp() throws {
switch self.httpHeadersKind {
@@ -42,12 +41,12 @@ extension HPACKHeadersNormalizationOfHTTPHeadersBenchmark: Benchmark {
}
}
func tearDown() { }
func tearDown() {}
func run() throws -> Int {
var count = 0
for _ in 0 ..< self.iterations {
for _ in 0..<self.iterations {
let normalize = HPACKHeaders(httpHeaders: self.httpHeaders, normalizeHTTPHeaders: true)
count &+= normalize.count
}
@@ -61,10 +60,10 @@ extension HTTPHeaders {
var httpHeaders: HTTPHeaders = [:]
httpHeaders.reserveCapacity(count + 1)
let connectionHeaderValue = (0 ..< count).map(String.init).joined(separator: ",")
let connectionHeaderValue = (0..<count).map(String.init).joined(separator: ",")
httpHeaders.add(name: "connection", value: connectionHeaderValue)
for i in 0 ..< count {
for i in 0..<count {
let header = String(describing: i)
httpHeaders.add(name: header, value: header)
}
@@ -78,10 +77,10 @@ extension HTTPHeaders {
// Assuming connection header values are decomposed into an array then this is the
// worst case: the whole array must be scanned for each of the `count` headers we add below.
let connectionHeaderValue = (0 ..< count).map { _ in "a" }.joined(separator: ",")
let connectionHeaderValue = (0..<count).map { _ in "a" }.joined(separator: ",")
httpHeaders.add(name: "connection", value: connectionHeaderValue)
for _ in 0 ..< count {
for _ in 0..<count {
httpHeaders.add(name: "b", value: "")
}
@@ -16,7 +16,6 @@ import Foundation
import NIOCore
import NIOHPACK
/// This benchmark is mostly attempting to stress the Huffman Decoding implementation by
/// way of using larger or more complex strings.
final class HuffmanDecodingBenchmark {
@@ -27,7 +26,11 @@ final class HuffmanDecodingBenchmark {
init(huffmanBytes: TestType, loopCount: Int) {
self.loopCount = loopCount
self.decoder = HPACKDecoder(allocator: .init(), maxDynamicTableSize: HPACKDecoder.maxDynamicTableSize, maxHeaderListSize: .max)
self.decoder = HPACKDecoder(
allocator: .init(),
maxDynamicTableSize: HPACKDecoder.maxDynamicTableSize,
maxHeaderListSize: .max
)
self.buffer = ByteBufferAllocator().buffer(capacity: 1024)
self.testType = huffmanBytes
}
@@ -47,21 +50,21 @@ final class HuffmanDecodingBenchmark {
}
}
extension HuffmanDecodingBenchmark: Benchmark {
func setUp() throws {
// We encode this header with both the name and value as a huffman string, never indexed.
self.buffer.writeInteger(UInt8(0x10)) // Never indexed, non-indexed name
self.buffer.encodeInteger(1, prefix: 7, prefixBits: 0x80) // Name length, huffman encoded, 7-bit integer
self.buffer.writeInteger(UInt8(0x97)) // Huffman encoded "f"
self.buffer.encodeInteger(UInt(self.testType.huffmanBytes.count), prefix: 7, prefixBits: 0x80) // Value length, huffman encoded, 7-bit integer
// Value length, huffman encoded, 7-bit integer
self.buffer.encodeInteger(UInt(self.testType.huffmanBytes.count), prefix: 7, prefixBits: 0x80)
self.buffer.writeBytes(self.testType.huffmanBytes)
// Run a single iteration of the loop. This warms up the encoder and decoder and ensures all the pages are mapped.
try self.loopIteration()
}
func tearDown() { }
func tearDown() {}
func run() throws -> Int {
for _ in 0..<self.loopCount {
@@ -77,7 +80,6 @@ extension HuffmanDecodingBenchmark: Benchmark {
}
}
extension Array where Element == UInt8 {
static let basicHuffmanBytes: [UInt8] = {
// This is hilariously slow (TWO intermediate Data objects!) but it's only invoked in setup so who cares?
@@ -95,10 +97,12 @@ extension Array where Element == UInt8 {
}()
// The location of the test fixtures.
private static let fixtureDirectoryURL = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Tests").appendingPathComponent("NIOHPACKTests").appendingPathComponent("Fixtures").absoluteURL
private static let fixtureDirectoryURL = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
.deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("Tests").appendingPathComponent(
"NIOHPACKTests"
).appendingPathComponent("Fixtures").absoluteURL
}
extension ByteBuffer {
// Copied directly from our internal implementation.
fileprivate mutating func encodeInteger(_ value: UInt, prefix: Int, prefixBits: UInt8 = 0) {
@@ -15,7 +15,6 @@
import NIOCore
import NIOHPACK
/// This benchmark is mostly attempting to stress the Huffman Encoding implementation by
/// way of using larger or more complex strings.
final class HuffmanEncodingBenchmark {
@@ -34,14 +33,13 @@ final class HuffmanEncodingBenchmark {
}
}
extension HuffmanEncodingBenchmark: Benchmark {
func setUp() throws {
// Run a single iteration of the loop. This warms up the encoder and decoder and ensures all the pages are mapped.
try self.loopIteration()
}
func tearDown() { }
func tearDown() {}
func run() throws -> Int {
for _ in 0..<self.loopCount {
@@ -57,17 +55,17 @@ extension HuffmanEncodingBenchmark: Benchmark {
}
}
extension String {
static let basicHuffmanString: String = {
var text = "Hello, world. I am a header value; I have Teh Texts. I am going on for quite a long time because I want to ensure that the encoded data buffer needs to be expanded to test out that code. I'll try some meta-characters too: \r\t\n ought to do it, no?"
static let basicHuffman: String = {
var text =
"Hello, world. I am a header value; I have Teh Texts. I am going on for quite a long time because I want to ensure that the encoded data buffer needs to be expanded to test out that code. I'll try some meta-characters too: \r\t\n ought to do it, no?"
while text.count < 1024 * 128 {
text += text
}
return text
}()
static let complexHuffmanString: String = {
static let complexHuffman: String = {
var text = "午セイ谷高ぐふあト食71入ツエヘナ津県を類及オモ曜一購ごきわ致掲ぎぐず敗文輪へけり鯖審ヘ塊米卸呪おぴ。"
while text.utf8.count < 128 * 1024 {
text += text
@@ -52,9 +52,12 @@ final class ServerOnly10KRequestsBenchmark: Benchmark {
headers.add(name: ":authority", value: "localhost", indexing: .nonIndexable)
headers.add(name: ":path", value: "/", indexing: .indexable)
headers.add(name: ":scheme", value: "https", indexing: .indexable)
headers.add(name: "user-agent",
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable)
headers.add(
name: "user-agent",
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable
)
headers.add(name: "accept-encoding", value: "gzip, deflate", indexing: .indexable)
var hpackEncoder = HPACKEncoder(allocator: .init())
@@ -115,7 +118,7 @@ final class ServerOnly10KRequestsBenchmark: Benchmark {
func setUp() throws {
let channel = EmbeddedChannel()
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(TestServer())
streamChannel.pipeline.addHandler(TestServer())
}.wait()
try channel.connect(to: .init(unixDomainSocketPath: "/fake"), promise: nil)
@@ -126,7 +129,7 @@ final class ServerOnly10KRequestsBenchmark: Benchmark {
initialBytes.writeImmutableBuffer(self.settingsACK)
try channel.writeInbound(initialBytes)
while try channel.readOutbound(as: ByteBuffer.self) != nil { }
while try channel.readOutbound(as: ByteBuffer.self) != nil {}
self.channel = channel
}
@@ -149,7 +152,7 @@ final class ServerOnly10KRequestsBenchmark: Benchmark {
private func sendInterleavedRequests(_ interleavedRequests: Int) throws -> Int {
var streamID = self.streamID
for _ in 0 ..< interleavedRequests {
for _ in 0..<interleavedRequests {
self.headersFrame.setInteger(UInt32(Int32(streamID)), at: self.headersFrame.readerIndex + 5)
try self.channel!.writeInbound(self.headersFrame)
streamID = streamID.advanced(by: 2)
@@ -157,7 +160,7 @@ final class ServerOnly10KRequestsBenchmark: Benchmark {
streamID = self.streamID
for _ in 0 ..< interleavedRequests {
for _ in 0..<interleavedRequests {
self.dataFrame.setInteger(UInt32(Int32(streamID)), at: self.dataFrame.readerIndex + 5)
try self.channel!.writeInbound(self.dataFrame)
streamID = streamID.advanced(by: 2)
@@ -176,7 +179,7 @@ final class ServerOnly10KRequestsBenchmark: Benchmark {
}
}
fileprivate class TestServer: ChannelInboundHandler {
private class TestServer: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame.FramePayload
public typealias OutboundOut = HTTP2Frame.FramePayload
@@ -27,9 +27,12 @@ final class StreamTeardownBenchmark: Benchmark {
headers.add(name: ":authority", value: "localhost", indexing: .nonIndexable)
headers.add(name: ":path", value: "/", indexing: .indexable)
headers.add(name: ":scheme", value: "https", indexing: .indexable)
headers.add(name: "user-agent",
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable)
headers.add(
name: "user-agent",
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
indexing: .nonIndexable
)
headers.add(name: "accept-encoding", value: "gzip, deflate", indexing: .indexable)
var hpackEncoder = HPACKEncoder(allocator: .init())
@@ -87,14 +90,14 @@ final class StreamTeardownBenchmark: Benchmark {
self.concurrentStreams = concurrentStreams
}
func setUp() { }
func setUp() {}
func tearDown() { }
func tearDown() {}
func createChannel() throws -> EmbeddedChannel {
let channel = EmbeddedChannel()
_ = try channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(DoNothingServer())
streamChannel.pipeline.addHandler(DoNothingServer())
}.wait()
try channel.pipeline.addHandler(SendGoawayHandler(expectedStreams: self.concurrentStreams)).wait()
@@ -106,7 +109,7 @@ final class StreamTeardownBenchmark: Benchmark {
initialBytes.writeImmutableBuffer(self.settingsACK)
try channel.writeInbound(initialBytes)
while try channel.readOutbound(as: ByteBuffer.self) != nil { }
while try channel.readOutbound(as: ByteBuffer.self) != nil {}
return channel
}
@@ -127,10 +130,13 @@ final class StreamTeardownBenchmark: Benchmark {
return bodyByteCount
}
private func sendInterleavedRequestsAndTerminate(_ interleavedRequests: Int, _ channel: EmbeddedChannel) throws -> Int {
private func sendInterleavedRequestsAndTerminate(
_ interleavedRequests: Int,
_ channel: EmbeddedChannel
) throws -> Int {
var streamID = HTTP2StreamID(1)
for _ in 0 ..< interleavedRequests {
for _ in 0..<interleavedRequests {
self.headersFrame.setInteger(UInt32(Int32(streamID)), at: self.headersFrame.readerIndex + 5)
try channel.writeInbound(self.headersFrame)
streamID = streamID.advanced(by: 2)
@@ -148,17 +154,18 @@ final class StreamTeardownBenchmark: Benchmark {
}
}
fileprivate class DoNothingServer: ChannelInboundHandler {
private class DoNothingServer: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame.FramePayload
public typealias OutboundOut = HTTP2Frame.FramePayload
}
fileprivate class SendGoawayHandler: ChannelInboundHandler {
private class SendGoawayHandler: ChannelInboundHandler {
public typealias InboundIn = HTTP2Frame
public typealias OutboundOut = HTTP2Frame
private static let goawayFrame: HTTP2Frame = HTTP2Frame(
streamID: .rootStream, payload: .goAway(lastStreamID: .rootStream, errorCode: .enhanceYourCalm, opaqueData: nil)
streamID: .rootStream,
payload: .goAway(lastStreamID: .rootStream, errorCode: .enhanceYourCalm, opaqueData: nil)
)
private let expectedStreams: Int
+91 -29
View File
@@ -12,19 +12,21 @@
//
//===----------------------------------------------------------------------===//
import Foundation
import Dispatch
import Foundation
// MARK: Test Harness
var _warning: String = ""
assert({
print("======================================================")
print("= YOU ARE RUNNING NIOPerformanceTester IN DEBUG MODE =")
print("======================================================")
_warning = " <<< DEBUG MODE >>>"
return true
}())
assert(
{
print("======================================================")
print("= YOU ARE RUNNING NIOPerformanceTester IN DEBUG MODE =")
print("======================================================")
_warning = " <<< DEBUG MODE >>>"
return true
}()
)
let warning = _warning
public func measure(_ fn: () throws -> Int) rethrows -> [TimeInterval] {
@@ -35,7 +37,7 @@ public func measure(_ fn: () throws -> Int) rethrows -> [TimeInterval] {
return end.timeIntervalSince(start)
}
_ = try measureOne(fn) /* pre-heat and throw away */
_ = try measureOne(fn) // pre-heat and throw away
var measurements = Array(repeating: 0.0, count: 10)
for i in 0..<10 {
measurements[i] = try measureOne(fn)
@@ -46,7 +48,7 @@ public func measure(_ fn: () throws -> Int) rethrows -> [TimeInterval] {
let limitSet = ProcessInfo.processInfo.arguments.dropFirst()
public func measureAndPrint(desc: String, fn: () throws -> Int) rethrows -> Void {
public func measureAndPrint(desc: String, fn: () throws -> Int) rethrows {
#if CACHEGRIND
// When CACHEGRIND is set we only run a single requested test, and we only run it once. No printing or other mess.
if limitSet.contains(desc) {
@@ -76,29 +78,89 @@ func measureAndPrint<B: Benchmark>(desc: String, benchmark bench: @autoclosure (
bench.tearDown()
}
try measureAndPrint(desc: desc) {
return try bench.run()
try bench.run()
}
}
// MARK: Utilities
try measureAndPrint(desc: "1_conn_10k_reqs", benchmark: Bench1Conn10kRequests())
try measureAndPrint(desc: "encode_100k_header_blocks_indexable", benchmark: HPACKHeaderEncodingBenchmark(headers: .indexable, loopCount: 100_000))
try measureAndPrint(desc: "encode_100k_header_blocks_nonindexable", benchmark: HPACKHeaderEncodingBenchmark(headers: .nonIndexable, loopCount: 100_000))
try measureAndPrint(desc: "encode_100k_header_blocks_neverIndexed", benchmark: HPACKHeaderEncodingBenchmark(headers: .neverIndexed, loopCount: 100_000))
try measureAndPrint(desc: "decode_100k_header_blocks_indexable", benchmark: HPACKHeaderDecodingBenchmark(headers: .indexable, loopCount: 100_000))
try measureAndPrint(desc: "decode_100k_header_blocks_nonindexable", benchmark: HPACKHeaderDecodingBenchmark(headers: .nonIndexable, loopCount: 100_000))
try measureAndPrint(desc: "decode_100k_header_blocks_neverIndexed", benchmark: HPACKHeaderDecodingBenchmark(headers: .neverIndexed, loopCount: 100_000))
try measureAndPrint(
desc: "encode_100k_header_blocks_indexable",
benchmark: HPACKHeaderEncodingBenchmark(headers: .indexable, loopCount: 100_000)
)
try measureAndPrint(
desc: "encode_100k_header_blocks_nonindexable",
benchmark: HPACKHeaderEncodingBenchmark(headers: .nonIndexable, loopCount: 100_000)
)
try measureAndPrint(
desc: "encode_100k_header_blocks_neverIndexed",
benchmark: HPACKHeaderEncodingBenchmark(headers: .neverIndexed, loopCount: 100_000)
)
try measureAndPrint(
desc: "decode_100k_header_blocks_indexable",
benchmark: HPACKHeaderDecodingBenchmark(headers: .indexable, loopCount: 100_000)
)
try measureAndPrint(
desc: "decode_100k_header_blocks_nonindexable",
benchmark: HPACKHeaderDecodingBenchmark(headers: .nonIndexable, loopCount: 100_000)
)
try measureAndPrint(
desc: "decode_100k_header_blocks_neverIndexed",
benchmark: HPACKHeaderDecodingBenchmark(headers: .neverIndexed, loopCount: 100_000)
)
try measureAndPrint(desc: "hpackheaders_canonical_form", benchmark: HPACKHeaderCanonicalFormBenchmark(.noTrimming))
try measureAndPrint(desc: "hpackheaders_canonical_form_trimming_whitespace", benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespace))
try measureAndPrint(desc: "hpackheaders_canonical_form_trimming_whitespace_short_strings", benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespaceFromShortStrings))
try measureAndPrint(desc: "hpackheaders_canonical_form_trimming_whitespace_long_strings", benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespaceFromLongStrings))
try measureAndPrint(desc: "hpackheaders_normalize_httpheaders_removing_10k_conn_headers", benchmark: HPACKHeadersNormalizationOfHTTPHeadersBenchmark(headersKind: .manyUniqueConnectionHeaderValues(10_000), iterations: 10))
try measureAndPrint(desc: "hpackheaders_normalize_httpheaders_keeping_10k_conn_headers", benchmark: HPACKHeadersNormalizationOfHTTPHeadersBenchmark(headersKind: .manyConnectionHeaderValuesWhichAreNotRemoved(10_000), iterations: 10))
try measureAndPrint(desc: "huffman_encode_basic", benchmark: HuffmanEncodingBenchmark(huffmanString: .basicHuffmanString, loopCount: 100))
try measureAndPrint(desc: "huffman_encode_complex", benchmark: HuffmanEncodingBenchmark(huffmanString: .complexHuffmanString, loopCount: 100))
try measureAndPrint(desc: "huffman_decode_basic", benchmark: HuffmanDecodingBenchmark(huffmanBytes: .basicHuffmanBytes, loopCount: 25))
try measureAndPrint(desc: "huffman_decode_complex", benchmark: HuffmanDecodingBenchmark(huffmanBytes: .complexHuffmanBytes, loopCount: 10))
try measureAndPrint(desc: "server_only_10k_requests_1_concurrent", benchmark: ServerOnly10KRequestsBenchmark(concurrentStreams: 1))
try measureAndPrint(desc: "server_only_10k_requests_100_concurrent", benchmark: ServerOnly10KRequestsBenchmark(concurrentStreams: 100))
try measureAndPrint(desc: "stream_teardown_10k_requests_100_concurrent", benchmark: StreamTeardownBenchmark(concurrentStreams: 100))
try measureAndPrint(
desc: "hpackheaders_canonical_form_trimming_whitespace",
benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespace)
)
try measureAndPrint(
desc: "hpackheaders_canonical_form_trimming_whitespace_short_strings",
benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespaceFromShortStrings)
)
try measureAndPrint(
desc: "hpackheaders_canonical_form_trimming_whitespace_long_strings",
benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespaceFromLongStrings)
)
try measureAndPrint(
desc: "hpackheaders_normalize_httpheaders_removing_10k_conn_headers",
benchmark: HPACKHeadersNormalizationOfHTTPHeadersBenchmark(
headersKind: .manyUniqueConnectionHeaderValues(10_000),
iterations: 10
)
)
try measureAndPrint(
desc: "hpackheaders_normalize_httpheaders_keeping_10k_conn_headers",
benchmark: HPACKHeadersNormalizationOfHTTPHeadersBenchmark(
headersKind: .manyConnectionHeaderValuesWhichAreNotRemoved(10_000),
iterations: 10
)
)
try measureAndPrint(
desc: "huffman_encode_basic",
benchmark: HuffmanEncodingBenchmark(huffmanString: .basicHuffman, loopCount: 100)
)
try measureAndPrint(
desc: "huffman_encode_complex",
benchmark: HuffmanEncodingBenchmark(huffmanString: .complexHuffman, loopCount: 100)
)
try measureAndPrint(
desc: "huffman_decode_basic",
benchmark: HuffmanDecodingBenchmark(huffmanBytes: .basicHuffmanBytes, loopCount: 25)
)
try measureAndPrint(
desc: "huffman_decode_complex",
benchmark: HuffmanDecodingBenchmark(huffmanBytes: .complexHuffmanBytes, loopCount: 10)
)
try measureAndPrint(
desc: "server_only_10k_requests_1_concurrent",
benchmark: ServerOnly10KRequestsBenchmark(concurrentStreams: 1)
)
try measureAndPrint(
desc: "server_only_10k_requests_100_concurrent",
benchmark: ServerOnly10KRequestsBenchmark(concurrentStreams: 100)
)
try measureAndPrint(
desc: "stream_teardown_10k_requests_100_concurrent",
benchmark: StreamTeardownBenchmark(concurrentStreams: 100)
)
+23 -15
View File
@@ -16,11 +16,10 @@
// hit it with curl like so: curl --http2-prior-knowledge http://localhost:8888/
import Foundation
import NIOCore
import NIOPosix
import NIOHTTP1
import NIOHTTP2
import NIOPosix
final class HTTP1TestServer: ChannelInboundHandler {
public typealias InboundIn = HTTPServerRequestPart
@@ -54,12 +53,22 @@ final class HTTP1TestServer: ChannelInboundHandler {
var headers = HTTPHeaders()
headers.add(name: "content-length", value: "5")
headers.add(name: "x-stream-id", value: String(Int(streamID)))
channel.write(http1TestServer.wrapOutboundOut(HTTPServerResponsePart.head(HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers))), promise: nil)
channel.write(
http1TestServer.wrapOutboundOut(
HTTPServerResponsePart.head(
HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers)
)
),
promise: nil
)
if requestHead.method != .HEAD {
var buffer = channel.allocator.buffer(capacity: 12)
buffer.writeStaticString("hello")
channel.write(http1TestServer.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil)
channel.write(
http1TestServer.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))),
promise: nil
)
}
return channel.writeAndFlush(http1TestServer.wrapOutboundOut(HTTPServerResponsePart.end(nil)))
@@ -70,17 +79,15 @@ final class HTTP1TestServer: ChannelInboundHandler {
}
}
final class ErrorHandler: ChannelInboundHandler {
typealias InboundIn = Never
func errorCaught(context: ChannelHandlerContext, error: Error) {
print("Server received error: \(error)")
context.close(promise: nil)
}
}
// First argument is the program path
let arguments = ProcessInfo.processInfo.arguments
let arg1 = arguments.dropFirst().first
@@ -99,16 +106,16 @@ enum BindTo {
let htdocs: String
let bindTarget: BindTo
switch (arg1, arg1.flatMap { Int($0) }, arg2, arg2.flatMap { Int($0) }, arg3) {
case (.some(let h), _ , _, .some(let p), let maybeHtdocs):
/* second arg an integer --> host port [htdocs] */
case (.some(let h), _, _, .some(let p), let maybeHtdocs):
// second arg an integer --> host port [htdocs]
bindTarget = .ip(host: h, port: p)
htdocs = maybeHtdocs ?? defaultHtdocs
case (_, .some(let p), let maybeHtdocs, _, _):
/* first arg an integer --> port [htdocs] */
// first arg an integer --> port [htdocs]
bindTarget = .ip(host: defaultHost, port: p)
htdocs = maybeHtdocs ?? defaultHtdocs
case (.some(let portString), .none, let maybeHtdocs, .none, .none):
/* couldn't parse as number --> uds-path [htdocs] */
// couldn't parse as number --> uds-path [htdocs]
bindTarget = .unixDomainSocket(path: portString)
htdocs = maybeHtdocs ?? defaultHtdocs
default:
@@ -124,14 +131,15 @@ let bootstrap = ServerBootstrap(group: group)
// Set the handlers that are applied to the accepted Channels
.childChannelInitializer { channel in
return channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
return streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap { () -> EventLoopFuture<Void> in
channel.configureHTTP2Pipeline(mode: .server) { streamChannel -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP2FramePayloadToHTTP1ServerCodec()).flatMap {
() -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(HTTP1TestServer())
}.flatMap { () -> EventLoopFuture<Void> in
streamChannel.pipeline.addHandler(ErrorHandler())
}
}.flatMap { (_: HTTP2StreamMultiplexer) in
return channel.pipeline.addHandler(ErrorHandler())
channel.pipeline.addHandler(ErrorHandler())
}
}
@@ -153,7 +161,7 @@ let channel = try { () -> Channel in
case .unixDomainSocket(let path):
return try bootstrap.bind(unixDomainSocketPath: path).wait()
}
}()
}()
print("Server started and listening on \(channel.localAddress!), htdocs path \(htdocs)")
+147 -78
View File
@@ -12,8 +12,9 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import XCTest
@testable import NIOHPACK
class HPACKCodingTests: XCTestCase {
@@ -29,29 +30,37 @@ class HPACKCodingTests: XCTestCase {
// HPACK RFC7541 § C.3
// http://httpwg.org/specs/rfc7541.html#request.examples.without.huffman.coding
func testRequestHeadersWithoutHuffmanCoding() throws {
var request1 = buffer(wrapping: [0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d])
var request2 = buffer(wrapping: [0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65])
var request3 = buffer(wrapping: [0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65])
var request1 = buffer(wrapping: [
0x82, 0x86, 0x84, 0x41, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
0x6f, 0x6d,
])
var request2 = buffer(wrapping: [
0x82, 0x86, 0x84, 0xbe, 0x58, 0x08, 0x6e, 0x6f, 0x2d, 0x63, 0x61, 0x63, 0x68, 0x65,
])
var request3 = buffer(wrapping: [
0x82, 0x87, 0x85, 0xbf, 0x40, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x6b, 0x65, 0x79, 0x0c, 0x63,
0x75, 0x73, 0x74, 0x6f, 0x6d, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65,
])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
(":authority", "www.example.com"),
])
let headers2 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache")
("cache-control", "no-cache"),
])
let headers3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html"),
(":authority", "www.example.com"),
("custom-key", "custom-value")
("custom-key", "custom-value"),
])
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
@@ -79,29 +88,34 @@ class HPACKCodingTests: XCTestCase {
// HPACK RFC7541 § C.4
// http://httpwg.org/specs/rfc7541.html#request.examples.with.huffman.coding
func testRequestHeadersWithHuffmanCoding() throws {
var request1 = buffer(wrapping: [0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request1 = buffer(wrapping: [
0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
])
var request2 = buffer(wrapping: [0x82, 0x86, 0x84, 0xbe, 0x58, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf])
var request3 = buffer(wrapping: [0x82, 0x87, 0x85, 0xbf, 0x40, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
var request3 = buffer(wrapping: [
0x82, 0x87, 0x85, 0xbf, 0x40, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49,
0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf,
])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
(":authority", "www.example.com"),
])
let headers2 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache")
("cache-control", "no-cache"),
])
let headers3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html"),
(":authority", "www.example.com"),
("custom-key", "custom-value")
("custom-key", "custom-value"),
])
var encoder = HPACKEncoder(allocator: allocator)
@@ -158,9 +172,11 @@ class HPACKCodingTests: XCTestCase {
// cache-control: private
0x58, 0x07, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65,
// date: Mon, 21 Oct 2013 20:13:21 GMT
0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x31, 0x20, 0x47, 0x4d, 0x54,
0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33,
0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x31, 0x20, 0x47, 0x4d, 0x54,
// location: https://www.example.com
0x6e, 0x17, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d
0x6e, 0x17, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
])
var response2 = buffer(wrapping: [
// :status: 307
@@ -170,7 +186,7 @@ class HPACKCodingTests: XCTestCase {
// date: Mon, 21 Oct 2013 20:13:21 GMT
0xc0,
// location: https://www.example.com
0xbf
0xbf,
])
var response3 = buffer(wrapping: [
// :status: 200
@@ -178,26 +194,30 @@ class HPACKCodingTests: XCTestCase {
// cache-control: private
0xc1,
// date: Mon, 21 Oct 2013 20:13:22 GMT
0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33, 0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x32, 0x20, 0x47, 0x4d, 0x54,
0x61, 0x1d, 0x4d, 0x6f, 0x6e, 0x2c, 0x20, 0x32, 0x31, 0x20, 0x4f, 0x63, 0x74, 0x20, 0x32, 0x30, 0x31, 0x33,
0x20, 0x32, 0x30, 0x3a, 0x31, 0x33, 0x3a, 0x32, 0x32, 0x20, 0x47, 0x4d, 0x54,
// location: https://www.example.com
0xc0,
// content-encoding: gzip
0x5a, 0x04, 0x67, 0x7a, 0x69, 0x70,
// set-cookie: foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1
0x77, 0x38, 0x66, 0x6f, 0x6f, 0x3d, 0x41, 0x53, 0x44, 0x4a, 0x4b, 0x48, 0x51, 0x4b, 0x42, 0x5a, 0x58, 0x4f, 0x51, 0x57, 0x45, 0x4f, 0x50, 0x49, 0x55, 0x41, 0x58, 0x51, 0x57, 0x45, 0x4f, 0x49, 0x55, 0x3b, 0x20, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65, 0x3d, 0x33, 0x36, 0x30, 0x30, 0x3b, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x31
0x77, 0x38, 0x66, 0x6f, 0x6f, 0x3d, 0x41, 0x53, 0x44, 0x4a, 0x4b, 0x48, 0x51, 0x4b, 0x42, 0x5a, 0x58, 0x4f,
0x51, 0x57, 0x45, 0x4f, 0x50, 0x49, 0x55, 0x41, 0x58, 0x51, 0x57, 0x45, 0x4f, 0x49, 0x55, 0x3b, 0x20, 0x6d,
0x61, 0x78, 0x2d, 0x61, 0x67, 0x65, 0x3d, 0x33, 0x36, 0x30, 0x30, 0x3b, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x3d, 0x31,
])
let headers1 = HPACKHeaders([
(":status", "302"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
("location", "https://www.example.com"),
])
let headers2 = HPACKHeaders([
(":status", "307"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
("location", "https://www.example.com"),
])
let headers3 = HPACKHeaders([
(":status", "200"),
@@ -205,7 +225,7 @@ class HPACKCodingTests: XCTestCase {
("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
("location", "https://www.example.com"),
("content-encoding", "gzip"),
("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1")
("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
])
var decoder = HPACKDecoder(allocator: ByteBufferAllocator(), maxDynamicTableSize: 256)
@@ -239,26 +259,32 @@ class HPACKCodingTests: XCTestCase {
// http://httpwg.org/specs/rfc7541.html#response.examples.with.huffman.coding
func testResponseHeadersWithHuffmanCoding() throws {
var response1 = buffer(wrapping: [
0x48, 0x82, 0x64, 0x02, 0x58, 0x85, 0xae, 0xc3, 0x77, 0x1a, 0x4b, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff, 0x6e, 0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3
0x48, 0x82, 0x64, 0x02, 0x58, 0x85, 0xae, 0xc3, 0x77, 0x1a, 0x4b, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10,
0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff, 0x6e,
0x91, 0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3,
])
var response2 = buffer(wrapping: [
0x48, 0x83, 0x64, 0x0e, 0xff, 0xc1, 0xc0, 0xbf
0x48, 0x83, 0x64, 0x0e, 0xff, 0xc1, 0xc0, 0xbf,
])
var response3 = buffer(wrapping: [
0x88, 0xc1, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d, 0x1b, 0xff, 0xc0, 0x5a, 0x83, 0x9b, 0xd9, 0xab, 0x77, 0xad, 0x94, 0xe7, 0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed, 0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07
0x88, 0xc1, 0x61, 0x96, 0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b,
0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d, 0x1b, 0xff, 0xc0, 0x5a, 0x83, 0x9b, 0xd9, 0xab, 0x77, 0xad, 0x94, 0xe7,
0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08,
0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed,
0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07,
])
let headers1 = HPACKHeaders([
(":status", "302"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
("location", "https://www.example.com"),
])
let headers2 = HPACKHeaders([
(":status", "307"),
("cache-control", "private"),
("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
("location", "https://www.example.com")
("location", "https://www.example.com"),
])
let headers3 = HPACKHeaders([
(":status", "200"),
@@ -266,7 +292,7 @@ class HPACKCodingTests: XCTestCase {
("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
("location", "https://www.example.com"),
("content-encoding", "gzip"),
("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1")
("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
])
var encoder = HPACKEncoder(allocator: allocator)
@@ -322,14 +348,24 @@ class HPACKCodingTests: XCTestCase {
}
func testNonIndexedRequest() throws {
var request1 = buffer(wrapping: [0x82, 0x86, 0x84, 0x01, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request2 = buffer(wrapping: [0x82, 0x86, 0x84, 0x01, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x0f, 0x09, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf, 0x00, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
var request3 = buffer(wrapping: [0x82, 0x87, 0x85, 0x11, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x10, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
var request1 = buffer(wrapping: [
0x82, 0x86, 0x84, 0x01, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff,
])
var request2 = buffer(wrapping: [
0x82, 0x86, 0x84, 0x01, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x0f,
0x09, 0x86, 0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf, 0x00, 0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f,
0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf,
])
var request3 = buffer(wrapping: [
0x82, 0x87, 0x85, 0x11, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff, 0x10,
0x88, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f, 0x89, 0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4,
0xbf,
])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/")
(":path", "/"),
])
let h1NoIndex = (name: ":authority", value: "www.example.com")
let h2NoIndex = (name: "cache-control", value: "no-cache")
@@ -337,7 +373,7 @@ class HPACKCodingTests: XCTestCase {
let headers3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html")
(":path", "/index.html"),
])
let h3NeverIndex = (name: "custom-key", value: "custom-value")
@@ -371,7 +407,7 @@ class HPACKCodingTests: XCTestCase {
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
(":authority", "www.example.com"),
])
let fullHeaders2 = HPACKHeaders([
(":method", "GET"),
@@ -379,14 +415,14 @@ class HPACKCodingTests: XCTestCase {
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache"),
("custom-key", "custom-value")
("custom-key", "custom-value"),
])
let fullHeaders3 = HPACKHeaders([
(":method", "GET"),
(":scheme", "https"),
(":path", "/index.html"),
(":authority", "www.example.com"),
("custom-key", "custom-value")
("custom-key", "custom-value"),
])
let decoded1 = try decoder.decodeHeaders(from: &request1)
@@ -403,16 +439,28 @@ class HPACKCodingTests: XCTestCase {
}
func testInlineDynamicTableResize() throws {
var request1 = buffer(wrapping: [0x3f, 0x32, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
let request2 = buffer(wrapping: [0x3f, 0x21, 0x3f, 0x32, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request3 = buffer(wrapping: [0x3f, 0xe1, 0x20, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request4 = buffer(wrapping: [0x82, 0x86, 0x3f, 0x32, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
var request1 = buffer(wrapping: [
0x3f, 0x32, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4,
0xff,
])
let request2 = buffer(wrapping: [
0x3f, 0x21, 0x3f, 0x32, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab,
0x90, 0xf4, 0xff,
])
var request3 = buffer(wrapping: [
0x3f, 0xe1, 0x20, 0x82, 0x86, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90,
0xf4, 0xff,
])
var request4 = buffer(wrapping: [
0x82, 0x86, 0x3f, 0x32, 0x84, 0x41, 0x8c, 0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4,
0xff,
])
let headers1 = HPACKHeaders([
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")
(":authority", "www.example.com"),
])
let oddMaxTableSize = 81
@@ -434,12 +482,12 @@ class HPACKCodingTests: XCTestCase {
XCTAssertEqualTuple(headers1[3], try encoder.headerIndexTable.header(at: 62))
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
decoder.maxDynamicTableLength = oddMaxTableSize // not enough to store the value we expect it to eventually store, but enough for the initial run
// not enough to store the value we expect it to eventually store, but enough for the initial run
decoder.maxDynamicTableLength = oddMaxTableSize
let decoded: HPACKHeaders
do {
decoded = try decoder.decodeHeaders(from: &request1)
}
catch {
} catch {
XCTFail("Failed to decode header set containing dynamic-table-resize command: \(error)")
return
}
@@ -449,7 +497,7 @@ class HPACKCodingTests: XCTestCase {
XCTAssertEqualTuple(headers1[3], try decoder.headerTable.header(at: 62))
// Now, ensure some special cases.
request1.moveReaderIndex(to: 0) // make the data available again in our sample buffer
request1.moveReaderIndex(to: 0) // make the data available again in our sample buffer
// 1 - if we try to change the size mid-buffer, it'll throw an error.
try encoder.setDynamicTableSize(4096)
@@ -462,7 +510,7 @@ class HPACKCodingTests: XCTestCase {
XCTAssertNoThrow(try encoder.append(header: ":method", value: "GET"))
XCTAssertNoThrow(try encoder.append(header: ":scheme", value: "http"))
XCTAssertNoThrow(try encoder.append(header: ":path", value: "/"))
XCTAssertThrowsError(try encoder.setDynamicTableSize(oddMaxTableSize)) // should throw
XCTAssertThrowsError(try encoder.setDynamicTableSize(oddMaxTableSize)) // should throw
XCTAssertNoThrow(try encoder.append(header: ":authority", value: "www.example.com"))
// No resize information, but the rest of the block will be there
@@ -477,7 +525,7 @@ class HPACKCodingTests: XCTestCase {
// 2 - We can set multiple sizes, and both the smallest and the latest will be sent.
try encoder.setDynamicTableSize(64)
try encoder.setDynamicTableSize(75)
try encoder.setDynamicTableSize(oddMaxTableSize /* 81 */)
try encoder.setDynamicTableSize(oddMaxTableSize) // 81
try encoder.beginEncoding(allocator: allocator)
XCTAssertNoThrow(try encoder.append(header: ":method", value: "GET"))
@@ -491,34 +539,40 @@ class HPACKCodingTests: XCTestCase {
// NB: current size is 81 bytes.
encoder.headerIndexTable.dynamicTable.clear()
XCTAssertThrowsError(try decoder.decodeHeaders(from: &request3)) {error in
XCTAssertThrowsError(try decoder.decodeHeaders(from: &request3)) { error in
XCTAssertTrue(error is NIOHPACKErrors.InvalidDynamicTableSize)
}
// 4 - Decoder will not accept a table size update unless it appears at the start of a header block.
decoder.headerTable.dynamicTable.clear()
XCTAssertThrowsError(try decoder.decodeHeaders(from: &request4)) {error in
XCTAssertThrowsError(try decoder.decodeHeaders(from: &request4)) { error in
XCTAssertTrue(error is NIOHPACKErrors.IllegalDynamicTableSizeChange)
}
}
func testHPACKHeadersDescription() throws {
let headerList1: [(String, String)] = [(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com")]
let headerList2: [(String, String)] = [(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache")]
let headerList3: [(HPACKIndexing, String, String)] = [(.indexable, ":method", "POST"),
(.indexable, ":scheme", "https"),
(.indexable, ":path", "/send.html"),
(.indexable, ":authority", "www.example.com"),
(.indexable, "custom-key", "custom-value"),
(.nonIndexable, "content-length", "42")]
let headerList1: [(String, String)] = [
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
]
let headerList2: [(String, String)] = [
(":method", "GET"),
(":scheme", "http"),
(":path", "/"),
(":authority", "www.example.com"),
("cache-control", "no-cache"),
]
let headerList3: [(HPACKIndexing, String, String)] = [
(.indexable, ":method", "POST"),
(.indexable, ":scheme", "https"),
(.indexable, ":path", "/send.html"),
(.indexable, ":authority", "www.example.com"),
(.indexable, "custom-key", "custom-value"),
(.nonIndexable, "content-length", "42"),
]
let headers1 = HPACKHeaders(headerList1)
let headers2 = HPACKHeaders(headerList2)
@@ -533,7 +587,7 @@ class HPACKCodingTests: XCTestCase {
XCTAssertEqual(description2, expected2)
let description3 = headers3.description
let expected3 = headerList3.description // already contains indexing where we'd expect
let expected3 = headerList3.description // already contains indexing where we'd expect
XCTAssertEqual(description3, expected3)
}
@@ -546,7 +600,7 @@ class HPACKCodingTests: XCTestCase {
("cache-control", "no-cache"),
("custom-key", "value-1,value-2"),
("set-cookie", "abcdefg,hijklmn,opqrst"),
("custom-key", "value-3")
("custom-key", "value-3"),
])
XCTAssertEqual(headers[":method"], ["GET"])
@@ -567,7 +621,7 @@ class HPACKCodingTests: XCTestCase {
"no-whitespace": "foo,bar",
"sp": " foo , bar ",
"htab": "\tfoo\t,\t\tbar\t\t",
"sp-and-htab": " \t foo \t\t,\t bar\t "
"sp-and-htab": " \t foo \t\t,\t bar\t ",
]
let expected = ["foo", "bar"]
@@ -580,7 +634,7 @@ class HPACKCodingTests: XCTestCase {
func testHPACKHeadersCanonicalFormDropsEmptyStrings() throws {
let headers: HPACKHeaders = [
"no-whitespace": "foo,,bar",
"with-whitespace": "foo, ,bar"
"with-whitespace": "foo, ,bar",
]
let expected = ["foo", "bar"]
XCTAssertEqual(headers[canonicalForm: "no-whitespace"], expected)
@@ -591,8 +645,8 @@ class HPACKCodingTests: XCTestCase {
let shouldNotBeSplitOrStripped = "should, not , be , split, or, stripped"
let headers: HPACKHeaders = ["set-cookie": shouldNotBeSplitOrStripped]
XCTAssertEqual(headers[canonicalForm: "set-cookie"], [shouldNotBeSplitOrStripped])
XCTAssertEqual(headers[canonicalForm: "Set-Cookie"], [shouldNotBeSplitOrStripped])
XCTAssertEqual(headers[canonicalForm: "set-cookie"], [shouldNotBeSplitOrStripped])
XCTAssertEqual(headers[canonicalForm: "Set-Cookie"], [shouldNotBeSplitOrStripped])
}
func testHPACKHeadersFirst() throws {
@@ -600,7 +654,7 @@ class HPACKCodingTests: XCTestCase {
(":method", "GET"),
("foo", "bar"),
("foo", "baz"),
("custom-key", "value-1,value-2")
("custom-key", "value-1,value-2"),
])
XCTAssertEqual(headers.first(name: ":method"), "GET")
@@ -618,7 +672,7 @@ class HPACKCodingTests: XCTestCase {
"cache-control": "no-cache",
"custom-key": "value-1,value-2",
"set-cookie": "abcdefg,hijklmn,opqrst",
"custom-key": "value-3"
"custom-key": "value-3",
]
let headers2 = HPACKHeaders([
@@ -629,7 +683,7 @@ class HPACKCodingTests: XCTestCase {
("cache-control", "no-cache"),
("custom-key", "value-1,value-2"),
("set-cookie", "abcdefg,hijklmn,opqrst"),
("custom-key", "value-3")
("custom-key", "value-3"),
])
XCTAssertEqual(headers, headers2)
@@ -702,7 +756,10 @@ class HPACKCodingTests: XCTestCase {
XCTAssertEqual(decoder.maxHeaderListSize, 16 * 1024)
XCTAssertThrowsError(try decoder.decodeHeaders(from: &request)) { error in
XCTAssertEqual(error as? NIOHPACKErrors.MaxHeaderListSizeViolation, NIOHPACKErrors.MaxHeaderListSizeViolation())
XCTAssertEqual(
error as? NIOHPACKErrors.MaxHeaderListSizeViolation,
NIOHPACKErrors.MaxHeaderListSizeViolation()
)
}
// Decoding a header block that is smaller than the max header list size is fine.
@@ -713,8 +770,10 @@ class HPACKCodingTests: XCTestCase {
}
func testDifferentlyCasedHPACKHeadersAreNotEqual() {
let variants: [HPACKHeaders] = [HPACKHeaders([("foo", "foox")]), HPACKHeaders([("Foo", "foo")]),
HPACKHeaders([("foo", "Foo")]), HPACKHeaders([("Foo", "Foo")])]
let variants: [HPACKHeaders] = [
HPACKHeaders([("foo", "foox")]), HPACKHeaders([("Foo", "foo")]),
HPACKHeaders([("foo", "Foo")]), HPACKHeaders([("Foo", "Foo")]),
]
for v1 in variants.indices {
for v2 in variants.indices {
@@ -734,8 +793,13 @@ class HPACKCodingTests: XCTestCase {
// This will be placed into the header table, and will use an indexed name but a literal value.
let firstHeaders = HPACKHeaders([("date", "Tue, 12 Nov 2019 09:48:43 GMT")])
XCTAssertNoThrow(try encoder.encode(headers: firstHeaders, to: &buffer))
XCTAssertEqual(Array(buffer.readableBytesView),
[0x61, 0x96, 0xdf, 0x69, 0x7e, 0x94, 0x08, 0x94, 0xd2, 0x7e, 0xea, 0x08, 0x01, 0x7d, 0x40, 0x3f, 0x71, 0xa7, 0xae, 0x34, 0xca, 0x98, 0xb4, 0x6f])
XCTAssertEqual(
Array(buffer.readableBytesView),
[
0x61, 0x96, 0xdf, 0x69, 0x7e, 0x94, 0x08, 0x94, 0xd2, 0x7e, 0xea, 0x08, 0x01, 0x7d, 0x40, 0x3f, 0x71,
0xa7, 0xae, 0x34, 0xca, 0x98, 0xb4, 0x6f,
]
)
// Now we're going to try to send that header again. This should be indexed fully, and sent trivially.
buffer.clear()
@@ -749,7 +813,12 @@ class HPACKCodingTests: XCTestCase {
secondHeaders.add(name: "date", value: "Tue, 12 Nov 2019 09:48:43 GMT", indexing: .nonIndexable)
buffer.clear()
XCTAssertNoThrow(try encoder.encode(headers: secondHeaders, to: &buffer))
XCTAssertEqual(Array(buffer.readableBytesView),
[0x0F, 0x12, 0x96, 0xdf, 0x69, 0x7e, 0x94, 0x08, 0x94, 0xd2, 0x7e, 0xea, 0x08, 0x01, 0x7d, 0x40, 0x3f, 0x71, 0xa7, 0xae, 0x34, 0xca, 0x98, 0xb4, 0x6f])
XCTAssertEqual(
Array(buffer.readableBytesView),
[
0x0F, 0x12, 0x96, 0xdf, 0x69, 0x7e, 0x94, 0x08, 0x94, 0xd2, 0x7e, 0xea, 0x08, 0x01, 0x7d, 0x40, 0x3f,
0x71, 0xa7, 0xae, 0x34, 0xca, 0x98, 0xb4, 0x6f,
]
)
}
}
+9 -9
View File
@@ -12,10 +12,10 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import NIOHPACK
import NIOHTTP1
import XCTest
final class HPACKHeadersTests: XCTestCase {
func testHPACKHeadersAreHashable() throws {
@@ -84,7 +84,7 @@ final class HPACKHeadersTests: XCTestCase {
"but-not-me": "",
"keep-alive": "remove-me",
"proxy-connection": "me too",
"transfer-encoding": "me three"
"transfer-encoding": "me three",
]
let normalized = HPACKHeaders(httpHeaders: httpHeaders, normalizeHTTPHeaders: true)
@@ -106,7 +106,7 @@ final class HPACKHeadersTests: XCTestCase {
// Add a bunch of connection headers to remove. We add a large number because the
// implementation of the normalizing init branches on the number of connection header
// values.
for i in 0 ..< 512 {
for i in 0..<512 {
let toRemove = "value-\(i)"
httpHeaders.add(name: "connection", value: toRemove)
httpHeaders.add(name: toRemove, value: "")
@@ -127,7 +127,7 @@ final class HPACKHeadersTests: XCTestCase {
"foo": "baz",
"foo": "bar,baz",
"foo": " bar ,baz ,,",
"bar": "foo"
"bar": "foo",
]
let values = headers.values(forHeader: "foo", canonicalForm: false)
@@ -137,7 +137,7 @@ final class HPACKHeadersTests: XCTestCase {
XCTAssertEqual(iterator.next(), "bar,baz")
XCTAssertEqual(iterator.next(), " bar ,baz ,,")
XCTAssertNil(iterator.next())
XCTAssertNil(iterator.next()) // Still nil
XCTAssertNil(iterator.next()) // Still nil
}
func testValuesSequenceInCanonicalForm() {
@@ -147,7 +147,7 @@ final class HPACKHeadersTests: XCTestCase {
"foo": "baz",
"foo": "bar,baz",
"foo": " bar ,baz ,,",
"bar": "foo"
"bar": "foo",
]
let values = headers.values(forHeader: "foo", canonicalForm: true)
@@ -159,13 +159,13 @@ final class HPACKHeadersTests: XCTestCase {
XCTAssertEqual(iterator.next(), "bar")
XCTAssertEqual(iterator.next(), "baz")
XCTAssertNil(iterator.next())
XCTAssertNil(iterator.next()) // Still nil
XCTAssertNil(iterator.next()) // Still nil
}
func testValuesSequenceIgnoresCanonicalFormForSetCookie() {
let expected = [
"haha,you,wont,split,me,im,set-cookie",
"but we should still get multiple values"
"but we should still get multiple values",
]
var headers = HPACKHeaders()
@@ -188,7 +188,7 @@ final class HPACKHeadersTests: XCTestCase {
"foo": "baz",
"foo": "bar,baz",
"foo": " bar ,baz ,,",
"bar": "foo"
"bar": "foo",
]
let originalCapacity = original.capacity
+78 -68
View File
@@ -12,11 +12,12 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import XCTest
@testable import NIOHPACK
class HPACKIntegrationTests : XCTestCase {
class HPACKIntegrationTests: XCTestCase {
private let hpackTestsURL: URL = {
let myURL = URL(fileURLWithPath: #filePath, isDirectory: false)
let result: URL
@@ -28,47 +29,53 @@ class HPACKIntegrationTests : XCTestCase {
}
return result
}()
private enum TestType : String {
private enum TestType: String {
case encoding = "raw-data"
case swiftNIOPlain = "swift-nio-hpack-plain-text"
case swiftNIOHuffman = "swift-nio-hpack-huffman"
case node = "node-http2-hpack"
case python = "python-hpack"
case go = "go-hpack"
case nghttp2
case nghttp2ChangeTableSize = "nghttp2-change-table-size"
case nghttp2LargeTables = "nghttp2-16384-4096"
case haskellStatic = "haskell-http2-static"
case haskellStaticHuffman = "haskell-http2-static-huffman"
case haskellNaive = "haskell-http2-naive"
case haskellNaiveHuffman = "haskell-http2-naive-huffman"
case haskellLinear = "haskell-http2-linear"
case haskellLinearHuffman = "haskell-http2-linear-huffman"
static let allCases: [TestType] = [
.encoding, .swiftNIOPlain, .swiftNIOHuffman, .node, .python, .go,
.nghttp2, .nghttp2ChangeTableSize, .nghttp2LargeTables,
.haskellStatic, .haskellStaticHuffman, .haskellNaive, .haskellNaiveHuffman,
.haskellLinear, .haskellLinearHuffman
.haskellLinear, .haskellLinearHuffman,
]
}
private func getSourceURL(for type: TestType) -> URL {
return self.hpackTestsURL.appendingPathComponent(type.rawValue, isDirectory: true).absoluteURL
self.hpackTestsURL.appendingPathComponent(type.rawValue, isDirectory: true).absoluteURL
}
private func loadStories(for test: TestType) -> [HPACKStory] {
let url = self.getSourceURL(for: test)
guard let contents = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []).sorted(by: {$0.lastPathComponent < $1.lastPathComponent}) else {
guard
let contents = try? FileManager.default.contentsOfDirectory(
at: url,
includingPropertiesForKeys: nil,
options: []
).sorted(by: { $0.lastPathComponent < $1.lastPathComponent })
else {
return []
}
let decoder = JSONDecoder()
do {
var stories = try contents.compactMap { url -> HPACKStory? in
// for some reason using filter({ $0.pathExtension == "json" }) throws the compiler type-checker for a loop
@@ -76,7 +83,7 @@ class HPACKIntegrationTests : XCTestCase {
let data = try Data(contentsOf: url, options: [.uncached, .mappedIfSafe])
return try decoder.decode(HPACKStory.self, from: data)
}
// ensure there are valid non-zero sequence numbers
// to ensure we modify these value types in-place we apparently need to use collection subscripting for all accesses
// it seems that if there are sequence numbers on any cases they're on all cases; we can optimize for that.
@@ -91,7 +98,7 @@ class HPACKIntegrationTests : XCTestCase {
return []
}
}
private func runEncodingTests(withOutput type: TestType) {
let huffmanEncoded = type == .swiftNIOHuffman
let stories = loadStories(for: .encoding)
@@ -99,47 +106,47 @@ class HPACKIntegrationTests : XCTestCase {
// we don't have the data, so don't go failing any tests
return
}
let outputDir = getSourceURL(for: type)
if FileManager.default.fileExists(atPath: outputDir.path) == false {
try! FileManager.default.createDirectory(at: outputDir, withIntermediateDirectories: true, attributes: nil)
}
var outputs: [HPACKStory] = []
let start = Date()
for (idx, story) in stories.enumerated() {
let encoded = runEncodeStory(story, idx, huffmanEncoded: huffmanEncoded)
outputs.append(encoded)
}
let end = Date()
print("Encoding took \(String(format: "%.02f", end.timeIntervalSince(start))) seconds.")
for (idx, story) in outputs.enumerated() {
writeOutputStory(story, at: idx, to: outputDir)
}
}
// funky names to ensure the encoder tests run before the decoder tests.
func testAAEncoderWithoutHuffmanCoding() {
runEncodingTests(withOutput: .swiftNIOPlain)
}
// funky names to ensure the encoder tests run before the decoder tests.
func testABEncoderWithHuffmanCoding() {
runEncodingTests(withOutput: .swiftNIOHuffman)
}
func testDecoder() {
var testTimings: [TestType : TimeInterval] = [:]
var testTimings: [TestType: TimeInterval] = [:]
for test in TestType.allCases where test != .encoding {
let duration = _testDecoder(for: test)
testTimings[test] = duration
}
print("Test timing metrics:")
for (test, duration) in testTimings {
let testName = test.rawValue
@@ -147,11 +154,11 @@ class HPACKIntegrationTests : XCTestCase {
if firstPart.count < 40 {
firstPart.append(String(repeating: " ", count: 40 - firstPart.count))
}
print("\(firstPart) : \(String(format: "%.02f", duration)) seconds.")
}
}
private func _testDecoder(for test: TestType) -> TimeInterval {
let loadStart = Date()
print("Loading \(test.rawValue)...", terminator: "")
@@ -163,23 +170,23 @@ class HPACKIntegrationTests : XCTestCase {
return 0
}
print(" done. Load time = \(String(format: "%.02f", loadTime)) seconds.")
// description comes from first test case
if let description = stories.first!.description {
print(description)
}
let start = Date()
for (idx, story) in stories.enumerated() {
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
for testCase in story.cases {
do {
if let size = testCase.headerTableSize {
decoder.maxDynamicTableLength = size
}
guard var bytes = testCase.wire else {
XCTFail("Decoder story case \(testCase.seqno) has no wire data to decode!")
return Date().timeIntervalSince(start)
@@ -192,17 +199,18 @@ class HPACKIntegrationTests : XCTestCase {
}
}
}
return Date().timeIntervalSince(start)
}
private func writeOutputStory(_ story: HPACKStory, at index: Int, to directory: URL) {
let outURL = directory
let outURL =
directory
.appendingPathComponent(String(format: "story_%02d", index), isDirectory: false)
.appendingPathExtension("json")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
do {
let data = try encoder.encode(story)
try data.write(to: outURL, options: [.atomic])
@@ -210,35 +218,37 @@ class HPACKIntegrationTests : XCTestCase {
print("Error writing encoded test case: \(error)")
}
}
private func runEncodeStory(_ story: HPACKStory, _ index: Int, huffmanEncoded: Bool = true) -> HPACKStory {
// do we need to care about the context?
var encoder = HPACKEncoder(allocator: ByteBufferAllocator(), useHuffmanEncoding: huffmanEncoded)
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
var result = story
result.cases.removeAll()
if huffmanEncoded {
result.description = "Encoded using the HPACK implementation of swift-nio-http2, with static table, dynamic table, and huffman encoded strings: <https://github.com/apple/swift-nio-http2>"
result.description =
"Encoded using the HPACK implementation of swift-nio-http2, with static table, dynamic table, and huffman encoded strings: <https://github.com/apple/swift-nio-http2>"
} else {
result.description = "Encoded using the HPACK implementation of swift-nio-http2, with static table, dynamic table, and plain-text encoded strings: <https://github.com/apple/swift-nio-http2>"
result.description =
"Encoded using the HPACK implementation of swift-nio-http2, with static table, dynamic table, and plain-text encoded strings: <https://github.com/apple/swift-nio-http2>"
}
for storyCase in story.cases {
do {
if let tableSize = storyCase.headerTableSize {
try encoder.setDynamicTableSize(tableSize)
decoder.maxDynamicTableLength = tableSize
}
try encoder.beginEncoding(allocator: ByteBufferAllocator())
try encoder.append(headers: storyCase.headers)
var outputCase = storyCase
var encoded = try encoder.endEncoding()
outputCase.wire = encoded
result.cases.append(outputCase)
// now try to decode it
let decoded = try decoder.decodeHeaders(from: &encoded)
XCTAssertEqual(storyCase.headers, decoded)
@@ -247,84 +257,84 @@ class HPACKIntegrationTests : XCTestCase {
XCTFail("Failure in story \(index), case \(storyCase.seqno) - \(error)")
}
}
// the output story now has all the wire values filled in
return result
}
}
struct HPACKStory : Codable {
struct HPACKStory: Codable {
var context: HPACKStoryContext?
var description: String?
var cases: [HPACKStoryCase]
}
enum HPACKStoryContext : String, Codable {
enum HPACKStoryContext: String, Codable {
case request
case response
}
struct HPACKStoryCase : Codable {
struct HPACKStoryCase: Codable {
var seqno: Int
var headerTableSize: Int?
var wire: ByteBuffer?
var headers: HPACKHeaders
private enum Keys : String, CodingKey {
private enum Keys: String, CodingKey {
case seqno
case headerTableSize = "header_table_size"
case wire
case headers
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
self.seqno = try container.decodeIfPresent(Int.self, forKey: .seqno) ?? 0
self.headerTableSize = try container.decodeIfPresent(Int.self, forKey: .headerTableSize)
if let wireString = try container.decodeIfPresent(String.self, forKey: .wire) {
self.wire = decodeHexData(from: wireString)
} else {
self.wire = nil
}
let rawHeaders = try container.decode([[String : String]].self, forKey: .headers)
let rawHeaders = try container.decode([[String: String]].self, forKey: .headers)
let pairs = rawHeaders.map { ($0.first!.key, $0.first!.value) }
self.headers = HPACKHeaders(pairs)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(self.seqno, forKey: .seqno)
try container.encode(self.headerTableSize, forKey: .headerTableSize)
if let bytes = self.wire {
try container.encode(encodeHex(data: bytes), forKey: .wire)
}
let rawHeaders = self.headers.map { [$0.0 : $0.1] }
let rawHeaders = self.headers.map { [$0.0: $0.1] }
try container.encode(rawHeaders, forKey: .headers)
}
}
fileprivate func decodeHexData(from string: String) -> ByteBuffer {
private func decodeHexData(from string: String) -> ByteBuffer {
var bytes: [UInt8] = []
var idx = string.startIndex
repeat {
let byteStr = string[idx...string.index(after: idx)]
bytes.append(UInt8(byteStr, radix: 16)!)
idx = string.index(idx, offsetBy: 2)
} while string.distance(from: idx, to: string.endIndex) > 1
var buf = ByteBufferAllocator().buffer(capacity: bytes.count)
_ = buf.writeBytes(bytes)
return buf
}
fileprivate func encodeHex(data: ByteBuffer) -> String {
private func encodeHex(data: ByteBuffer) -> String {
var result = ""
for byte in data.readableBytesView {
let str = String(byte, radix: 16)
+24 -22
View File
@@ -12,9 +12,9 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import NIOHPACK
import XCTest
class HPACKRegressionTests: XCTestCase {
@@ -39,44 +39,46 @@ class HPACKRegressionTests: XCTestCase {
XCTAssertEqual(decoded, HPACKHeaders([(":status", "200"), ("date", "Tue, 16 Apr 2019 08:43:34 GMT")]))
}
func testHPACKDecoderForbidsZeroLengthFieldNames() throws {
// This test validates the fix for CVE-XXXX: we reject header fields without a header field name.
let request1 = buffer(wrapping: [
0x00, // Literal Header Field without Indexing - New Name
0x00, // Empty header field name, no huffman.
0x01, 0x42 // Header field value one character long, 'B', no huffman
0x00, // Literal Header Field without Indexing - New Name
0x00, // Empty header field name, no huffman.
0x01, 0x42, // Header field value one character long, 'B', no huffman
])
let request2 = buffer(wrapping: [
0x00, // Literal Header Field without Indexing - New Name
0x80, // Empty header field name with huffman.
0x01, 0x42 // Header field value one character long, 'B', no huffman.
0x00, // Literal Header Field without Indexing - New Name
0x80, // Empty header field name with huffman.
0x01, 0x42, // Header field value one character long, 'B', no huffman.
])
let request3 = buffer(wrapping: [
0x40, // Literal Header Field with Incremental Indexing - New Name
0x00, // Empty header field name, no huffman.
0x01, 0x42 // Header field value one character long, 'B', no huffman
0x40, // Literal Header Field with Incremental Indexing - New Name
0x00, // Empty header field name, no huffman.
0x01, 0x42, // Header field value one character long, 'B', no huffman
])
let request4 = buffer(wrapping: [
0x40, // Literal Header Field with Incremental Indexing - New Name
0x80, // Empty header field name with huffman.
0x01, 0x42 // Header field value one character long, 'B', no huffman.
0x40, // Literal Header Field with Incremental Indexing - New Name
0x80, // Empty header field name with huffman.
0x01, 0x42, // Header field value one character long, 'B', no huffman.
])
let request5 = buffer(wrapping: [
0x10, // Literal Header Field Never Indexed - New Name
0x00, // Empty header field name, no huffman.
0x01, 0x42 // Header field value one character long, 'B', no huffman
])
0x10, // Literal Header Field Never Indexed - New Name
0x00, // Empty header field name, no huffman.
0x01, 0x42, // Header field value one character long, 'B', no huffman
])
let request6 = buffer(wrapping: [
0x10, // Literal Header Field Never Indexed - New Name
0x80, // Empty header field name with huffman.
0x01, 0x42 // Header field value one character long, 'B', no huffman.
0x10, // Literal Header Field Never Indexed - New Name
0x80, // Empty header field name with huffman.
0x01, 0x42, // Header field value one character long, 'B', no huffman.
])
for var request in [request1, request2, request3, request4, request5, request6] {
var decoder = HPACKDecoder(allocator: ByteBufferAllocator())
XCTAssertThrowsError(try decoder.decodeHeaders(from: &request)) { error in
XCTAssertEqual(error as? NIOHPACKErrors.EmptyLiteralHeaderFieldName, NIOHPACKErrors.EmptyLiteralHeaderFieldName())
XCTAssertEqual(
error as? NIOHPACKErrors.EmptyLiteralHeaderFieldName,
NIOHPACKErrors.EmptyLiteralHeaderFieldName()
)
}
}
}
+38 -31
View File
@@ -12,14 +12,18 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import XCTest
@testable import NIOHPACK
func XCTAssertEqualTuple<T1: Equatable, T2: Equatable>(_ expression1: @autoclosure () throws -> (T1, T2)?,
_ expression2: @autoclosure () throws -> (T1, T2)?,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line) {
func XCTAssertEqualTuple<T1: Equatable, T2: Equatable>(
_ expression1: @autoclosure () throws -> (T1, T2)?,
_ expression2: @autoclosure () throws -> (T1, T2)?,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line
) {
let ex1: (T1, T2)?
let ex2: (T1, T2)?
do {
@@ -29,7 +33,7 @@ func XCTAssertEqualTuple<T1: Equatable, T2: Equatable>(_ expression1: @autoclosu
XCTFail("Unexpected exception: \(error) \(message())", file: (file), line: line)
return
}
let left1 = ex1?.0
let right1 = ex2?.0
let left2 = ex1?.1
@@ -43,7 +47,7 @@ class HeaderTableTests: XCTestCase {
func testStaticHeaderTable() {
let table = IndexedHeaderTable(allocator: ByteBufferAllocator())
// headers with matching values
XCTAssertEqualTuple((2, true), table.firstHeaderMatch(for: ":method", value: "GET")!)
XCTAssertEqualTuple((3, true), table.firstHeaderMatch(for: ":method", value: "POST")!)
@@ -53,99 +57,102 @@ class HeaderTableTests: XCTestCase {
XCTAssertEqualTuple((13, true), table.firstHeaderMatch(for: ":status", value: "404")!)
XCTAssertEqualTuple((14, true), table.firstHeaderMatch(for: ":status", value: "500")!)
XCTAssertEqualTuple((16, true), table.firstHeaderMatch(for: "accept-encoding", value: "gzip, deflate")!)
// headers with no values in the table
XCTAssertEqualTuple((15, false), table.firstHeaderMatch(for: "accept-charset", value: "any")!)
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "private")!)
// header-only matches for table entries with a different value
XCTAssertEqualTuple((8, false), table.firstHeaderMatch(for: ":status", value: "501")!)
XCTAssertEqualTuple((4, false), table.firstHeaderMatch(for: ":path", value: "/test/path.html")!)
XCTAssertEqualTuple((2, false), table.firstHeaderMatch(for: ":method", value: "CONNECT")!)
// things that aren't in the table at all
XCTAssertNil(table.firstHeaderMatch(for: "non-existent-key", value: "non-existent-value"))
}
func testDynamicTableInsertion() {
// NB: I'm using the overall table class to verify the expected indices of dynamic table items.
var table = IndexedHeaderTable(allocator: ByteBufferAllocator(), maxDynamicTableSize: 1024)
XCTAssertEqual(table.dynamicTableLength, 0)
XCTAssertNoThrow(try table.add(headerNamed: ":authority", value: "www.example.com"))
XCTAssertEqual(table.dynamicTableLength, 57)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.something-else.com")!)
XCTAssertNoThrow(try table.add(headerNamed: "cache-control", value: "no-cache"))
XCTAssertEqual(table.dynamicTableLength, 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
// custom key not yet in the table, should return nil
XCTAssertNil(table.firstHeaderMatch(for: "custom-key", value: "custom-value"))
XCTAssertNoThrow(try table.add(headerNamed: "custom-key", value: "custom-value"))
XCTAssertEqual(table.dynamicTableLength, 164)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((64, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
// should evict the first-inserted value (:authority = www.example.com)
table.dynamicTableAllowedLength = 128
XCTAssertEqual(table.dynamicTableLength, 164 - 57)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!) // will find the header name in static table
// will find the header name in static table
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
table.dynamicTableAllowedLength = 64
XCTAssertEqual(table.dynamicTableLength, 164 - 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!) // will find the header name in static table
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!) // will find the header name in static table
table.dynamicTableAllowedLength = 164 - 110 // should cause no evictions
// will find the header name in static table
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
// will find the header name in static table
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
table.dynamicTableAllowedLength = 164 - 110 // should cause no evictions
XCTAssertEqual(table.dynamicTableLength, 164 - 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
// get code coverage for dynamic table search with no value to match
// Note that the resulting index is an index into the dynamic table only, so we
// have to modify it to check that it's what we expect.
if let found = table.dynamicTable.findExistingHeader(named: "custom-key", value: nil) {
XCTAssertEqualTuple((62 - StaticHeaderTable.count, false), found)
}
// evict final entry
table.dynamicTableAllowedLength = table.dynamicTableLength - 1
XCTAssertEqual(table.dynamicTableLength, 0)
XCTAssertNil(table.firstHeaderMatch(for: "custom-key", value: "custom-value"))
}
func testHeaderDump() throws {
var table = IndexedHeaderTable(allocator: ByteBufferAllocator())
let description = table.dumpHeaders()
// construct what we expect
var expected = StaticHeaderTable.enumerated().reduce("") {
$0 + "\($1.0) - \($1.1.0) : \($1.1.1)\n"
}
// there will be a newline at the end, followed by no data for the dynamic table
expected += "\n"
XCTAssertEqual(description, expected)
// add an item to the dynamic table
try table.add(headerNamed: "custom-key", value: "custom-value")
expected += "\(StaticHeaderTable.count) - custom-key : custom-value\n"
let description2 = table.dumpHeaders()
XCTAssertEqual(description2, expected)
}
func testHeaderDescription() throws {
let table = IndexedHeaderTable(allocator: ByteBufferAllocator())
let staticDescription = table.staticTable.description
+88 -27
View File
@@ -12,78 +12,139 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import Foundation
import NIOCore
import NIOFoundationCompat
import Foundation
import XCTest
@testable import NIOHPACK
class HuffmanCodingTests: XCTestCase {
var scratchBuffer: ByteBuffer = ByteBufferAllocator().buffer(capacity: 4096)
// MARK: - Helper Methods
func assertEqualContents(_ buffer: ByteBuffer, _ array: [UInt8], file: StaticString = #filePath, line: UInt = #line) {
XCTAssertEqual(buffer.readableBytes, array.count, "Buffer and array are different sizes", file: (file), line: line)
func assertEqualContents(_ buffer: ByteBuffer, _ array: [UInt8], file: StaticString = #filePath, line: UInt = #line)
{
XCTAssertEqual(
buffer.readableBytes,
array.count,
"Buffer and array are different sizes",
file: (file),
line: line
)
buffer.withUnsafeReadableBytes { bufPtr in
array.withUnsafeBytes { arrayPtr in
XCTAssertEqual(memcmp(bufPtr.baseAddress!, arrayPtr.baseAddress!, arrayPtr.count), 0, "Buffer contents don't match", file: (file), line: line)
XCTAssertEqual(
memcmp(bufPtr.baseAddress!, arrayPtr.baseAddress!, arrayPtr.count),
0,
"Buffer contents don't match",
file: (file),
line: line
)
}
}
}
func verifyHuffmanCoding(_ string: String, _ bytes: [UInt8], file: StaticString = #filePath, line: UInt = #line) throws {
func verifyHuffmanCoding(
_ string: String,
_ bytes: [UInt8],
file: StaticString = #filePath,
line: UInt = #line
) throws {
self.scratchBuffer.clear()
let utf8 = string.utf8
let numBytes = self.scratchBuffer.writeHuffmanEncoded(bytes: utf8)
XCTAssertEqual(numBytes, bytes.count, "Wrong length encoding '\(string)'", file: (file), line: line)
assertEqualContents(self.scratchBuffer, bytes, file: (file), line: line)
let decoded = try scratchBuffer.getHuffmanEncodedString(at: self.scratchBuffer.readerIndex, length: self.scratchBuffer.readableBytes)
let decoded = try scratchBuffer.getHuffmanEncodedString(
at: self.scratchBuffer.readerIndex,
length: self.scratchBuffer.readableBytes
)
XCTAssertEqual(decoded, string, "Failed to decode '\(string)'", file: (file), line: line)
}
func testBasicCoding() throws {
// all these values come from http://httpwg.org/specs/rfc7541.html#request.examples.with.huffman.coding
try verifyHuffmanCoding("www.example.com", [0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff])
try verifyHuffmanCoding(
"www.example.com",
[0xf1, 0xe3, 0xc2, 0xe5, 0xf2, 0x3a, 0x6b, 0xa0, 0xab, 0x90, 0xf4, 0xff]
)
try verifyHuffmanCoding("no-cache", [0xa8, 0xeb, 0x10, 0x64, 0x9c, 0xbf])
try verifyHuffmanCoding("custom-key", [0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xa9, 0x7d, 0x7f])
try verifyHuffmanCoding("custom-value", [0x25, 0xa8, 0x49, 0xe9, 0x5b, 0xb8, 0xe8, 0xb4, 0xbf])
// these come from http://httpwg.org/specs/rfc7541.html#response.examples.with.huffman.coding
try verifyHuffmanCoding("302", [0x64, 0x02])
try verifyHuffmanCoding("private", [0xae, 0xc3, 0x77, 0x1a, 0x4b])
try verifyHuffmanCoding("Mon, 21 Oct 2013 20:13:21 GMT", [0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x82, 0xa6, 0x2d, 0x1b, 0xff])
try verifyHuffmanCoding("https://www.example.com", [0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3])
try verifyHuffmanCoding(
"Mon, 21 Oct 2013 20:13:21 GMT",
[
0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0,
0x82, 0xa6, 0x2d, 0x1b, 0xff,
]
)
try verifyHuffmanCoding(
"https://www.example.com",
[0x9d, 0x29, 0xad, 0x17, 0x18, 0x63, 0xc7, 0x8f, 0x0b, 0x97, 0xc8, 0xe9, 0xae, 0x82, 0xae, 0x43, 0xd3]
)
try verifyHuffmanCoding("307", [0x64, 0x0e, 0xff])
try verifyHuffmanCoding("Mon, 21 Oct 2013 20:13:22 GMT", [0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0, 0x84, 0xa6, 0x2d, 0x1b, 0xff])
try verifyHuffmanCoding(
"Mon, 21 Oct 2013 20:13:22 GMT",
[
0xd0, 0x7a, 0xbe, 0x94, 0x10, 0x54, 0xd4, 0x44, 0xa8, 0x20, 0x05, 0x95, 0x04, 0x0b, 0x81, 0x66, 0xe0,
0x84, 0xa6, 0x2d, 0x1b, 0xff,
]
)
try verifyHuffmanCoding("gzip", [0x9b, 0xd9, 0xab])
try verifyHuffmanCoding("foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1", [0x94, 0xe7, 0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 0x39, 0x60, 0xd5, 0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60, 0x65, 0xc0, 0x03, 0xed, 0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07])
try verifyHuffmanCoding(
"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
[
0x94, 0xe7, 0x82, 0x1d, 0xd7, 0xf2, 0xe6, 0xc7, 0xb3, 0x35, 0xdf, 0xdf, 0xcd, 0x5b, 0x39, 0x60, 0xd5,
0xaf, 0x27, 0x08, 0x7f, 0x36, 0x72, 0xc1, 0xab, 0x27, 0x0f, 0xb5, 0x29, 0x1f, 0x95, 0x87, 0x31, 0x60,
0x65, 0xc0, 0x03, 0xed, 0x4e, 0xe5, 0xb1, 0x06, 0x3d, 0x50, 0x07,
]
)
}
func testComplexCoding() throws {
try verifyHuffmanCoding("鯖審", [0xff, 0xff, 0xaf, 0xff, 0xff, 0x67, 0xff, 0xfe, 0x2f, 0xff, 0xf3, 0xff, 0xff, 0xec, 0xff, 0xff, 0x77])
let text1 = "Hello, world. I am a header value; I have Teh Texts. I am going on for quite a long time because I want to ensure that the encoded data buffer needs to be expanded to test out that code. I'll try some meta-characters too: \r\t\n ought to do it, no?"
guard let encoded1Data = Data(base64Encoded: "xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/Pw==") else {
try verifyHuffmanCoding(
"鯖審",
[0xff, 0xff, 0xaf, 0xff, 0xff, 0x67, 0xff, 0xfe, 0x2f, 0xff, 0xf3, 0xff, 0xff, 0xec, 0xff, 0xff, 0x77]
)
let text1 =
"Hello, world. I am a header value; I have Teh Texts. I am going on for quite a long time because I want to ensure that the encoded data buffer needs to be expanded to test out that code. I'll try some meta-characters too: \r\t\n ought to do it, no?"
guard
let encoded1Data = Data(
base64Encoded:
"xlooP9KeD2USLqZFB0qDUnKOQtincdFpftTIpOPuVTeWdTeXylC6mRQdKkxzVTKHqUlPYp2tMkqg1KD1TKJNSVSMpB2oKpkU8DqSok6hakW2FUTONKiZyqFqIeQsikg0jUjtllLYpUUsiFEnUjKoXzWOqQsiiTqJKhKh7UqJnGlQh5CrqZP9UUKJs9KIPSVSkqRrEnHYMiS2IUSc9xT////3//+r////xQ9s06VEnUkOoZP0pUf/Pw=="
)
else {
XCTFail("Unable to import base-64 result data")
return
}
// oh if only Data could conform to ContiguousCollection...
try verifyHuffmanCoding(text1, Array(encoded1Data))
// The encoder has a 256-byte encoding buffer to start with, and we want to overflow that to make it expand automatically
// We also want to include some > 24-bit codes to test all the conditions.
// So: here's a goodly-sized chunk of UTF-8 text. It won't compress worth a damn.
let text2 = "午セイ谷高ぐふあト食71入ツエヘナ津県を類及オモ曜一購ごきわ致掲ぎぐず敗文輪へけり鯖審ヘ塊米卸呪おぴ。"
guard let encoded2Data = Data(base64Encoded: "//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//+z//93//zf/8///r///X//3f//z//+f//7///2///N//z//+9//5v/+j//p//9H//5f//Z//+n//q//5v//q//8X//8P//N//6X//93//m//9L//73//m//5///v///V//9n//9v//o///r//+D//zf/+l///X//5v//S///s//83//pf/+x//6P//w///j//9H//4v//s///V///D//3v//N//6X//V//5v//S///t//83//P//9v//6///2f//i//8///7P//d//83//R//+T//z//93//7f//8///D//4v//P//97//q//8///9v//vf/+b//0v//t//+b//0v//2//+b//m//5/") else {
guard
let encoded2Data = Data(
base64Encoded:
"//8///3v//W//83//P//8n//m//5///c//+r//wf//v//+v///D//8n//m//9L//+z//zf/+l//8P//m//9L//n//83//R//63//1//+3///e6H//z//+p//+j//zf/9H//p//+b//n///V//5v/+j//8n//m//6P//t//+j//9v//6P//5//+z//93//zf/8///r///X//3f//z//+f//7///2///N//z//+9//5v/+j//p//9H//5f//Z//+n//q//5v//q//8X//8P//N//6X//93//m//9L//73//m//5///v///V//9n//9v//o///r//+D//zf/+l///X//5v//S///s//83//pf/+x//6P//w///j//9H//4v//s///V///D//3v//N//6X//V//5v//S///t//83//P//9v//6///2f//i//8///7P//d//83//R//+T//z//93//7f//8///D//4v//P//97//q//8///9v//vf/+b//0v//t//+b//0v//2//+b//m//5/"
)
else {
XCTFail("Unable to import base-64 result data")
return
}
try verifyHuffmanCoding(text2, Array(encoded2Data))
}
}
+60 -38
View File
@@ -12,16 +12,17 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import XCTest
@testable import NIOHPACK
class IntegerCodingTests : XCTestCase {
class IntegerCodingTests: XCTestCase {
var scratchBuffer = ByteBufferAllocator().buffer(capacity: 11)
// MARK: - Array-based helpers
private func encodeIntegerToArray(_ value: UInt64, prefix: Int) -> [UInt8] {
var data = [UInt8]()
scratchBuffer.clear()
@@ -29,40 +30,40 @@ class IntegerCodingTests : XCTestCase {
data.append(contentsOf: scratchBuffer.viewBytes(at: 0, length: len)!)
return data
}
private func decodeInteger(from array: [UInt8], prefix: Int) throws -> Int {
scratchBuffer.clear()
scratchBuffer.writeBytes(array)
let result = try NIOHPACK.decodeInteger(from: scratchBuffer.readableBytesView, prefix: prefix)
return result.value
}
// MARK: - Tests
func testIntegerEncoding() {
// values from the standard: http://httpwg.org/specs/rfc7541.html#integer.representation.examples
var data = encodeIntegerToArray(10, prefix: 5) // 0000 1010
XCTAssertEqual(data.count, 1)
XCTAssertEqual(data[0], 0b00001010)
data = encodeIntegerToArray(1337, prefix: 5) // 0000 0101 0011 1001
data = encodeIntegerToArray(1337, prefix: 5) // 0000 0101 0011 1001
XCTAssertEqual(data.count, 3)
// prefix bits = 31 = 0001 1111, 1337 - 31 = 1306 = 0101 0001 1010 -> x0001010 x0011010
XCTAssertEqual(data, [31, 154, 10]) // 00011111 , 10011010 , 00001010
XCTAssertEqual(data, [31, 154, 10]) // 00011111 , 10011010 , 00001010
// prefix 8 == use the whole first octet
data = encodeIntegerToArray(42, prefix: 8) // 0010 1010
data = encodeIntegerToArray(42, prefix: 8) // 0010 1010
XCTAssertEqual(data.count, 1)
XCTAssertEqual(data[0], 42)
data = encodeIntegerToArray(256, prefix: 8) // 0001 0000 0000 -> 1111 1111 + 0000 0001
data = encodeIntegerToArray(256, prefix: 8) // 0001 0000 0000 -> 1111 1111 + 0000 0001
XCTAssertEqual(data.count, 2)
XCTAssertEqual(data, [255, 1])
// very large value, few bits set, 8-bit prefix
data = encodeIntegerToArray(17 << 57, prefix: 8) // 00010001 << 57 or 2449958197289549824
data = encodeIntegerToArray(17 << 57, prefix: 8) // 00010001 << 57 or 2449958197289549824
// calculations:
// subtract prefix:
// 2449958197289549824 - 255 = 2449958197289549569 or 0010 0001 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0000 0001
@@ -74,10 +75,10 @@ class IntegerCodingTests : XCTestCase {
// 10000001 11111110 11111111 11111111 11111111 11111111 11111111 11111111 00100001
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, [255, 129, 254, 255, 255, 255, 255, 255, 255, 33])
// same value, 1-bit prefix:
data = encodeIntegerToArray(17 << 57, prefix: 1)
// calculations:
// subtract prefix:
// 2449958197289549824 - 1 = 2449958197289549823 or 0010 0001 (1111 x14)
@@ -89,10 +90,10 @@ class IntegerCodingTests : XCTestCase {
// 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00100001
XCTAssertEqual(data.count, 10)
XCTAssertEqual(data, [1, 255, 255, 255, 255, 255, 255, 255, 255, 33])
// encoding max 64-bit unsigned integer, 1-bit prefix
data = encodeIntegerToArray(UInt64.max, prefix: 1)
// calculations:
// subtract prefix:
// 18446744073709551615 - 1 = 18446744073709551614 or (1111 x15) 1110
@@ -104,10 +105,10 @@ class IntegerCodingTests : XCTestCase {
// 11111110 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000001
XCTAssertEqual(data.count, 11)
XCTAssertEqual(data, [1, 254, 255, 255, 255, 255, 255, 255, 255, 255, 1])
// something carefully crafted to produce maximum number of output bytes with minimum number of
// nonzero bits:
data = encodeIntegerToArray(9223372036854775809, prefix: 1)
data = encodeIntegerToArray(9_223_372_036_854_775_809, prefix: 1)
// calculations:
// subtract prefix:
@@ -120,11 +121,11 @@ class IntegerCodingTests : XCTestCase {
// 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 10000000 00000001
XCTAssertEqual(data.count, 11)
XCTAssertEqual(data, [1, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1])
// something similar, which uses an 8-bit prefix and still produces lots of zero bits:
// for those interested: this is the previous value + 254; thus only the first byte should differ
data = encodeIntegerToArray(9223372036854776063, prefix: 8)
data = encodeIntegerToArray(9_223_372_036_854_776_063, prefix: 8)
// calculations:
// subtract prefix:
// 9223372036854776063 - 255 = 9223372036854775808 or 1000 (0000 x15)
@@ -137,12 +138,12 @@ class IntegerCodingTests : XCTestCase {
XCTAssertEqual(data.count, 11)
XCTAssertEqual(data, [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 1])
}
func testIntegerDecoding() throws {
// any bits above the prefix amount shouldn't affect the outcome.
XCTAssertEqual(try decodeInteger(from: [0b00001010], prefix: 5), 10)
XCTAssertEqual(try decodeInteger(from: [0b11101010], prefix: 5), 10)
XCTAssertEqual(try decodeInteger(from: [0b00011111, 154, 10], prefix: 5), 1337)
XCTAssertEqual(try decodeInteger(from: [0b11111111, 154, 10], prefix: 5), 1337)
@@ -150,15 +151,30 @@ class IntegerCodingTests : XCTestCase {
// Now some larger numbers:
#if !os(watchOS)
XCTAssertEqual(try decodeInteger(from: [255, 129, 254, 255, 255, 255, 255, 255, 255, 33], prefix: 8), 2449958197289549824)
XCTAssertEqual(try decodeInteger(from: [1, 255, 255, 255, 255, 255, 255, 255, 255, 33], prefix: 1), 2449958197289549824)
XCTAssertEqual(try decodeInteger(from: [1, 254, 255, 255, 255, 255, 255, 255, 255, 127, 1], prefix: 1), Int.max)
XCTAssertEqual(
try decodeInteger(from: [255, 129, 254, 255, 255, 255, 255, 255, 255, 33], prefix: 8),
2_449_958_197_289_549_824
)
XCTAssertEqual(
try decodeInteger(from: [1, 255, 255, 255, 255, 255, 255, 255, 255, 33], prefix: 1),
2_449_958_197_289_549_824
)
XCTAssertEqual(
try decodeInteger(from: [1, 254, 255, 255, 255, 255, 255, 255, 255, 127, 1], prefix: 1),
Int.max
)
// lots of zeroes: each 128 yields zero
XCTAssertEqual(try decodeInteger(from: [1, 128, 128, 128, 128, 128, 128, 128, 128, 127, 1], prefix: 1), 9151314442816847873)
XCTAssertEqual(
try decodeInteger(from: [1, 128, 128, 128, 128, 128, 128, 128, 128, 127, 1], prefix: 1),
9_151_314_442_816_847_873
)
// almost the same bytes, but a different prefix:
XCTAssertEqual(try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 127, 1], prefix: 8), 9151314442816848127)
XCTAssertEqual(
try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 127, 1], prefix: 8),
9_151_314_442_816_848_127
)
#endif
// now a silly version which should never have been encoded in so many bytes
@@ -168,7 +184,9 @@ class IntegerCodingTests : XCTestCase {
func testIntegerDecodingMultiplicationDoesNotOverflow() throws {
// Zeros with continuation bits (e.g. 128) to increase the shift value (to 9 * 7 = 63), and then multiply by 127.
for `prefix` in 1...8 {
XCTAssertThrowsError(try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 127], prefix: prefix)) { error in
XCTAssertThrowsError(
try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 127], prefix: prefix)
) { error in
XCTAssert(error is NIOHPACKErrors.UnrepresentableInteger)
}
}
@@ -177,7 +195,9 @@ class IntegerCodingTests : XCTestCase {
func testIntegerDecodingAdditionDoesNotOverflow() throws {
// Zeros with continuation bits (e.g. 128) to increase the shift value (to 9 * 7 = 63), and then multiply by 127.
for `prefix` in 1...8 {
XCTAssertThrowsError(try decodeInteger(from: [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127], prefix: prefix)) { error in
XCTAssertThrowsError(
try decodeInteger(from: [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127], prefix: prefix)
) { error in
XCTAssert(error is NIOHPACKErrors.UnrepresentableInteger)
}
}
@@ -186,7 +206,9 @@ class IntegerCodingTests : XCTestCase {
func testIntegerDecodingShiftDoesNotOverflow() throws {
// With enough iterations we expect the shift to become greater >= 64.
for `prefix` in 1...8 {
XCTAssertThrowsError(try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], prefix: prefix)) { error in
XCTAssertThrowsError(
try decodeInteger(from: [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], prefix: prefix)
) { error in
XCTAssert(error is NIOHPACKErrors.UnrepresentableInteger)
}
}
@@ -12,8 +12,9 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOCore
import XCTest
@testable import NIOHTTP2
final class CircularBufferExtensionsTests: XCTestCase {
@@ -50,7 +51,6 @@ final class CircularBufferExtensionsTests: XCTestCase {
XCTAssertEqual(buffer.count, 15)
XCTAssertEqual(buffer.effectiveCapacity, 15)
buffer.prependWithoutExpanding(17)
XCTAssertEqual(Array(buffer), [17, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
XCTAssertEqual(buffer.count, 15)
@@ -12,11 +12,12 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOConcurrencyHelpers
import NIOCore
import NIOEmbedded
import NIOHPACK
import XCTest
@testable import NIOHTTP2
final class CompoundOutboundBufferTest: XCTestCase {
@@ -32,11 +33,18 @@ final class CompoundOutboundBufferTest: XCTestCase {
}
func testSimpleControlFramePassthrough() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 100, maxBufferedControlFrames: 1000)
var buffer = CompoundOutboundBuffer(
mode: .client,
initialMaxOutboundStreams: 100,
maxBufferedControlFrames: 1000
)
let frames: [HTTP2Frame] = [
HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 0), ack: true)),
HTTP2Frame(streamID: .rootStream, payload: .goAway(lastStreamID: 0, errorCode: .protocolError, opaqueData: nil)),
HTTP2Frame(
streamID: .rootStream,
payload: .goAway(lastStreamID: 0, errorCode: .protocolError, opaqueData: nil)
),
HTTP2Frame(streamID: .rootStream, payload: .settings(.settings([]))),
HTTP2Frame(streamID: .rootStream, payload: .alternativeService(origin: nil, field: nil)),
HTTP2Frame(streamID: .rootStream, payload: .origin([])),
@@ -45,7 +53,10 @@ final class CompoundOutboundBufferTest: XCTestCase {
]
for frame in frames {
XCTAssertNoThrow(try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertForward(), "Threw on \(frame)")
XCTAssertNoThrow(
try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertForward(),
"Threw on \(frame)"
)
}
}
@@ -55,14 +66,21 @@ final class CompoundOutboundBufferTest: XCTestCase {
// Let's create 100 new outbound streams.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrames: [HTTP2Frame] = stride(from: 3, to: 201, by: 2).map { streamID in
HTTP2Frame(streamID: HTTP2StreamID(streamID), payload: .headers(.init(headers: HPACKHeaders([]), endStream: true)))
HTTP2Frame(
streamID: HTTP2StreamID(streamID),
payload: .headers(.init(headers: HPACKHeaders([]), endStream: true))
)
}
// Write the first frame, and all the subsequent frames, and flush them. This will lead to one flushed frame.
XCTAssertNoThrow(try buffer.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try buffer.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward()
)
buffer.streamCreated(1, initialWindowSize: 65535)
for frame in subsequentFrames {
XCTAssertNoThrow(try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(frame, promise: nil, channelWritable: true).assertNothing()
)
}
buffer.flushReceived()
buffer.nextFlushedWritableFrame(channelWritable: true).assertNoFrame()
@@ -110,11 +128,13 @@ final class CompoundOutboundBufferTest: XCTestCase {
let elevenFrame = HTTP2Frame(streamID: 11, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try buffer.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 15)
XCTAssertNoThrow(try buffer.processOutboundFrame(elevenFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(elevenFrame, promise: nil, channelWritable: true).assertNothing()
)
// Now we're going to try to write a frame for stream 5. This will fail.
let fiveFrame = HTTP2Frame(streamID: 5, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertThrowsError(try buffer.processOutboundFrame(fiveFrame, promise: nil, channelWritable: true)) {error in
XCTAssertThrowsError(try buffer.processOutboundFrame(fiveFrame, promise: nil, channelWritable: true)) { error in
XCTAssertTrue(error is NIOHTTP2Errors.StreamIDTooSmall)
}
}
@@ -144,7 +164,9 @@ final class CompoundOutboundBufferTest: XCTestCase {
let dataFrame = self.createDataFrame(3, byteBufferSize: 15)
XCTAssertNoThrow(try buffer.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward())
buffer.streamCreated(1, initialWindowSize: 15)
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(dataFrame, promise: nil, channelWritable: true).assertNothing()
)
buffer.flushReceived()
buffer.nextFlushedWritableFrame(channelWritable: true).assertNoFrame()
@@ -168,14 +190,22 @@ final class CompoundOutboundBufferTest: XCTestCase {
let startPromise = self.loop.makePromise(of: Void.self)
let dataPromise = self.loop.makePromise(of: Void.self)
if streamID == 1 {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertForward())
XCTAssertNoThrow(
try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true)
.assertForward()
)
startPromise.succeed(())
buffer.streamCreated(streamID, initialWindowSize: 65535)
} else {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true)
.assertNothing()
)
}
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: dataPromise, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(dataFrame, promise: dataPromise, channelWritable: true).assertNothing()
)
futures.append(contentsOf: [startPromise.futureResult, dataPromise.futureResult])
}
@@ -185,7 +215,10 @@ final class CompoundOutboundBufferTest: XCTestCase {
results[idx] = true
}
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .protocolError))
XCTAssertEqual(
error as? NIOHTTP2Errors.StreamClosed,
NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .protocolError)
)
results.withLockedValue { results in
results[idx] = false
}
@@ -218,10 +251,14 @@ final class CompoundOutboundBufferTest: XCTestCase {
func testRejectsPrioritySelfDependency() {
var buffer = CompoundOutboundBuffer(mode: .client, initialMaxOutboundStreams: 1, maxBufferedControlFrames: 1000)
XCTAssertThrowsError(try buffer.priorityUpdate(streamID: 1, priorityData: .init(exclusive: false, dependency: 1, weight: 36))) { error in
XCTAssertThrowsError(
try buffer.priorityUpdate(streamID: 1, priorityData: .init(exclusive: false, dependency: 1, weight: 36))
) { error in
XCTAssertEqual(error as? NIOHTTP2Errors.PriorityCycle, NIOHTTP2Errors.priorityCycle(streamID: 1))
}
XCTAssertThrowsError(try buffer.priorityUpdate(streamID: 1, priorityData: .init(exclusive: true, dependency: 1, weight: 36))) { error in
XCTAssertThrowsError(
try buffer.priorityUpdate(streamID: 1, priorityData: .init(exclusive: true, dependency: 1, weight: 36))
) { error in
XCTAssertEqual(error as? NIOHTTP2Errors.PriorityCycle, NIOHTTP2Errors.priorityCycle(streamID: 1))
}
}
@@ -244,12 +281,20 @@ final class CompoundOutboundBufferTest: XCTestCase {
let secondHeaders = HTTP2Frame(streamID: 3, payload: .headers(.init(headers: HPACKHeaders([]))))
let secondData = self.createDataFrame(3, byteBufferSize: 5)
XCTAssertNoThrow(try buffer.processOutboundFrame(firstHeaders, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try buffer.processOutboundFrame(firstHeaders, promise: nil, channelWritable: true).assertForward()
)
buffer.streamCreated(1, initialWindowSize: 65535)
XCTAssertNoThrow(try buffer.processOutboundFrame(firstData, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(firstData, promise: nil, channelWritable: true).assertNothing()
)
XCTAssertNoThrow(try buffer.processOutboundFrame(secondHeaders, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(try buffer.processOutboundFrame(secondData, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(secondHeaders, promise: nil, channelWritable: true).assertNothing()
)
XCTAssertNoThrow(
try buffer.processOutboundFrame(secondData, promise: nil, channelWritable: true).assertNothing()
)
// Now mark them flushed.
buffer.flushReceived()
@@ -302,20 +347,30 @@ final class CompoundOutboundBufferTest: XCTestCase {
let startPromise = self.loop.makePromise(of: Void.self)
let dataPromise = self.loop.makePromise(of: Void.self)
if streamID == 1 {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertForward())
XCTAssertNoThrow(
try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true)
.assertForward()
)
startPromise.succeed(())
buffer.streamCreated(streamID, initialWindowSize: 65535)
} else {
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(startFrame, promise: startPromise, channelWritable: true)
.assertNothing()
)
}
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: dataPromise, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(dataFrame, promise: dataPromise, channelWritable: true).assertNothing()
)
futures.append(contentsOf: [startPromise.futureResult, dataPromise.futureResult])
}
let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 0), ack: false))
let pingPromise = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(try buffer.processOutboundFrame(pingFrame, promise: pingPromise, channelWritable: false).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(pingFrame, promise: pingPromise, channelWritable: false).assertNothing()
)
futures.append(pingPromise.futureResult)
for (idx, future) in futures.enumerated() {
@@ -349,16 +404,22 @@ final class CompoundOutboundBufferTest: XCTestCase {
// Send a HEADERS frame through to open up a space in the flow control buffer.
let startFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders())))
XCTAssertNoThrow(try buffer.processOutboundFrame(startFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try buffer.processOutboundFrame(startFrame, promise: nil, channelWritable: true).assertForward()
)
buffer.streamCreated(1, initialWindowSize: 65535)
// Send a DATA frame. This is buffered.
let dataFrame = self.createDataFrame(1, byteBufferSize: 50)
XCTAssertNoThrow(try buffer.processOutboundFrame(dataFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(dataFrame, promise: nil, channelWritable: true).assertNothing()
)
// Send in a PING frame which will be buffered.
let pingFrame = HTTP2Frame(streamID: .rootStream, payload: .ping(.init(withInteger: 9), ack: false))
XCTAssertNoThrow(try buffer.processOutboundFrame(pingFrame, promise: nil, channelWritable: false).assertNothing())
XCTAssertNoThrow(
try buffer.processOutboundFrame(pingFrame, promise: nil, channelWritable: false).assertNothing()
)
buffer.flushReceived()
@@ -377,7 +438,6 @@ final class CompoundOutboundBufferTest: XCTestCase {
}
}
extension CompoundOutboundBufferTest {
func createDataFrame(_ streamID: HTTP2StreamID, byteBufferSize: Int) -> HTTP2Frame {
var buffer = ByteBufferAllocator().buffer(capacity: byteBufferSize)
@@ -390,18 +450,18 @@ extension CompoundOutboundBuffer {
fileprivate mutating func receivedFrames(file: StaticString = #filePath, line: UInt = #line) -> [HTTP2Frame] {
var receivedFrames: [HTTP2Frame] = Array()
loop: while true {
switch self.nextFlushedWritableFrame(channelWritable: true) {
case .noFrame:
break loop
case .frame(let bufferedFrame, let promise):
receivedFrames.append(bufferedFrame)
promise?.succeed(())
case .error(let promise, let error):
promise?.fail(error)
XCTFail("Caught error: \(error)", file: (file), line: line)
loop: while true {
switch self.nextFlushedWritableFrame(channelWritable: true) {
case .noFrame:
break loop
case .frame(let bufferedFrame, let promise):
receivedFrames.append(bufferedFrame)
promise?.succeed(())
case .error(let promise, let error):
promise?.fail(error)
XCTFail("Caught error: \(error)", file: (file), line: line)
}
}
}
return receivedFrames
}
@@ -415,7 +475,11 @@ extension CompoundOutboundBuffer.FlushedWritableFrameResult {
}
}
internal func assertError<ErrorType: Error & Equatable>(_ error: ErrorType, file: StaticString = #filePath, line: UInt = #line) {
internal func assertError<ErrorType: Error & Equatable>(
_ error: ErrorType,
file: StaticString = #filePath,
line: UInt = #line
) {
guard case .error(let promise, let thrownError) = self else {
XCTFail("Expected .error, got \(self)", file: (file), line: line)
return
@@ -12,15 +12,15 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import NIOConcurrencyHelpers
import NIOCore
import NIOEmbedded
import NIOHPACK
import XCTest
@testable import NIOHTTP2
struct TestCaseError: Error { }
struct TestCaseError: Error {}
// MARK: Helper assertions for outbound frame actions.
// We need these to work around the fact that neither EventLoopPromise or MarkedCircularBuffer have equatable
@@ -40,7 +40,10 @@ extension OutboundFrameAction {
}
}
internal func assertForwardAndDrop(file: StaticString = #filePath, line: UInt = #line) throws -> (MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>, NIOHTTP2Errors.StreamClosed) {
internal func assertForwardAndDrop(
file: StaticString = #filePath,
line: UInt = #line
) throws -> (MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>, NIOHTTP2Errors.StreamClosed) {
guard case .forwardAndDrop(let promises, let error) = self else {
XCTFail("Expected .forwardAndDrop, got \(self)", file: (file), line: line)
throw TestCaseError()
@@ -49,7 +52,10 @@ extension OutboundFrameAction {
return (promises, error)
}
internal func assertSucceedAndDrop(file: StaticString = #filePath, line: UInt = #line) throws -> (MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>, NIOHTTP2Errors.StreamClosed) {
internal func assertSucceedAndDrop(
file: StaticString = #filePath,
line: UInt = #line
) throws -> (MarkedCircularBuffer<(HTTP2Frame, EventLoopPromise<Void>?)>, NIOHTTP2Errors.StreamClosed) {
guard case .succeedAndDrop(let promises, let error) = self else {
XCTFail("Expected .succeedAndDrop, got \(self)", file: (file), line: line)
throw TestCaseError()
@@ -59,7 +65,6 @@ extension OutboundFrameAction {
}
}
final class ConcurrentStreamBufferTests: XCTestCase {
var loop: EmbeddedEventLoop!
@@ -76,14 +81,18 @@ final class ConcurrentStreamBufferTests: XCTestCase {
var manager = ConcurrentStreamBuffer(mode: .client, initialMaxOutboundStreams: 2)
// Write frames for three streams.
let frames = stride(from: 1, through: 5, by: 2).map { HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(.init(headers: HPACKHeaders([])))) }
let results: [OutboundFrameAction] = try assertNoThrowWithValue(frames.map {
let result = try manager.processOutboundFrame($0, promise: nil, channelWritable: true)
if case .forward = result {
manager.streamCreated($0.streamID)
let frames = stride(from: 1, through: 5, by: 2).map {
HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(.init(headers: HPACKHeaders([]))))
}
let results: [OutboundFrameAction] = try assertNoThrowWithValue(
frames.map {
let result = try manager.processOutboundFrame($0, promise: nil, channelWritable: true)
if case .forward = result {
manager.streamCreated($0.streamID)
}
return result
}
return result
})
)
// Only two of these should have been passed through.
results[0].assertForward()
@@ -113,14 +122,18 @@ final class ConcurrentStreamBufferTests: XCTestCase {
var manager = ConcurrentStreamBuffer(mode: .client, initialMaxOutboundStreams: 2)
// Write frames for three streams and flush them.
let frames = stride(from: 1, through: 5, by: 2).map { HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(.init(headers: HPACKHeaders([])))) }
let results: [OutboundFrameAction] = try assertNoThrowWithValue(frames.map {
let result = try manager.processOutboundFrame($0, promise: nil, channelWritable: true)
if case .forward = result {
manager.streamCreated($0.streamID)
let frames = stride(from: 1, through: 5, by: 2).map {
HTTP2Frame(streamID: HTTP2StreamID($0), payload: .headers(.init(headers: HPACKHeaders([]))))
}
let results: [OutboundFrameAction] = try assertNoThrowWithValue(
frames.map {
let result = try manager.processOutboundFrame($0, promise: nil, channelWritable: true)
if case .forward = result {
manager.streamCreated($0.streamID)
}
return result
}
return result
})
)
// Only two of these should have been passed through.
results[0].assertForward()
@@ -138,8 +151,13 @@ final class ConcurrentStreamBufferTests: XCTestCase {
XCTAssertNil(manager.nextFlushedWritableFrame())
// Write a new stream. This should be buffered.
let newStreamFrame = HTTP2Frame(streamID: 7, payload: .headers(.init(headers: HPACKHeaders([]), endStream: true)))
XCTAssertNoThrow(try manager.processOutboundFrame(newStreamFrame, promise: nil, channelWritable: true).assertNothing())
let newStreamFrame = HTTP2Frame(
streamID: 7,
payload: .headers(.init(headers: HPACKHeaders([]), endStream: true))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(newStreamFrame, promise: nil, channelWritable: true).assertNothing()
)
XCTAssertNil(manager.nextFlushedWritableFrame())
// Complete stream 1. Nothing should happen, as only one stream is allowed through.
@@ -167,14 +185,21 @@ final class ConcurrentStreamBufferTests: XCTestCase {
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrames: [HTTP2Frame] = stride(from: 3, to: 201, by: 2).map { streamID in
HTTP2Frame(streamID: HTTP2StreamID(streamID), payload: .headers(.init(headers: HPACKHeaders([]), endStream: true)))
HTTP2Frame(
streamID: HTTP2StreamID(streamID),
payload: .headers(.init(headers: HPACKHeaders([]), endStream: true))
)
}
// Write the first frame, and all the subsequent frames, and flush them. This will lead to one flushed frame.
XCTAssertNoThrow(try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
for frame in subsequentFrames {
XCTAssertNoThrow(try manager.processOutboundFrame(frame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(frame, promise: nil, channelWritable: true).assertNothing()
)
}
manager.flushReceived()
XCTAssertNil(manager.nextFlushedWritableFrame())
@@ -198,13 +223,28 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3. We'll flush some, but not all of them
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
let subsequentFrame = HTTP2Frame(
streamID: 3,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
XCTAssertNoThrow(try (0..<15).forEach { _ in XCTAssertNoThrow(try manager.processOutboundFrame(subsequentFrame, promise: nil, channelWritable: true).assertNothing()) })
for _ in 0..<15 {
XCTAssertNoThrow(
try manager.processOutboundFrame(subsequentFrame, promise: nil, channelWritable: true)
.assertNothing()
)
}
manager.flushReceived()
XCTAssertNoThrow(try (0..<15).forEach { _ in XCTAssertNoThrow(try manager.processOutboundFrame(subsequentFrame, promise: nil, channelWritable: true).assertNothing()) })
for _ in 0..<15 {
XCTAssertNoThrow(
try manager.processOutboundFrame(subsequentFrame, promise: nil, channelWritable: true)
.assertNothing()
)
}
XCTAssertNil(manager.nextFlushedWritableFrame())
@@ -233,23 +273,39 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3. We'll flush some, but not all of them
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
let subsequentFrame = HTTP2Frame(
streamID: 3,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
let writeStatus = NIOLockedValueBox<[Bool?]>(Array(repeating: nil, count: 30))
var writePromises: [EventLoopFuture<Void>] = try assertNoThrowWithValue((0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing())
return p.futureResult
})
var writePromises: [EventLoopFuture<Void>] = try assertNoThrowWithValue(
(0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(
try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing()
)
return p.futureResult
}
)
manager.flushReceived()
XCTAssertNoThrow(try writePromises.append(contentsOf: (0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing())
return p.futureResult
}))
XCTAssertNoThrow(
try writePromises.append(
contentsOf: (0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(
try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true)
.assertNothing()
)
return p.futureResult
}
)
)
for (idx, promise) in writePromises.enumerated() {
promise.map {
@@ -257,7 +313,10 @@ final class ConcurrentStreamBufferTests: XCTestCase {
writeStatus[idx] = true
}
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .cancel))
XCTAssertEqual(
error as? NIOHTTP2Errors.StreamClosed,
NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .cancel)
)
writeStatus.withLockedValue { writeStatus in
writeStatus[idx] = false
}
@@ -280,12 +339,17 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// Only the first 15 are done.
writeStatus.withLockedValue { writeStatus in
XCTAssertEqual(writeStatus, Array(repeating: true as Bool?, count: 15) + Array(repeating: nil as Bool?, count: 15))
XCTAssertEqual(
writeStatus,
Array(repeating: true as Bool?, count: 15) + Array(repeating: nil as Bool?, count: 15)
)
}
// Now we're going to write a RST_STREAM frame on stream 3
let rstStreamFrame = HTTP2Frame(streamID: 3, payload: .rstStream(.cancel))
let (droppedWrites, error) = try assertNoThrowWithValue(manager.processOutboundFrame(rstStreamFrame, promise: nil, channelWritable: true).assertForwardAndDrop())
let (droppedWrites, error) = try assertNoThrowWithValue(
manager.processOutboundFrame(rstStreamFrame, promise: nil, channelWritable: true).assertForwardAndDrop()
)
// Fail all these writes.
for write in droppedWrites {
@@ -294,7 +358,10 @@ final class ConcurrentStreamBufferTests: XCTestCase {
XCTAssertEqual(error, NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .cancel))
writeStatus.withLockedValue { writeStatus in
XCTAssertEqual(writeStatus, Array(repeating: true as Bool?, count: 15) + Array(repeating: false as Bool?, count: 15))
XCTAssertEqual(
writeStatus,
Array(repeating: true as Bool?, count: 15) + Array(repeating: false as Bool?, count: 15)
)
}
}
@@ -303,17 +370,26 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3. We won't flush stream 3.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
let subsequentFrame = HTTP2Frame(
streamID: 3,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
let writeStatus = NIOLockedValueBox<[Bool?]>(Array(repeating: nil, count: 15))
let writePromises: [EventLoopFuture<Void>] = try assertNoThrowWithValue((0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing())
return p.futureResult
})
let writePromises: [EventLoopFuture<Void>] = try assertNoThrowWithValue(
(0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(
try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing()
)
return p.futureResult
}
)
for (idx, promise) in writePromises.enumerated() {
promise.map {
@@ -321,7 +397,10 @@ final class ConcurrentStreamBufferTests: XCTestCase {
writeStatus[idx] = true
}
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .cancel))
XCTAssertEqual(
error as? NIOHTTP2Errors.StreamClosed,
NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .cancel)
)
writeStatus.withLockedValue { writeStatus in
writeStatus[idx] = false
}
@@ -336,7 +415,9 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// Now we're going to send RST_STREAM on 3. All writes should fail, and we should be asked to drop the RST_STREAM frame.
let rstStreamFrame = HTTP2Frame(streamID: 3, payload: .rstStream(.cancel))
let (droppedWrites, error) = try assertNoThrowWithValue(manager.processOutboundFrame(rstStreamFrame, promise: nil, channelWritable: true).assertSucceedAndDrop())
let (droppedWrites, error) = try assertNoThrowWithValue(
manager.processOutboundFrame(rstStreamFrame, promise: nil, channelWritable: true).assertSucceedAndDrop()
)
// Fail all these writes.
for write in droppedWrites {
@@ -358,14 +439,19 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to create stream 1 and stream 11. Stream 1 will be passed through, stream 11 will have to wait.
let oneFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let elevenFrame = HTTP2Frame(streamID: 11, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try manager.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try manager.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
XCTAssertNoThrow(try manager.processOutboundFrame(elevenFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(elevenFrame, promise: nil, channelWritable: true).assertNothing()
)
// Now we're going to try to write a frame for stream 5. This will fail.
let fiveFrame = HTTP2Frame(streamID: 5, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertThrowsError(try manager.processOutboundFrame(fiveFrame, promise: nil, channelWritable: true)) { error in
XCTAssertThrowsError(try manager.processOutboundFrame(fiveFrame, promise: nil, channelWritable: true)) {
error in
XCTAssertTrue(error is NIOHTTP2Errors.StreamIDTooSmall)
}
}
@@ -376,13 +462,19 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to create stream 1 and stream 3, which will be buffered.
let oneFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let threeFrame = HTTP2Frame(streamID: 3, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try manager.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try manager.processOutboundFrame(oneFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
XCTAssertNoThrow(try manager.processOutboundFrame(threeFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(threeFrame, promise: nil, channelWritable: true).assertNothing()
)
// Now we're going to try to write a frame for stream 4, a server-initiated stream. This will be passed through safely.
let fourFrame = HTTP2Frame(streamID: 4, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try manager.processOutboundFrame(fourFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try manager.processOutboundFrame(fourFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(4)
XCTAssertEqual(manager.currentOutboundStreams, 1)
@@ -394,21 +486,37 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to create stream 2 and stream 4. First, however, we reserve them. These are not buffered, per RFC 7540 § 5.1.2:
//
// > Streams in either of the "reserved" states do not count toward the stream limit.
let twoFramePromise = HTTP2Frame(streamID: 1, payload: .pushPromise(.init(pushedStreamID: 2, headers: HPACKHeaders([]))))
let fourFramePromise = HTTP2Frame(streamID: 1, payload: .pushPromise(.init(pushedStreamID: 4, headers: HPACKHeaders([]))))
XCTAssertNoThrow(try manager.processOutboundFrame(twoFramePromise, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(try manager.processOutboundFrame(fourFramePromise, promise: nil, channelWritable: true).assertForward())
let twoFramePromise = HTTP2Frame(
streamID: 1,
payload: .pushPromise(.init(pushedStreamID: 2, headers: HPACKHeaders([])))
)
let fourFramePromise = HTTP2Frame(
streamID: 1,
payload: .pushPromise(.init(pushedStreamID: 4, headers: HPACKHeaders([])))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(twoFramePromise, promise: nil, channelWritable: true).assertForward()
)
XCTAssertNoThrow(
try manager.processOutboundFrame(fourFramePromise, promise: nil, channelWritable: true).assertForward()
)
// Now we're going to try to initiate these by sending HEADERS frames. This will buffer the second one.
let twoFrame = HTTP2Frame(streamID: 2, payload: .headers(.init(headers: HPACKHeaders([]))))
let fourFrame = HTTP2Frame(streamID: 4, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try manager.processOutboundFrame(twoFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try manager.processOutboundFrame(twoFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(2)
XCTAssertNoThrow(try manager.processOutboundFrame(fourFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(fourFrame, promise: nil, channelWritable: true).assertNothing()
)
// Now we're going to try to write a frame for stream 3, a client-initiated stream. This will be passed through safely.
let threeFrame = HTTP2Frame(streamID: 3, payload: .headers(.init(headers: HPACKHeaders([]))))
XCTAssertNoThrow(try manager.processOutboundFrame(threeFrame, promise: nil, channelWritable: true).assertForward())
XCTAssertNoThrow(
try manager.processOutboundFrame(threeFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(3)
XCTAssertEqual(manager.currentOutboundStreams, 1)
@@ -420,23 +528,35 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// We're going to start stream 1, and then arrange a buffer of a bunch of frames in stream 3.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders([]))))
let subsequentFrame = HTTP2Frame(streamID: 3, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward())
let subsequentFrame = HTTP2Frame(
streamID: 3,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: true).assertForward()
)
manager.streamCreated(1)
let writeStatus = NIOLockedValueBox<[Bool?]>(Array(repeating: nil, count: 15))
let writePromises: [EventLoopFuture<Void>] = try assertNoThrowWithValue((0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing())
return p.futureResult
})
let writePromises: [EventLoopFuture<Void>] = try assertNoThrowWithValue(
(0..<15).map { _ in
let p = self.loop.makePromise(of: Void.self)
XCTAssertNoThrow(
try manager.processOutboundFrame(subsequentFrame, promise: p, channelWritable: true).assertNothing()
)
return p.futureResult
}
)
for (idx, promise) in writePromises.enumerated() {
promise.map {
writeStatus.withLockedValue { writeStatus in
writeStatus[idx] = true
}
}.whenFailure { error in
XCTAssertEqual(error as? NIOHTTP2Errors.StreamClosed, NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .protocolError))
XCTAssertEqual(
error as? NIOHTTP2Errors.StreamClosed,
NIOHTTP2Errors.streamClosed(streamID: 3, errorCode: .protocolError)
)
writeStatus.withLockedValue { writeStatus in
writeStatus[idx] = false
}
@@ -470,11 +590,15 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// Write a frame for the first stream. This will be buffered, as the connection isn't writable.
let firstFrame = HTTP2Frame(streamID: 1, payload: .headers(.init(headers: HPACKHeaders())))
XCTAssertNoThrow(try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: false).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(firstFrame, promise: nil, channelWritable: false).assertNothing()
)
// Write a frame for the second stream. This should also be buffered, as the connection still isn't writable.
let secondFrame = HTTP2Frame(streamID: 3, payload: .headers(.init(headers: HPACKHeaders())))
XCTAssertNoThrow(try manager.processOutboundFrame(secondFrame, promise: nil, channelWritable: false).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(secondFrame, promise: nil, channelWritable: false).assertNothing()
)
manager.flushReceived()
@@ -488,18 +612,35 @@ final class ConcurrentStreamBufferTests: XCTestCase {
// Now write a frame for stream 5. Even though the connection is writable, this should be buffered.
let thirdFrame = HTTP2Frame(streamID: 5, payload: .headers(.init(headers: HPACKHeaders())))
XCTAssertNoThrow(try manager.processOutboundFrame(thirdFrame, promise: nil, channelWritable: true).assertNothing())
XCTAssertNoThrow(
try manager.processOutboundFrame(thirdFrame, promise: nil, channelWritable: true).assertNothing()
)
// Now we're going to try to write data once to each stream, simulating the channel being blocked. Stream 1 should
// progress, as the stream is actually open, while the writes for stream 3 and 5 should be buffered.
let fourthFrame = HTTP2Frame(streamID: 1, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(fourthFrame, promise: nil, channelWritable: true).assertForward())
let fourthFrame = HTTP2Frame(
streamID: 1,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(fourthFrame, promise: nil, channelWritable: true).assertForward()
)
let fifthFrame = HTTP2Frame(streamID: 3, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(fifthFrame, promise: nil, channelWritable: true).assertNothing())
let fifthFrame = HTTP2Frame(
streamID: 3,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(fifthFrame, promise: nil, channelWritable: true).assertNothing()
)
let sixthFrame = HTTP2Frame(streamID: 5, payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0)))))
XCTAssertNoThrow(try manager.processOutboundFrame(sixthFrame, promise: nil, channelWritable: true).assertNothing())
let sixthFrame = HTTP2Frame(
streamID: 5,
payload: .data(.init(data: .byteBuffer(ByteBufferAllocator().buffer(capacity: 0))))
)
XCTAssertNoThrow(
try manager.processOutboundFrame(sixthFrame, promise: nil, channelWritable: true).assertNothing()
)
manager.flushReceived()
Loaded 100 of 125 files, more files were not shown because too many files have changed in this diff. Show more