Skip to content

Commit 7d86d26

Browse files
authored
Fix crash when generating attributes for streaming body (#232)
1 parent 8ce0366 commit 7d86d26

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Sources/Hummingbird/Middleware/TracingMiddleware.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct HBTracingMiddleware: HBMiddleware {
5555
attributes["http.flavor"] = "\(request.version.major).\(request.version.minor)"
5656
attributes["http.scheme"] = request.uri.scheme?.rawValue
5757
attributes["http.user_agent"] = request.headers.first(name: "user-agent")
58-
attributes["http.request_content_length"] = request.body.buffer?.readableBytes
58+
attributes["http.request_content_length"] = request.headers["content-length"].first.map { Int($0) } ?? nil
5959

6060
attributes["net.host.name"] = request.application.server.configuration.address.host
6161
attributes["net.host.port"] = request.application.server.configuration.address.port
@@ -90,7 +90,9 @@ public struct HBTracingMiddleware: HBMiddleware {
9090
switch response.body {
9191
case .byteBuffer(let buffer):
9292
attributes["http.response_content_length"] = buffer.readableBytes
93-
case .stream, .empty:
93+
case .stream:
94+
attributes["http.response_content_length"] = response.headers["content-length"].first.map { Int($0) } ?? nil
95+
case .empty:
9496
break
9597
}
9698
}

Tests/HummingbirdTests/TracingTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ final class TracingTests: XCTestCase {
7575
try app.XCTStart()
7676
defer { app.XCTStop() }
7777

78-
try app.XCTExecute(uri: "/users", method: .POST, body: ByteBuffer(string: "42")) { response in
78+
try app.XCTExecute(uri: "/users", method: .POST, headers: ["content-length": "2"], body: ByteBuffer(string: "42")) { response in
7979
XCTAssertEqual(response.status, .internalServerError)
8080
}
8181

0 commit comments

Comments
 (0)