diff --git a/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift b/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift index d04dedab8..55b2e4f9f 100644 --- a/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift +++ b/Sources/NIOHTTP1/HTTPServerPipelineHandler.swift @@ -363,7 +363,7 @@ public final class HTTPServerPipelineHandler: ChannelDuplexHandler { self.deliverOneMessage(ctx: ctx, data: read) deliveredRead = true case .error(let error): - ctx.fireErrorCaught(error) + self.deliverOneError(ctx: ctx, error: error) case .halfClose: // When we fire the half-close, we want to forget all prior reads. // They will just trigger further half-close notifications we don't diff --git a/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest+XCTest.swift b/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest+XCTest.swift index 7e7d70de6..ad6cbce6f 100644 --- a/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest+XCTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest+XCTest.swift @@ -43,6 +43,8 @@ extension HTTPServerPipelineHandlerTest { ("testQuiescingAfterRequestAndResponseHeadsButBeforeAnyEndsThenRequestEndBeforeResponseEnd", testQuiescingAfterRequestAndResponseHeadsButBeforeAnyEndsThenRequestEndBeforeResponseEnd), ("testQuiescingAfterRequestAndResponseHeadsButBeforeAnyEndsThenRequestEndAfterResponseEnd", testQuiescingAfterRequestAndResponseHeadsButBeforeAnyEndsThenRequestEndAfterResponseEnd), ("testQuiescingAfterHavingReceivedOneRequestButBeforeResponseWasSentWithMoreRequestsInTheBuffer", testQuiescingAfterHavingReceivedOneRequestButBeforeResponseWasSentWithMoreRequestsInTheBuffer), + ("testParserErrorOnly", testParserErrorOnly), + ("testLegitRequestFollowedByParserErrorArrivingWhilstResponseOutstanding", testLegitRequestFollowedByParserErrorArrivingWhilstResponseOutstanding), ] } } diff --git a/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest.swift b/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest.swift index a3f11ca4e..28fb2073a 100644 --- a/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPServerPipelineHandlerTest.swift @@ -20,7 +20,7 @@ import XCTest import NIO -import NIOHTTP1 +@testable import NIOHTTP1 private final class ReadRecorder: ChannelInboundHandler { typealias InboundIn = HTTPServerRequestPart @@ -706,4 +706,99 @@ class HTTPServerPipelineHandlerTest: XCTestCase { XCTAssertFalse(self.channel.isActive) self.channel = nil } + + func testParserErrorOnly() throws { + class VerifyOrderHandler: ChannelInboundHandler { + typealias InboundIn = HTTPServerRequestPart + typealias OutboundOut = HTTPServerResponsePart + + enum State { + case errorExpected + case done + } + var state: State = .errorExpected + + func errorCaught(ctx: ChannelHandlerContext, error: Error) { + XCTAssertEqual(HTTPParserError.headerOverflow, error as? HTTPParserError) + XCTAssertEqual(.errorExpected, self.state) + self.state = .done + } + + func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + XCTFail("no requests expected") + } + } + + let handler = VerifyOrderHandler() + XCTAssertNoThrow(try self.channel.pipeline.add(handler: HTTPServerProtocolErrorHandler()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.add(handler: handler).wait()) + + self.channel.pipeline.fireErrorCaught(HTTPParserError.headerOverflow) + + XCTAssertEqual(.done, handler.state) + } + + func testLegitRequestFollowedByParserErrorArrivingWhilstResponseOutstanding() throws { + func makeRequestHead(uri: String) -> HTTPRequestHead { + var requestHead = HTTPRequestHead(version: .init(major: 1, minor: 1), method: .GET, uri: uri) + requestHead.headers.add(name: "Host", value: "example.com") + return requestHead + } + + class VerifyOrderHandler: ChannelInboundHandler { + typealias InboundIn = HTTPServerRequestPart + typealias OutboundOut = HTTPServerResponsePart + + enum State { + case reqHeadExpected + case reqEndExpected + case errorExpected + case done + } + var state: State = .reqHeadExpected + + func errorCaught(ctx: ChannelHandlerContext, error: Error) { + XCTAssertEqual(HTTPParserError.closedConnection, error as? HTTPParserError) + XCTAssertEqual(.errorExpected, self.state) + self.state = .done + } + + func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + switch self.unwrapInboundIn(data) { + case .head: + // We dispatch this to the event loop so that it doesn't happen immediately but rather can be + // run from the driving test code whenever it wants by running the EmbeddedEventLoop. + ctx.eventLoop.execute { + ctx.writeAndFlush(self.wrapOutboundOut(.head(.init(version: HTTPVersion(major: 1, minor: 1), + status: .ok))), + promise: nil) + } + XCTAssertEqual(.reqHeadExpected, self.state) + self.state = .reqEndExpected + case .body: + XCTFail("no body expected") + case .end: + // We dispatch this to the event loop so that it doesn't happen immediately but rather can be + // run from the driving test code whenever it wants by running the EmbeddedEventLoop. + ctx.eventLoop.execute { + ctx.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) + } + XCTAssertEqual(.reqEndExpected, self.state) + self.state = .errorExpected + } + } + } + + let handler = VerifyOrderHandler() + XCTAssertNoThrow(try self.channel.pipeline.add(handler: HTTPServerProtocolErrorHandler()).wait()) + XCTAssertNoThrow(try self.channel.pipeline.add(handler: handler).wait()) + + XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.head(makeRequestHead(uri: "/one")))) + XCTAssertNoThrow(try self.channel.writeInbound(HTTPServerRequestPart.end(nil))) + self.channel.pipeline.fireErrorCaught(HTTPParserError.closedConnection) + + // let's now run the HTTP responses that we enqueued earlier on. + (self.channel.eventLoop as! EmbeddedEventLoop).run() + XCTAssertEqual(.done, handler.state) + } }