diff --git a/Sources/Hummingbird/Router/EndpointResponder.swift b/Sources/Hummingbird/Router/EndpointResponder.swift index 87583c59f..b7495e069 100644 --- a/Sources/Hummingbird/Router/EndpointResponder.swift +++ b/Sources/Hummingbird/Router/EndpointResponder.swift @@ -35,9 +35,8 @@ struct EndpointResponders: Sendable { mutating func autoGenerateHeadEndpoint() { if self.methods[.head] == nil, let get = methods[.get] { self.methods[.head] = CallbackResponder { request, context in - var response = try await get.respond(to: request, context: context) - response.body = .init() - return response + let response = try await get.respond(to: request, context: context) + return response.createHeadResponse() } } } diff --git a/Sources/HummingbirdCore/Response/Response.swift b/Sources/HummingbirdCore/Response/Response.swift index f6d143576..71eda27a8 100644 --- a/Sources/HummingbirdCore/Response/Response.swift +++ b/Sources/HummingbirdCore/Response/Response.swift @@ -17,7 +17,13 @@ import HTTPTypes /// Holds all the required to generate a HTTP Response public struct Response: Sendable { public var head: HTTPResponse - public var body: ResponseBody + public var body: ResponseBody { + didSet { + if let contentLength = body.contentLength { + self.head.headerFields[.contentLength] = String(describing: contentLength) + } + } + } public init(status: HTTPResponse.Status, headers: HTTPFields = .init(), body: ResponseBody = .init()) { self.head = .init(status: status, headerFields: headers) @@ -36,6 +42,11 @@ public struct Response: Sendable { get { self.head.headerFields } set { self.head.headerFields = newValue } } + + /// Return HEAD response based off this response + public func createHeadResponse() -> Response { + .init(status: self.status, headers: self.headers, body: .init()) + } } extension Response: CustomStringConvertible {