Skip to content

Commit

Permalink
Add content-length to response generator body init
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed May 19, 2024
1 parent cc5760a commit 99ff126
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Codable/JSON/JSONCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
Expand Down
27 changes: 24 additions & 3 deletions Sources/Hummingbird/Router/ResponseGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}

Expand All @@ -42,15 +49,29 @@ 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)
)
}
}

/// Extend ByteBuffer to conform to 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)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdCore/Response/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 99ff126

Please sign in to comment.