diff --git a/Sources/NIOCrashTester/CrashTestSuites.swift b/Sources/NIOCrashTester/CrashTestSuites.swift index 0e6adc883..03ec6e76e 100644 --- a/Sources/NIOCrashTester/CrashTestSuites.swift +++ b/Sources/NIOCrashTester/CrashTestSuites.swift @@ -16,4 +16,5 @@ let crashTestSuites: [String: Any] = [ "EventLoopCrashTests": EventLoopCrashTests(), "ByteBufferCrashTests": ByteBufferCrashTests(), "SystemCrashTests": SystemCrashTests(), + "HTTPCrashTests": HTTPCrashTests(), ] diff --git a/Sources/NIOCrashTester/CrashTests+HTTP.swift b/Sources/NIOCrashTester/CrashTests+HTTP.swift new file mode 100644 index 000000000..3c24f5a52 --- /dev/null +++ b/Sources/NIOCrashTester/CrashTests+HTTP.swift @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import NIO +import NIOHTTP1 + +struct HTTPCrashTests { + let testEncodingChunkedAndContentLengthForRequestsCrashes = CrashTest( + regex: "^Assertion failed: illegal HTTP sent: HTTPRequestHead .* contains both a content-length and transfer-encoding:chunked", + { + let channel = EmbeddedChannel(handler: HTTPRequestEncoder()) + _ = try? channel.writeAndFlush( + HTTPClientRequestPart.head( + HTTPRequestHead(version: .init(major: 1, minor: 1), + method: .POST, + uri: "/", + headers: ["content-Length": "1", + "transfer-Encoding": "chunked"]))).wait() + }) + + let testEncodingChunkedAndContentLengthForResponseCrashes = CrashTest( + regex: "^Assertion failed: illegal HTTP sent: HTTPResponseHead .* contains both a content-length and transfer-encoding:chunked", + { + let channel = EmbeddedChannel(handler: HTTPResponseEncoder()) + _ = try? channel.writeAndFlush( + HTTPServerResponsePart.head( + HTTPResponseHead(version: .init(major: 1, minor: 1), + status: .ok, + headers: ["content-Length": "1", + "transfer-Encoding": "chunked"]))).wait() + }) +} diff --git a/Sources/NIOCrashTester/OutputGrepper.swift b/Sources/NIOCrashTester/OutputGrepper.swift index 836d76760..2ff32fdbb 100644 --- a/Sources/NIOCrashTester/OutputGrepper.swift +++ b/Sources/NIOCrashTester/OutputGrepper.swift @@ -65,7 +65,9 @@ private final class GrepHandler: ChannelInboundHandler { func channelRead(context: ChannelHandlerContext, data: NIOAny) { let line = self.unwrapInboundIn(data) - if line.lowercased().starts(with: "fatal error: ") || line.lowercased().starts(with: "precondition failed: ") { + if line.lowercased().starts(with: "fatal error: ") || + line.lowercased().starts(with: "precondition failed: ") || + line.lowercased().starts(with: "assertion failed: ") { self.promise.succeed(line) context.close(promise: nil) } diff --git a/Sources/NIOHTTP1/HTTPEncoder.swift b/Sources/NIOHTTP1/HTTPEncoder.swift index 4a2e543a9..ded177e0e 100644 --- a/Sources/NIOHTTP1/HTTPEncoder.swift +++ b/Sources/NIOHTTP1/HTTPEncoder.swift @@ -143,7 +143,11 @@ public final class HTTPRequestEncoder: ChannelOutboundHandler, RemovableChannelH public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { switch self.unwrapOutboundIn(data) { case .head(var request): - self.isChunked = sanitizeTransportHeaders(hasBody: request.method.hasRequestBody, headers: &request.headers, version: request.version) == .chunked + assert(!(request.headers.contains(name: "content-length") && + request.headers[canonicalForm: "transfer-encoding"].contains("chunked"[...])), + "illegal HTTP sent: \(request) contains both a content-length and transfer-encoding:chunked") + self.isChunked = sanitizeTransportHeaders(hasBody: request.method.hasRequestBody, + headers: &request.headers, version: request.version) == .chunked writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in buffer.write(request: request) @@ -177,8 +181,11 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler, RemovableChannel public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { switch self.unwrapOutboundIn(data) { case .head(var response): - - self.isChunked = sanitizeTransportHeaders(hasBody: response.status.mayHaveResponseBody ? .yes : .no, headers: &response.headers, version: response.version) == .chunked + assert(!(response.headers.contains(name: "content-length") && + response.headers[canonicalForm: "transfer-encoding"].contains("chunked"[...])), + "illegal HTTP sent: \(response) contains both a content-length and transfer-encoding:chunked") + self.isChunked = sanitizeTransportHeaders(hasBody: response.status.mayHaveResponseBody ? .yes : .no, + headers: &response.headers, version: response.version) == .chunked writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in buffer.write(response: response)