From 7d5109a673f3fdcbfd2233aedfb948a07d1825e3 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Fri, 19 Jan 2018 15:46:52 +0000 Subject: [PATCH] no auto headers for GET --- Sources/NIOHTTP1/HTTPEncoder.swift | 14 +++++++++----- Sources/NIOHTTP1/HTTPTypes.swift | 19 ++++++++++++++----- .../HTTPRequestEncoderTest+XCTest.swift | 2 ++ .../HTTPRequestEncoderTest.swift | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Sources/NIOHTTP1/HTTPEncoder.swift b/Sources/NIOHTTP1/HTTPEncoder.swift index e6e300939..68a46975b 100644 --- a/Sources/NIOHTTP1/HTTPEncoder.swift +++ b/Sources/NIOHTTP1/HTTPEncoder.swift @@ -96,12 +96,16 @@ private func isChunkedPart(_ headers: HTTPHeaders) -> Bool { /// /// Note that for HTTP/1.0 if there is no Content-Length then the response should be followed /// by connection close. We require that the user send that connection close: we don't do it. -private func sanitizeTransportHeaders(mayHaveBody: Bool, headers: inout HTTPHeaders, version: HTTPVersion) { - if !mayHaveBody { +private func sanitizeTransportHeaders(hasBody: HTTPMethod.HasBody, headers: inout HTTPHeaders, version: HTTPVersion) { + switch hasBody { + case .no: headers.remove(name: "content-length") headers.remove(name: "transfer-encoding") - } else if headers["content-length"].count == 0 && version.major == 1 && version.minor >= 1 { + case .yes where headers["content-length"].count == 0 && version.major == 1 && version.minor >= 1: headers.replaceOrAdd(name: "transfer-encoding", value: "chunked") + case .yes, .unlikely: + /* leave alone */ + () } } @@ -120,7 +124,7 @@ public final class HTTPRequestEncoder : ChannelOutboundHandler { public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { switch self.unwrapOutboundIn(data) { case .head(var request): - sanitizeTransportHeaders(mayHaveBody: request.method.mayHaveRequestBody, headers: &request.headers, version: request.version) + sanitizeTransportHeaders(hasBody: request.method.hasRequestBody, headers: &request.headers, version: request.version) self.isChunked = isChunkedPart(request.headers) @@ -155,7 +159,7 @@ public final class HTTPResponseEncoder : ChannelOutboundHandler { public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { switch self.unwrapOutboundIn(data) { case .head(var response): - sanitizeTransportHeaders(mayHaveBody: response.status.mayHaveResponseBody, headers: &response.headers, version: response.version) + sanitizeTransportHeaders(hasBody: response.status.mayHaveResponseBody ? .yes : .no, headers: &response.headers, version: response.version) self.isChunked = isChunkedPart(response.headers) writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in diff --git a/Sources/NIOHTTP1/HTTPTypes.swift b/Sources/NIOHTTP1/HTTPTypes.swift index 86b68f5c2..72b231084 100644 --- a/Sources/NIOHTTP1/HTTPTypes.swift +++ b/Sources/NIOHTTP1/HTTPTypes.swift @@ -358,6 +358,12 @@ extension HTTPHeaders : Equatable { } public enum HTTPMethod: Equatable { + public enum HasBody { + case yes + case no + case unlikely + } + public static func ==(lhs: HTTPMethod, rhs: HTTPMethod) -> Bool { switch (lhs, rhs){ case (.GET, .GET): @@ -469,13 +475,16 @@ public enum HTTPMethod: Equatable { case RAW(value: String) /// Whether requests with this verb may have a request body. - public var mayHaveRequestBody: Bool { - // TODO: Add more + public var hasRequestBody: HasBody { switch self { - case .HEAD: - return false + case .HEAD, .DELETE, .TRACE: + return .no + case .POST, .PUT, .CONNECT, .PATCH: + return .yes + case .GET, .OPTIONS: + fallthrough default: - return true + return .unlikely } } } diff --git a/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest+XCTest.swift b/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest+XCTest.swift index 6b8f695b4..e095b0709 100644 --- a/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest+XCTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest+XCTest.swift @@ -27,6 +27,8 @@ extension HTTPRequestEncoderTests { static var allTests : [(String, (HTTPRequestEncoderTests) -> () throws -> Void)] { return [ ("testNoAutoHeadersForHEAD", testNoAutoHeadersForHEAD), + ("testNoAutoHeadersForGET", testNoAutoHeadersForGET), + ("testGETContentHeadersLeftAlone", testGETContentHeadersLeftAlone), ("testNoContentLengthHeadersForHEAD", testNoContentLengthHeadersForHEAD), ("testNoTransferEncodingHeadersForHEAD", testNoTransferEncodingHeadersForHEAD), ("testNoChunkedEncodingForHTTP10", testNoChunkedEncodingForHTTP10), diff --git a/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest.swift b/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest.swift index cec4c0955..ce9756a76 100644 --- a/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPRequestEncoderTest.swift @@ -44,6 +44,21 @@ class HTTPRequestEncoderTests: XCTestCase { let writtenData = try sendRequest(withMethod: .HEAD, andHeaders: HTTPHeaders()) writtenData.assertContainsOnly("HEAD /uri HTTP/1.1\r\n\r\n") } + + func testNoAutoHeadersForGET() throws { + let writtenData = try sendRequest(withMethod: .GET, andHeaders: HTTPHeaders()) + writtenData.assertContainsOnly("GET /uri HTTP/1.1\r\n\r\n") + } + + func testGETContentHeadersLeftAlone() throws { + var headers = HTTPHeaders([("content-length", "17")]) + var writtenData = try sendRequest(withMethod: .GET, andHeaders: headers) + writtenData.assertContainsOnly("GET /uri HTTP/1.1\r\ncontent-length: 17\r\n\r\n") + + headers = HTTPHeaders([("transfer-encoding", "chunked")]) + writtenData = try sendRequest(withMethod: .GET, andHeaders: headers) + writtenData.assertContainsOnly("GET /uri HTTP/1.1\r\ntransfer-encoding: chunked\r\n\r\n") + } func testNoContentLengthHeadersForHEAD() throws { let headers = HTTPHeaders([("content-length", "0")])