mirror of
https://github.com/swift-server/async-http-client.git
synced 2026-05-03 07:32:29 +00:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user