mirror of
https://github.com/apple/swift-nio-extras
synced 2026-11-26 04:57:49 +00:00
Handle absolute-form requests when converting from/to HTTPTypes (#293)
Handle absolute-form requests when converting from/to HTTPTypes. ### Motivation: HTTPTypes represent modern HTTP/2 / HTTP/3 semantics which do not support absolute-form requests. ### Modifications: When converting an absolute-form request to an HTTPRequest, the request target is converted to its path. When converting an HTTPRequest back, the user needs to specify which form to convert to. ### Result: HTTPRequest no longer contains invalid paths when working with cleartext HTTP/1 proxies.
This commit is contained in:
1 parent
c8e77b29ce
commit
d5578e7b77
4 files changed
+153
-11
No files matched your search
@@ -15,6 +15,12 @@
|
||||
import HTTPTypes
|
||||
import NIOHTTP1
|
||||
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
#else
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
public struct HTTP1TypeConversionError: Error, Equatable {
|
||||
private enum Internal {
|
||||
case invalidMethod
|
||||
@@ -160,8 +166,32 @@ extension HTTPFields {
|
||||
|
||||
extension HTTPRequestHead {
|
||||
public init(_ newRequest: HTTPRequest) throws {
|
||||
guard let path = newRequest.method == .connect ? newRequest.authority : newRequest.path else {
|
||||
throw HTTP1TypeConversionError.missingPath
|
||||
try self.init(newRequest, absoluteForm: false)
|
||||
}
|
||||
|
||||
public init(_ newRequest: HTTPRequest, absoluteForm: Bool) throws {
|
||||
let uri: String
|
||||
if newRequest.method == .connect {
|
||||
if let authority = newRequest.authority {
|
||||
uri = authority
|
||||
} else {
|
||||
throw HTTP1TypeConversionError.missingPath
|
||||
}
|
||||
} else if !absoluteForm {
|
||||
if let path = newRequest.path {
|
||||
uri = path
|
||||
} else {
|
||||
throw HTTP1TypeConversionError.missingPath
|
||||
}
|
||||
} else {
|
||||
if let scheme = newRequest.scheme,
|
||||
let authority = newRequest.authority,
|
||||
let path = newRequest.path
|
||||
{
|
||||
uri = "\(scheme)://\(authority)\(path)"
|
||||
} else {
|
||||
throw HTTP1TypeConversionError.missingPath
|
||||
}
|
||||
}
|
||||
var headers = HTTPHeaders()
|
||||
headers.reserveCapacity(newRequest.headerFields.count + 1)
|
||||
@@ -182,22 +212,54 @@ extension HTTPRequestHead {
|
||||
self.init(
|
||||
version: .http1_1,
|
||||
method: HTTPMethod(newRequest.method),
|
||||
uri: path,
|
||||
uri: uri,
|
||||
headers: headers
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension HTTPRequest {
|
||||
private static func requestPath(from oldPath: String) -> String {
|
||||
guard let components = URLComponents(string: oldPath),
|
||||
let urlString = components.string,
|
||||
components.rangeOfScheme != nil
|
||||
else {
|
||||
return oldPath
|
||||
}
|
||||
|
||||
let pathRange = components.rangeOfPath
|
||||
let queryRange = components.rangeOfQuery
|
||||
let requestPathRange: Range<String.Index>?
|
||||
if let lowerBound = pathRange?.lowerBound ?? queryRange?.lowerBound,
|
||||
let upperBound = queryRange?.upperBound ?? pathRange?.upperBound
|
||||
{
|
||||
requestPathRange = lowerBound..<upperBound
|
||||
} else {
|
||||
requestPathRange = nil
|
||||
}
|
||||
let path: String
|
||||
if let pathRange, !pathRange.isEmpty, let requestPathRange {
|
||||
path = String(urlString[requestPathRange])
|
||||
} else {
|
||||
if let requestPathRange, !requestPathRange.isEmpty {
|
||||
path = "/\(urlString[requestPathRange])"
|
||||
} else {
|
||||
path = "/"
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
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
|
||||
let path = Self.requestPath(from: oldRequest.uri)
|
||||
self.init(
|
||||
method: method,
|
||||
scheme: scheme,
|
||||
authority: authority,
|
||||
path: oldRequest.uri,
|
||||
path: path,
|
||||
headerFields: HTTPFields(oldRequest.headers, splitCookie: splitCookie)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user