Skip to content

Commit

Permalink
chore(gateway): idiomatic loop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Mar 18, 2020
1 parent dd8c804 commit 8287c2c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions core/corehttp/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,24 @@ func knownSubdomainDetails(hostname string, knownGateways map[string]config.Gate
// Example: given "dist.ipfs.io.ipns.dweb.link":
// 1. Lookup "link" TLD in knownGateways: negative
// 2. Lookup "dweb.link" in knownGateways: positive
for i := range labels {
start := len(labels) - 1 - i
fqdn := strings.Join(labels[start:], ".")
//
// Stops when we have 2 or fewer labels left as we need at least a
// rootId and a namespace.
for i := len(labels) - 1; i >= 2; i-- {
fqdn := strings.Join(labels[i:], ".")
gw, ok := isKnownHostname(fqdn, knownGateways)
// Are there at least two labels left? (ns, rootID)
if ok && start > 1 {
ns := labels[start-1]
if isSubdomainNamespace(ns) {
// Merge remaining labels (could be a FQDN with DNSLink)
rootID := strings.Join(labels[:start-1], ".")
return gw, fqdn, ns, rootID, true
}
if !ok {
continue
}

ns := labels[i-1]
if !isSubdomainNamespace(ns) {
break
}

// Merge remaining labels (could be a FQDN with DNSLink)
rootID := strings.Join(labels[:i-1], ".")
return gw, fqdn, ns, rootID, true
}
// not a known subdomain gateway
return gw, "", "", "", false
Expand Down

0 comments on commit 8287c2c

Please sign in to comment.