From 8d2768e4d9c4eb53211fdf7a6b9676e89c746ac7 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 19 May 2024 12:32:58 +0100 Subject: [PATCH] Use HTTPFields.contains Doesn't construct http field contents --- Sources/Hummingbird/Middleware/CORSMiddleware.swift | 2 +- Sources/Hummingbird/Router/ResponseGenerator.swift | 2 +- Sources/HummingbirdCore/Response/Response.swift | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Hummingbird/Middleware/CORSMiddleware.swift b/Sources/Hummingbird/Middleware/CORSMiddleware.swift index f90d81324..7d58cc9a5 100644 --- a/Sources/Hummingbird/Middleware/CORSMiddleware.swift +++ b/Sources/Hummingbird/Middleware/CORSMiddleware.swift @@ -86,7 +86,7 @@ public struct CORSMiddleware: RouterMiddleware { /// apply CORS middleware public func handle(_ request: Request, context: Context, next: (Request, Context) async throws -> Response) async throws -> Response { // if no origin header then don't apply CORS - guard request.headers[.origin] != nil else { + guard request.headers.contains(.origin) else { return try await next(request, context) } diff --git a/Sources/Hummingbird/Router/ResponseGenerator.swift b/Sources/Hummingbird/Router/ResponseGenerator.swift index 54df093f2..933d654d1 100644 --- a/Sources/Hummingbird/Router/ResponseGenerator.swift +++ b/Sources/Hummingbird/Router/ResponseGenerator.swift @@ -119,7 +119,7 @@ public struct EditedResponse: ResponseGenerator { // only add headers from generated response if they don't exist in override headers var headers = self.headers for header in response.headers { - if headers[header.name] == nil { + if !headers.contains(header.name) { headers.append(header) } } diff --git a/Sources/HummingbirdCore/Response/Response.swift b/Sources/HummingbirdCore/Response/Response.swift index 0314f32cf..d01f571e9 100644 --- a/Sources/HummingbirdCore/Response/Response.swift +++ b/Sources/HummingbirdCore/Response/Response.swift @@ -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[values: .contentLength].count == 0 { + if let contentLength = body.contentLength, !headers.contains(.contentLength) { self.head.headerFields[.contentLength] = String(describing: contentLength) } }