diff --git a/Sources/NIOHTTP2/ConnectionStateMachine/ConnectionStateMachine.swift b/Sources/NIOHTTP2/ConnectionStateMachine/ConnectionStateMachine.swift index 492674a..edb767f 100644 --- a/Sources/NIOHTTP2/ConnectionStateMachine/ConnectionStateMachine.swift +++ b/Sources/NIOHTTP2/ConnectionStateMachine/ConnectionStateMachine.swift @@ -460,6 +460,16 @@ struct HTTP2ConnectionStateMachine { return false } } + + /// Whether the preamble can be sent. + var mustSendPreamble: Bool { + switch self.state { + case .idle, .prefaceReceived, .quiescingPrefaceReceived: + return true + default: + return false + } + } } // MARK:- State modifying methods diff --git a/Sources/NIOHTTP2/HTTP2ChannelHandler.swift b/Sources/NIOHTTP2/HTTP2ChannelHandler.swift index 1857822..002541b 100644 --- a/Sources/NIOHTTP2/HTTP2ChannelHandler.swift +++ b/Sources/NIOHTTP2/HTTP2ChannelHandler.swift @@ -318,6 +318,10 @@ extension NIOHTTP2Handler { extension NIOHTTP2Handler { /// Issues the preamble when necessary. private func writeAndFlushPreamble(context: ChannelHandlerContext) { + guard self.stateMachine.mustSendPreamble else { + return + } + if case .client = self.mode { self.writeBuffer.clear() self.writeBuffer.writeStaticString(NIOHTTP2Handler.clientMagic) diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests+XCTest.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests+XCTest.swift index 62d2cee..067ff04 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests+XCTest.swift @@ -28,6 +28,7 @@ extension ConfiguringPipelineTests { return [ ("testBasicPipelineCommunicates", testBasicPipelineCommunicates), ("testPipelineRespectsPositionRequest", testPipelineRespectsPositionRequest), + ("testPreambleGetsWrittenOnce", testPreambleGetsWrittenOnce), ] } } diff --git a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift index 4dda9db..32382ae 100644 --- a/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift +++ b/Tests/NIOHTTP2Tests/ConfiguringPipelineTests.swift @@ -105,4 +105,37 @@ class ConfiguringPipelineTests: XCTestCase { XCTAssertNoThrow(try self.clientChannel.finish()) XCTAssertNoThrow(try self.serverChannel.finish()) } + + func testPreambleGetsWrittenOnce() throws { + // This test checks that the preamble sent by NIOHTTP2Handler is only written once. There are two paths + // to sending the preamble, in handlerAdded(context:) (if the channel is active) and in channelActive(context:), + // we want to hit both of these. + let connectionPromise: EventLoopPromise = self.serverChannel.eventLoop.makePromise() + + // Register the callback on the promise so we run it as it succeeds (i.e. before fireChannelActive is called). + let handlerAdded = connectionPromise.futureResult.flatMap { + // Use .server to avoid sending client magic. + self.serverChannel.configureHTTP2Pipeline(mode: .server) + } + + self.serverChannel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake"), promise: connectionPromise) + XCTAssertNoThrow(try handlerAdded.wait()) + + let initialSettingsData: IOData? = try self.serverChannel.readOutbound() + XCTAssertNotNil(initialSettingsData) + + guard case .some(.byteBuffer(let initialSettingsBuffer)) = initialSettingsData else { + XCTFail("Expected ByteBuffer containing the initial SETTINGS frame") + return + } + + XCTAssertGreaterThanOrEqual(initialSettingsBuffer.readableBytes, 9) + // The 4-th byte contains the frame type (4 for SETTINGS). + XCTAssertEqual(initialSettingsBuffer.getInteger(at: initialSettingsBuffer.readerIndex + 3, as: UInt8.self), 4) + // Bytes 6-9 contain the stream ID; this should be the root stream, 0. + XCTAssertEqual(initialSettingsBuffer.getInteger(at: initialSettingsBuffer.readerIndex + 6, as: UInt32.self), 0) + + // We don't expect anything else at this point. + XCTAssertNil(try self.serverChannel.readOutbound(as: IOData.self)) + } }