Skip to content

Commit 8007a5f

Browse files
authored
Update for HB 2.0.0-alpha.2 (#14)
* Update for HB 2.0.0-alpha.2 * Ensure request/response length is set
1 parent 81deeb9 commit 8007a5f

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.0.0"),
16-
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.1"),
16+
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-alpha.2"),
1717
],
1818
targets: [
1919
.target(

Sources/OpenAPIHummingbird/OpenAPITransport.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ extension HBRequest {
4949
/// Construct ``OpenAPIRuntime.Request`` from Hummingbird ``HBRequest``
5050
func makeOpenAPIRequest<Context: HBBaseRequestContext>(context: Context) throws -> (HTTPRequest, HTTPBody?) {
5151
let request = self.head
52-
let body: HTTPBody?
53-
switch self.body {
54-
case .byteBuffer(let buffer):
55-
body = HTTPBody([UInt8](buffer: buffer))
56-
case .stream(let streamer):
57-
body = .init(
58-
streamer.map { [UInt8](buffer: $0) },
59-
length: .unknown,
60-
iterationBehavior: .single
61-
)
52+
// extract length from content-length header
53+
let length = if let contentLengthHeader = self.headers[.contentLength], let contentLength = Int(contentLengthHeader) {
54+
HTTPBody.Length.known(numericCast(contentLength))
55+
} else {
56+
HTTPBody.Length.unknown
6257
}
58+
let body = HTTPBody(
59+
self.body.map { [UInt8](buffer: $0) },
60+
length: length,
61+
iterationBehavior: .single
62+
)
6363
return (request, body)
6464
}
6565
}
@@ -80,7 +80,15 @@ extension HBResponse {
8080
let responseBody: HBResponseBody
8181
if let body = body {
8282
let bufferSequence = body.map { ByteBuffer(bytes: $0)}
83-
responseBody = .init(asyncSequence: bufferSequence)
83+
if case .known(let length) = body.length {
84+
responseBody = .init(contentLength: numericCast(length)) { writer in
85+
for try await buffer in bufferSequence {
86+
try await writer.write(buffer)
87+
}
88+
}
89+
} else {
90+
responseBody = .init(asyncSequence: bufferSequence)
91+
}
8492
} else {
8593
responseBody = .init(byteBuffer: ByteBuffer())
8694
}

Tests/OpenAPIHummingbirdTests/OpenAPITransportTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ final class HBOpenAPITransportTests: XCTestCase {
7979
// Check the HBResponse (created from the Response) is what meets expectations.
8080
XCTAssertEqual(hbResponse.status, .created)
8181
XCTAssertEqual(hbResponse.headers[.xMumble], "mumble")
82+
XCTAssertEqual(hbResponse.headers[.contentLength], "👋".utf8.count.description)
8283
XCTAssertEqual(try String(buffer: XCTUnwrap(hbResponse.body)), "👋")
8384
}
8485
}

0 commit comments

Comments
 (0)