Skip to content

Commit

Permalink
Fix crash when generating attributes for streaming body (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Sep 4, 2023
1 parent 8ce0366 commit 7d86d26
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Sources/Hummingbird/Middleware/TracingMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct HBTracingMiddleware: HBMiddleware {
attributes["http.flavor"] = "\(request.version.major).\(request.version.minor)"
attributes["http.scheme"] = request.uri.scheme?.rawValue
attributes["http.user_agent"] = request.headers.first(name: "user-agent")
attributes["http.request_content_length"] = request.body.buffer?.readableBytes
attributes["http.request_content_length"] = request.headers["content-length"].first.map { Int($0) } ?? nil

attributes["net.host.name"] = request.application.server.configuration.address.host
attributes["net.host.port"] = request.application.server.configuration.address.port
Expand Down Expand Up @@ -90,7 +90,9 @@ public struct HBTracingMiddleware: HBMiddleware {
switch response.body {
case .byteBuffer(let buffer):
attributes["http.response_content_length"] = buffer.readableBytes
case .stream, .empty:
case .stream:
attributes["http.response_content_length"] = response.headers["content-length"].first.map { Int($0) } ?? nil
case .empty:
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdTests/TracingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class TracingTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

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

Expand Down

0 comments on commit 7d86d26

Please sign in to comment.