From a3b640d7dc567225db7c94386a6e71aded1bfa63 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Mon, 12 Feb 2024 10:25:12 +0000 Subject: [PATCH] Make HTTP type conversion code public (#216) * Make HTTP1 type conversion code public * Convert HTTP1TypeConversionError to struct --------- Co-authored-by: Franz Busch --- .../HTTPTypeConversion.swift | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/Sources/NIOHTTPTypesHTTP1/HTTPTypeConversion.swift b/Sources/NIOHTTPTypesHTTP1/HTTPTypeConversion.swift index f8e2c4b7..1c8007a3 100644 --- a/Sources/NIOHTTPTypesHTTP1/HTTPTypeConversion.swift +++ b/Sources/NIOHTTPTypesHTTP1/HTTPTypeConversion.swift @@ -15,14 +15,27 @@ import HTTPTypes import NIOHTTP1 -private enum HTTP1TypeConversionError: Error { - case invalidMethod - case missingPath - case invalidStatusCode +public struct HTTP1TypeConversionError: Error, Equatable { + private enum Internal { + case invalidMethod + case missingPath + case invalidStatusCode + } + private let value: Internal + private init(_ value: Internal) { + self.value = value + } + + /// Failed to create HTTPRequest.Method from HTTPMethod + public static var invalidMethod: Self { .init(.invalidMethod)} + /// Failed to extract a path from HTTPRequest + public static var missingPath: Self { .init(.missingPath)} + /// HTTPResponseHead had an invalid status code + public static var invalidStatusCode: Self { .init(.invalidStatusCode)} } extension HTTPMethod { - init(_ newMethod: HTTPRequest.Method) { + public init(_ newMethod: HTTPRequest.Method) { switch newMethod { case .get: self = .GET case .head: self = .HEAD @@ -68,7 +81,7 @@ extension HTTPMethod { } extension HTTPRequest.Method { - init(_ oldMethod: HTTPMethod) throws { + public init(_ oldMethod: HTTPMethod) throws { switch oldMethod { case .GET: self = .get case .PUT: self = .put @@ -114,14 +127,14 @@ extension HTTPRequest.Method { } extension HTTPHeaders { - init(_ newFields: HTTPFields) { + public init(_ newFields: HTTPFields) { let fields = newFields.map { ($0.name.rawName, $0.value) } self.init(fields) } } extension HTTPFields { - init(_ oldHeaders: HTTPHeaders, splitCookie: Bool) { + public init(_ oldHeaders: HTTPHeaders, splitCookie: Bool) { self.init() self.reserveCapacity(count) var firstHost = true @@ -144,7 +157,7 @@ extension HTTPFields { } extension HTTPRequestHead { - init(_ newRequest: HTTPRequest) throws { + public init(_ newRequest: HTTPRequest) throws { guard let path = newRequest.method == .connect ? newRequest.authority : newRequest.path else { throw HTTP1TypeConversionError.missingPath } @@ -174,7 +187,7 @@ extension HTTPRequestHead { } extension HTTPRequest { - init(_ oldRequest: HTTPRequestHead, secure: Bool, splitCookie: Bool) throws { + public init(_ oldRequest: HTTPRequestHead, secure: Bool, splitCookie: Bool) throws { let method = try Method(oldRequest.method) let scheme = secure ? "https" : "http" let authority = oldRequest.headers["Host"].first @@ -189,7 +202,7 @@ extension HTTPRequest { } extension HTTPResponseHead { - init(_ newResponse: HTTPResponse) { + public init(_ newResponse: HTTPResponse) { self.init( version: .http1_1, status: HTTPResponseStatus( @@ -202,7 +215,7 @@ extension HTTPResponseHead { } extension HTTPResponse { - init(_ oldResponse: HTTPResponseHead) throws { + public init(_ oldResponse: HTTPResponseHead) throws { guard oldResponse.status.code <= 999 else { throw HTTP1TypeConversionError.invalidStatusCode }