From 7de397fda80f399f5feda25c2511d07ecf7c4db2 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Thu, 18 Jul 2019 17:04:27 +0100 Subject: [PATCH] ignore frames that don't have a HTTP/1 equivalent (#146) Motivation: Previously, the H2to1 codec would crash if for example a push promise came by. Modifications: Instead of crashing, ignore the frames without equivalent. Result: Fewer crashes. --- Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift | 7 ++---- .../HTTP2ToHTTP1CodecTests+XCTest.swift | 1 + .../HTTP2ToHTTP1CodecTests.swift | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift index 2a2ebc6..8228d28 100644 --- a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift +++ b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift @@ -79,12 +79,9 @@ public final class HTTP2ToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutbou if content.endStream { context.fireChannelRead(self.wrapInboundOut(.end(nil))) } - case .alternativeService, .rstStream, .priority, .windowUpdate: - // All these frames may be sent on a stream, but are ignored by the multiplexer and will be - // handled elsewhere. + case .alternativeService, .rstStream, .priority, .windowUpdate, .settings, .pushPromise, .ping, .goAway, .origin: + // These don't have an HTTP/1 equivalent, so let's drop them. return - default: - fatalError("unimplemented frame \(frame)") } } diff --git a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests+XCTest.swift b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests+XCTest.swift index 2325181..916b35d 100644 --- a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests+XCTest.swift @@ -53,6 +53,7 @@ extension HTTP2ToHTTP1CodecTests { ("testReceiveResponseWithNonNumericalStatus", testReceiveResponseWithNonNumericalStatus), ("testSendRequestWithoutHost", testSendRequestWithoutHost), ("testSendRequestWithDuplicateHost", testSendRequestWithDuplicateHost), + ("testFramesWithoutHTTP1EquivalentAreIgnored", testFramesWithoutHTTP1EquivalentAreIgnored), ] } } diff --git a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift index 63074b4..3966e53 100644 --- a/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2ToHTTP1CodecTests.swift @@ -640,4 +640,26 @@ final class HTTP2ToHTTP1CodecTests: XCTestCase { XCTAssertEqual(error as? NIOHTTP2Errors.DuplicateHostHeader, NIOHTTP2Errors.DuplicateHostHeader()) } } + + func testFramesWithoutHTTP1EquivalentAreIgnored() throws { + let streamID = HTTP2StreamID(1) + XCTAssertNoThrow(try self.channel.pipeline.addHandler(HTTP2ToHTTP1ClientCodec(streamID: streamID, + httpProtocol: .https)).wait()) + + let headers = HPACKHeaders([(":method", "GET"), (":scheme", "https"), (":path", "/x")]) + let frames: [HTTP2Frame.FramePayload] = [.alternativeService(origin: nil, field: nil), + .rstStream(.init(networkCode: 1)), + .priority(.init(exclusive: true, dependency: streamID, weight: 1)), + .windowUpdate(windowSizeIncrement: 1), + .settings(.ack), + .pushPromise(.init(pushedStreamID: HTTP2StreamID(2), headers: headers)), + .ping(.init(withInteger: 123), ack: true), + .goAway(lastStreamID: streamID, errorCode: .init(networkCode: 1), opaqueData: nil), + .origin([])] + for payload in frames { + let frame = HTTP2Frame(streamID: streamID, payload: payload) + XCTAssertNoThrow(try self.channel.writeInbound(frame), "error on \(frame)") + } + XCTAssertNoThrow(XCTAssertTrue(try self.channel.finish().isClean)) + } }