Skip to content

Commit

Permalink
refactor: cleanup/comment
Browse files Browse the repository at this point in the history
ipfs/kubo#7319 (comment)

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>


This commit was moved from ipfs/kubo@6b6569f
  • Loading branch information
lidel committed Aug 19, 2020
1 parent c0b7f3f commit c37f055
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions gateway/core/corehttp/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ func HostnameOption() ServeOption {
// HTTP Host check: is this one of our subdomain-based "known gateways"?
// Example: {cid}.ipfs.localhost, {cid}.ipfs.dweb.link
if gw, hostname, ns, rootID, ok := knownSubdomainDetails(host, knownGateways); ok {
// Looks like we're using known subdomain gateway.
// Looks like we're using a known gateway in subdomain mode.

// Assemble original path prefix.
pathPrefix := "/" + ns + "/" + rootID

// Does this gateway _handle_ this path?
// Does this gateway _handle_ subdomains AND this path?
if !(gw.UseSubdomains && hasPrefix(pathPrefix, gw.Paths...)) {
// If not, resource does not exist, return 404
http.NotFound(w, r)
Expand Down Expand Up @@ -290,10 +290,10 @@ func isKnownHostname(hostname string, knownGateways gatewayHosts) (gw *config.Ga
}
}

return gw, ok
return nil, false
}

// Parses Host header and looks for a known subdomain gateway host.
// Parses Host header and looks for a known gateway matching subdomain host.
// If found, returns GatewaySpec and subdomain components.
// Note: hostname is host + optional port
func knownSubdomainDetails(hostname string, knownGateways gatewayHosts) (gw *config.GatewaySpec, knownHostname, ns, rootID string, ok bool) {
Expand Down Expand Up @@ -321,8 +321,8 @@ func knownSubdomainDetails(hostname string, knownGateways gatewayHosts) (gw *con
rootID := strings.Join(labels[:i-1], ".")
return gw, fqdn, ns, rootID, true
}
// not a known subdomain gateway
return gw, "", "", "", false
// no match
return nil, "", "", "", false
}

// isDNSLinkRequest returns bool that indicates if request
Expand Down
10 changes: 5 additions & 5 deletions gateway/core/corehttp/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func TestDNSPrefix(t *testing.T) {
}

func TestKnownSubdomainDetails(t *testing.T) {
gwLocalhost := &config.GatewaySpec{}
gwDweb := &config.GatewaySpec{}
gwLong := &config.GatewaySpec{}
gwWildcard1 := &config.GatewaySpec{}
gwWildcard2 := &config.GatewaySpec{}
gwLocalhost := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
gwDweb := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
gwLong := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
gwWildcard1 := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
gwWildcard2 := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}

knownGateways := prepareKnownGateways(map[string]*config.GatewaySpec{
"localhost": gwLocalhost,
Expand Down

0 comments on commit c37f055

Please sign in to comment.