-
Notifications
You must be signed in to change notification settings - Fork 125
Refactor deconstructURL
and scheme
parsing
#504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
fae26db
ef5d726
e4d5351
10c556b
aec9ecb
c63d8fc
a2c9cbb
43a0bb0
d428d68
0e4f36f
b3b2cfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
struct DeconstructedURL { | ||
enum Scheme: String { | ||
case http | ||
case https | ||
case unix | ||
case httpUnix = "http+unix" | ||
case httpsUnix = "https+unix" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this should be namespaced in DeconstructedURL. Would you mind moving this out? |
||
|
||
var scheme: Scheme | ||
var connectionTarget: ConnectionTarget | ||
var uri: String | ||
|
||
init( | ||
scheme: DeconstructedURL.Scheme, | ||
connectionTarget: ConnectionTarget, | ||
uri: String | ||
) { | ||
self.scheme = scheme | ||
self.connectionTarget = connectionTarget | ||
self.uri = uri | ||
} | ||
} | ||
|
||
extension DeconstructedURL { | ||
init(url: URL) throws { | ||
guard let schemeString = url.scheme else { | ||
throw HTTPClientError.emptyScheme | ||
} | ||
guard let scheme = Scheme(rawValue: schemeString.lowercased()) else { | ||
throw HTTPClientError.unsupportedScheme(schemeString) | ||
} | ||
|
||
switch scheme { | ||
case .http, .https: | ||
guard let host = url.host, !host.isEmpty else { | ||
throw HTTPClientError.emptyHost | ||
} | ||
self.init( | ||
scheme: scheme, | ||
connectionTarget: .init(remoteHost: host, port: url.port ?? scheme.defaultPort), | ||
uri: url.uri | ||
) | ||
|
||
case .httpUnix, .httpsUnix: | ||
guard let socketPath = url.host, !socketPath.isEmpty else { | ||
throw HTTPClientError.missingSocketPath | ||
} | ||
self.init( | ||
scheme: scheme, | ||
connectionTarget: .unixSocket(path: socketPath), | ||
uri: url.uri | ||
) | ||
|
||
case .unix: | ||
let socketPath = url.baseURL?.path ?? url.path | ||
let uri = url.baseURL != nil ? url.uri : "/" | ||
guard !socketPath.isEmpty else { | ||
throw HTTPClientError.missingSocketPath | ||
} | ||
self.init( | ||
scheme: scheme, | ||
connectionTarget: .unixSocket(path: socketPath), | ||
uri: uri | ||
) | ||
} | ||
} | ||
} | ||
|
||
extension DeconstructedURL.Scheme { | ||
var useTLS: Bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. He, she, it das s muss mit ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will diverge from the public API on HTTPClient.Request but I’m okay with that |
||
switch self { | ||
case .http, .httpUnix, .unix: | ||
return false | ||
case .https, .httpsUnix: | ||
return true | ||
} | ||
} | ||
|
||
var defaultPort: Int { | ||
self.useTLS ? 443 : 80 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Only import what is actually needed.