diff --git a/Sources/Hummingbird/Codable/JSON/JSONCoding.swift b/Sources/Hummingbird/Codable/JSON/JSONCoding.swift index 48af13b3e..a58ae24a1 100644 --- a/Sources/Hummingbird/Codable/JSON/JSONCoding.swift +++ b/Sources/Hummingbird/Codable/JSON/JSONCoding.swift @@ -30,7 +30,7 @@ extension JSONEncoder: ResponseEncoder { status: .ok, headers: [ .contentType: "application/json; charset=utf-8", - .contentLength: String(describing: data.count), + .contentLength: data.count.description, ], body: .init(byteBuffer: buffer) ) diff --git a/Sources/Hummingbird/Codable/URLEncodedForm/URLEncodedForm+Request.swift b/Sources/Hummingbird/Codable/URLEncodedForm/URLEncodedForm+Request.swift index 2ab62afe3..cf3425c31 100644 --- a/Sources/Hummingbird/Codable/URLEncodedForm/URLEncodedForm+Request.swift +++ b/Sources/Hummingbird/Codable/URLEncodedForm/URLEncodedForm+Request.swift @@ -23,7 +23,10 @@ extension URLEncodedFormEncoder: ResponseEncoder { buffer.writeString(string) return Response( status: .ok, - headers: [.contentType: "application/x-www-form-urlencoded"], + headers: [ + .contentType: "application/x-www-form-urlencoded", + .contentLength: buffer.readableBytes.description, + ], body: .init(byteBuffer: buffer) ) } diff --git a/Sources/Hummingbird/Router/ResponseGenerator.swift b/Sources/Hummingbird/Router/ResponseGenerator.swift index 1d3268709..54df093f2 100644 --- a/Sources/Hummingbird/Router/ResponseGenerator.swift +++ b/Sources/Hummingbird/Router/ResponseGenerator.swift @@ -33,7 +33,14 @@ extension String: ResponseGenerator { /// Generate response holding string public func response(from request: Request, context: some BaseRequestContext) -> Response { let buffer = context.allocator.buffer(string: self) - return Response(status: .ok, headers: [.contentType: "text/plain; charset=utf-8"], body: .init(byteBuffer: buffer)) + return Response( + status: .ok, + headers: [ + .contentType: "text/plain; charset=utf-8", + .contentLength: buffer.readableBytes.description, + ], + body: .init(byteBuffer: buffer) + ) } } @@ -42,7 +49,14 @@ extension Substring: ResponseGenerator { /// Generate response holding string public func response(from request: Request, context: some BaseRequestContext) -> Response { let buffer = context.allocator.buffer(substring: self) - return Response(status: .ok, headers: [.contentType: "text/plain; charset=utf-8"], body: .init(byteBuffer: buffer)) + return Response( + status: .ok, + headers: [ + .contentType: "text/plain; charset=utf-8", + .contentLength: buffer.readableBytes.description, + ], + body: .init(byteBuffer: buffer) + ) } } @@ -50,7 +64,14 @@ extension Substring: ResponseGenerator { extension ByteBuffer: ResponseGenerator { /// Generate response holding bytebuffer public func response(from request: Request, context: some BaseRequestContext) -> Response { - Response(status: .ok, headers: [.contentType: "application/octet-stream"], body: .init(byteBuffer: self)) + Response( + status: .ok, + headers: [ + .contentType: "application/octet-stream", + .contentLength: self.readableBytes.description, + ], + body: .init(byteBuffer: self) + ) } } diff --git a/Sources/HummingbirdCore/Response/Response.swift b/Sources/HummingbirdCore/Response/Response.swift index 71eda27a8..0314f32cf 100644 --- a/Sources/HummingbirdCore/Response/Response.swift +++ b/Sources/HummingbirdCore/Response/Response.swift @@ -28,7 +28,7 @@ public struct Response: Sendable { public init(status: HTTPResponse.Status, headers: HTTPFields = .init(), body: ResponseBody = .init()) { self.head = .init(status: status, headerFields: headers) self.body = body - if let contentLength = body.contentLength, headers[.contentLength] == nil { + if let contentLength = body.contentLength, headers[values: .contentLength].count == 0 { self.head.headerFields[.contentLength] = String(describing: contentLength) } }