assume chunked on a stream with no length (#247)

Motivation:
Streams length parameter is optional to allow cases were stream length is not known in advance, but we do not support this in request validation. This PR aims to address that.

Modifications:
Modifies request validation to default to chunked encoding if body length is zero or to passed in content-length header
Adds a test

Result:
Closes #218
This commit is contained in:
Artem Redkin
2020-06-23 16:42:16 +01:00
committed by GitHub
parent aac4357b65
commit 61a80a2d34
6 changed files with 237 additions and 38 deletions
@@ -18,6 +18,10 @@ import NIOHTTP1
extension HTTPHeaders {
mutating func validate(method: HTTPMethod, body: HTTPClient.Body?) throws {
// validate transfer encoding and content length (https://tools.ietf.org/html/rfc7230#section-3.3.1)
if self.contains(name: "Transfer-Encoding"), self.contains(name: "Content-Length") {
throw HTTPClientError.incompatibleHeaders
}
var transferEncoding: String?
var contentLength: Int?
let encodings = self[canonicalForm: "Transfer-Encoding"].map { $0.lowercased() }
@@ -27,11 +31,11 @@ extension HTTPHeaders {
}
self.remove(name: "Transfer-Encoding")
self.remove(name: "Content-Length")
try self.validateFieldNames()
guard let body = body else {
self.remove(name: "Content-Length")
// if we don't have a body we might not need to send the Content-Length field
// https://tools.ietf.org/html/rfc7230#section-3.3.2
switch method {
@@ -60,11 +64,15 @@ extension HTTPHeaders {
}
if encodings.isEmpty {
guard let length = body.length else {
throw HTTPClientError.contentLengthMissing
if let length = body.length {
self.remove(name: "Content-Length")
contentLength = length
} else if !self.contains(name: "Content-Length") {
transferEncoding = "chunked"
}
contentLength = length
} else {
self.remove(name: "Content-Length")
transferEncoding = encodings.joined(separator: ", ")
if !encodings.contains("chunked") {
guard let length = body.length else {