Skip to content

Commit

Permalink
Merge pull request #18 from swift-server/nio-http-types
Browse files Browse the repository at this point in the history
Migrate to NIOExtras HTTP Types Conversion
  • Loading branch information
0xTim authored Apr 9, 2024
2 parents 0ae9850 + 092e8ce commit b53c151
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 52 deletions.
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ 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(
name: "OpenAPIVapor",
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
),
Expand Down
52 changes: 2 additions & 50 deletions Sources/OpenAPIVapor/VaporTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import HTTPTypes
import Vapor
import NIOFoundationCompat
import Atomics
import NIOHTTPTypesHTTP1

public final class VaporTransport {

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit b53c151

Please sign in to comment.