From 092e8ce37bd007a8179e8da1b5d7cfac3a4b9f05 Mon Sep 17 00:00:00 2001 From: Tim <0xtimc@gmail.com> Date: Tue, 9 Apr 2024 12:43:21 +0100 Subject: [PATCH] Migrate to using SwiftNIO Extras to convert between NIOTypes and Swift HTTP Types --- Package.swift | 6 ++- Sources/OpenAPIVapor/VaporTransport.swift | 52 +---------------------- 2 files changed, 6 insertions(+), 52 deletions(-) diff --git a/Package.swift b/Package.swift index e957628..2af7f7a 100644 --- a/Package.swift +++ b/Package.swift @@ -26,7 +26,8 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.0.0"), .package(url: "https://github.com/vapor/vapor.git", from: "4.91.1"), - .package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0") + .package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"), + .package(url: "https://github.com/apple/swift-nio-extras", from: "1.22.0"), ], targets: [ .target( @@ -34,7 +35,8 @@ let package = Package( dependencies: [ .product(name: "Vapor", package: "vapor"), .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), - .product(name: "Atomics", package: "swift-atomics") + .product(name: "Atomics", package: "swift-atomics"), + .product(name: "NIOHTTPTypesHTTP1", package: "swift-nio-extras"), ], swiftSettings: swiftSettings ), diff --git a/Sources/OpenAPIVapor/VaporTransport.swift b/Sources/OpenAPIVapor/VaporTransport.swift index 7916202..bb5c702 100644 --- a/Sources/OpenAPIVapor/VaporTransport.swift +++ b/Sources/OpenAPIVapor/VaporTransport.swift @@ -18,6 +18,7 @@ import HTTPTypes import Vapor import NIOFoundationCompat import Atomics +import NIOHTTPTypesHTTP1 public final class VaporTransport { @@ -84,7 +85,7 @@ extension [Vapor.PathComponent] { extension HTTPTypes.HTTPRequest { init(_ vaporRequest: Vapor.Request) throws { - let headerFields: HTTPTypes.HTTPFields = .init(vaporRequest.headers) + let headerFields: HTTPTypes.HTTPFields = .init(vaporRequest.headers, splitCookie: true) let method = try HTTPTypes.HTTPRequest.Method(vaporRequest.method) let queries = vaporRequest.url.query.map { "?\($0)" } ?? "" self.init( @@ -181,52 +182,3 @@ extension Vapor.Response.Body { } } } - -extension HTTPTypes.HTTPFields { - init(_ headers: NIOHTTP1.HTTPHeaders) { - self.init(headers.compactMap { name, value in - guard let name = HTTPField.Name(name) else { - return nil - } - return HTTPField(name: name, value: value) - }) - } -} - -extension NIOHTTP1.HTTPHeaders { - init(_ headers: HTTPTypes.HTTPFields) { - self.init(headers.map { ($0.name.rawName, $0.value) }) - } -} - -extension HTTPTypes.HTTPRequest.Method { - init(_ method: NIOHTTP1.HTTPMethod) throws { - switch method { - case .GET: self = .get - case .PUT: self = .put - case .POST: self = .post - case .DELETE: self = .delete - case .OPTIONS: self = .options - case .HEAD: self = .head - case .PATCH: self = .patch - case .TRACE: self = .trace - default: throw VaporTransportError.unsupportedHTTPMethod(method.rawValue) - } - } -} - -extension NIOHTTP1.HTTPMethod { - init(_ method: HTTPTypes.HTTPRequest.Method) { - switch method { - case .get: self = .GET - case .put: self = .PUT - case .post: self = .POST - case .delete: self = .DELETE - case .options: self = .OPTIONS - case .head: self = .HEAD - case .patch: self = .PATCH - case .trace: self = .TRACE - default: self = .RAW(value: method.rawValue) - } - } -}