Skip to content

Commit

Permalink
Make HTTP type conversion code public (#216)
Browse files Browse the repository at this point in the history
* Make HTTP1 type conversion code public

* Convert HTTP1TypeConversionError to struct

---------

Co-authored-by: Franz Busch <[email protected]>
  • Loading branch information
adam-fowler and FranzBusch authored Feb 12, 2024
1 parent cdd1580 commit a3b640d
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions Sources/NIOHTTPTypesHTTP1/HTTPTypeConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -189,7 +202,7 @@ extension HTTPRequest {
}

extension HTTPResponseHead {
init(_ newResponse: HTTPResponse) {
public init(_ newResponse: HTTPResponse) {
self.init(
version: .http1_1,
status: HTTPResponseStatus(
Expand All @@ -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
}
Expand Down

0 comments on commit a3b640d

Please sign in to comment.