Skip to content

Commit

Permalink
Don't format headers
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed May 4, 2024
1 parent d1bdac4 commit aea7a11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
36 changes: 14 additions & 22 deletions Sources/Hummingbird/Middleware/LogRequestMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public struct LogRequestsMiddleware<Context: BaseRequestContext>: RouterMiddlewa
metadata: [
"hb_uri": .stringConvertible(request.uri),
"hb_method": .string(request.method.rawValue),
"hb_headers": .string(self.allHeaders(headers: request.headers, except: except)),
"hb_headers": .stringConvertible(self.allHeaders(headers: request.headers, except: except)),
]
)
case .some(let filter):
Expand All @@ -79,44 +79,36 @@ public struct LogRequestsMiddleware<Context: BaseRequestContext>: RouterMiddlewa
metadata: [
"hb_uri": .stringConvertible(request.uri),
"hb_method": .string(request.method.rawValue),
"hb_headers": .string(self.filterHeaders(headers: request.headers, filter: filter)),
"hb_headers": .stringConvertible(self.filterHeaders(headers: request.headers, filter: filter)),
]
)
}
return try await next(request, context)
}

func filterHeaders(headers: HTTPFields, filter: [HTTPField.Name]) -> String {
let headerString = filter
.compactMap { entry in
func filterHeaders(headers: HTTPFields, filter: [HTTPField.Name]) -> [String: String] {
let headers = filter
.compactMap { entry -> (key: String, value: String)? in
guard let value = headers[entry] else { return nil }
if self.redactHeaders.contains(entry) {
return "\"\(entry.canonicalName)\":\"***\""
return (key: entry.canonicalName, value: "***")
} else {
return "\"\(entry.canonicalName)\":\"\(value)\""
return (key: entry.canonicalName, value: value)
}
}
.joined(separator: ", ")
return "{\(headerString)}"
return .init(headers) { "\($0),\($1)" }
}

func allHeaders(headers: HTTPFields, except: [HTTPField.Name]) -> String {
let headerString = headers
.compactMap { entry -> String? in
func allHeaders(headers: HTTPFields, except: [HTTPField.Name]) -> [String: String] {
let headers = headers
.compactMap { entry -> (key: String, value: String)? in
guard except.first(where: { entry.name == $0 }) == nil else { return nil }
if self.redactHeaders.contains(entry.name) {
return "\"\(entry.name.canonicalName)\":\"***\""
return (key: entry.name.canonicalName, value: "***")
} else {
return "\"\(entry.name.canonicalName)\":\"\(entry.value)\""
return (key: entry.name.canonicalName, value: entry.value)
}
}
.joined(separator: ", ")
return "{\(headerString)}"
}
}

extension HTTPFields {
private func logOutput(redacted: [HTTPField.Name]) -> String {
"{\(self.map { "\"\($0.name.canonicalName)\":\"\(redacted.contains($0.name) ? "***" : $0.value)\"" }.joined(separator: ", "))}"
return .init(headers) { "\($0),\($1)" }
}
}
16 changes: 10 additions & 6 deletions Tests/HummingbirdTests/MiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ final class MiddlewareTests: XCTestCase {
body: .init(string: "{}")
) { _ in
let logEntries = logAccumalator.filter { $0.metadata?["hb_uri"]?.description == "/some" }
XCTAssertEqual(logEntries.first?.metadata?["hb_headers"]?.description, #"{"content-type":"application/json"}"#)
XCTAssertEqual(logEntries.first?.metadata?["hb_headers"], .stringConvertible(["content-type": "application/json"]))
}
try await client.execute(
uri: "/none",
Expand All @@ -305,8 +305,10 @@ final class MiddlewareTests: XCTestCase {
body: .init(string: "{}")
) { _ in
let logEntries = logAccumalator.filter { $0.metadata?["hb_uri"]?.description == "/all" }
let reportedHeadersString = try XCTUnwrap(logEntries.first?.metadata?["hb_headers"]?.description)
let reportedHeaders = try JSONDecoder().decode([String: String].self, from: Data(reportedHeadersString.utf8))
guard case .stringConvertible(let headers) = logEntries.first?.metadata?["hb_headers"] else {
fatalError("Should never get here")
}
let reportedHeaders = try XCTUnwrap(headers as? [String: String])
XCTAssertEqual(reportedHeaders["content-type"], "application/json")
XCTAssertEqual(reportedHeaders["content-length"], "2")
XCTAssertNil(reportedHeaders["connection"])
Expand Down Expand Up @@ -337,7 +339,7 @@ final class MiddlewareTests: XCTestCase {
body: .init(string: "{}")
) { _ in
let logEntries = logAccumalator.filter { $0.metadata?["hb_uri"]?.description == "/some" }
XCTAssertEqual(logEntries.first?.metadata?["hb_headers"]?.description, #"{"authorization":"***"}"#)
XCTAssertEqual(logEntries.first?.metadata?["hb_headers"], .stringConvertible(["authorization": "***"]))
}
try await client.execute(
uri: "/all",
Expand All @@ -346,8 +348,10 @@ final class MiddlewareTests: XCTestCase {
body: .init(string: "{}")
) { _ in
let logEntries = logAccumalator.filter { $0.metadata?["hb_uri"]?.description == "/all" }
let reportedHeadersString = try XCTUnwrap(logEntries.first?.metadata?["hb_headers"]?.description)
let reportedHeaders = try JSONDecoder().decode([String: String].self, from: Data(reportedHeadersString.utf8))
guard case .stringConvertible(let headers) = logEntries.first?.metadata?["hb_headers"] else {
fatalError("Should never get here")
}
let reportedHeaders = try XCTUnwrap(headers as? [String: String])
XCTAssertEqual(reportedHeaders["authorization"], "***")
XCTAssertEqual(reportedHeaders["content-length"], "2")
}
Expand Down

0 comments on commit aea7a11

Please sign in to comment.