Skip to content

Commit dc40e56

Browse files
Improved parsing of subdomains so IP-like domains get properly parsed
1 parent 617adc9 commit dc40e56

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Sources/HostRouter/HostComponent.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ extension HostComponent: CustomStringConvertible {
3838

3939
extension StringProtocol {
4040
/// Converts a host (either a domain or an IP address) into a reversed collection of ``HostComponent``s.
41+
@inlinable
4142
public var reversedDomainComponents: [HostComponent] {
4243
guard !self.isIPAddress else { return [.constant(String(self))] }
43-
return self.split(separator: ".").reversed().map { .init(stringLiteral: $0.lowercased()) }
44+
return reversedSubDomainComponents
45+
}
46+
47+
/// Converts a subdomain into a reversed collection of ``HostComponent``s.
48+
@inlinable
49+
public var reversedSubDomainComponents: [HostComponent] {
50+
self.split(separator: ".").reversed().map { .init(stringLiteral: $0.lowercased()) }
4451
}
4552

4653
/// Retrieve the port and domain from the receiving Host string.
54+
@usableFromInline
4755
var hostComponents: (port: String?, reverseDomain: [String]) {
4856
let baseComponents = self.split(separator: ":")
4957
let (host, port) = baseComponents.count == 2 ? (String(baseComponents[0]), String(baseComponents[1])) : (String(self), nil)
@@ -52,7 +60,8 @@ extension StringProtocol {
5260
}
5361

5462
/// Check if the string is an IP address.
55-
private var isIPAddress: Bool {
63+
@usableFromInline
64+
var isIPAddress: Bool {
5665
// TODO: This was unused in Vapor, and NIO may have a better helper for this.
5766

5867
/// We need some scratch space to let inet_pton write into.

Sources/HostRouter/HostRoutesGroup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension TopLevelHostRoutesBuilder {
4040

4141
extension HostRoutesBuilder {
4242
public func grouped(subDomain: some StringProtocol) -> some HostRoutesBuilder {
43-
HostRoutesGroup(root: self, port: nil, domainPath: subDomain.reversedDomainComponents)
43+
HostRoutesGroup(root: self, port: nil, domainPath: subDomain.reversedSubDomainComponents)
4444
}
4545

4646
public func grouped(reverseDomain: HostComponent...) -> some HostRoutesBuilder {
@@ -52,7 +52,7 @@ extension HostRoutesBuilder {
5252
}
5353

5454
public func group(subDomain: some StringProtocol, configure: (HostRoutesBuilder) throws -> ()) rethrows {
55-
try configure(HostRoutesGroup(root: self, port: nil, domainPath: subDomain.reversedDomainComponents))
55+
try configure(HostRoutesGroup(root: self, port: nil, domainPath: subDomain.reversedSubDomainComponents))
5656
}
5757

5858
public func group(reverseDomain: HostComponent..., configure: (HostRoutesBuilder) throws -> ()) rethrows {

0 commit comments

Comments
 (0)