refactor: cleanup/comment
https://github.com/ipfs/go-ipfs/pull/7319#discussion_r472734905 License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
Marcin Rataj committed
Aug 19, 2020 at 15:59 UTC
6b6569f3e5c1cb2e229b98c26c88ffa34f0abe81
2 files changed
+11
-11
core/corehttp/hostname.go
+6
-6
@@ -141,12 +141,12 @@ func HostnameOption() ServeOption {
141
// HTTP Host check: is this one of our subdomain-based "known gateways"?
142
// Example: {cid}.ipfs.localhost, {cid}.ipfs.dweb.link
143
if gw, hostname, ns, rootID, ok := knownSubdomainDetails(host, knownGateways); ok {
144
- // Looks like we're using known subdomain gateway.
144
+ // Looks like we're using a known gateway in subdomain mode.
145
146
// Assemble original path prefix.
147
pathPrefix := "/" + ns + "/" + rootID
148
149
- // Does this gateway _handle_ this path?
149
+ // Does this gateway _handle_ subdomains AND this path?
150
if !(gw.UseSubdomains && hasPrefix(pathPrefix, gw.Paths...)) {
151
// If not, resource does not exist, return 404
152
http.NotFound(w, r)
@@ -290,10 +290,10 @@ func isKnownHostname(hostname string, knownGateways gatewayHosts) (gw *config.Ga
290
}
291
}
292
293
- return gw, ok
293
+ return nil, false
294
}
295
296
-// Parses Host header and looks for a known subdomain gateway host.
296
+// Parses Host header and looks for a known gateway matching subdomain host.
297
// If found, returns GatewaySpec and subdomain components.
298
// Note: hostname is host + optional port
299
func knownSubdomainDetails(hostname string, knownGateways gatewayHosts) (gw *config.GatewaySpec, knownHostname, ns, rootID string, ok bool) {
@@ -321,8 +321,8 @@ func knownSubdomainDetails(hostname string, knownGateways gatewayHosts) (gw *con
321
rootID := strings.Join(labels[:i-1], ".")
322
return gw, fqdn, ns, rootID, true
323
}
324
- // not a known subdomain gateway
325
- return gw, "", "", "", false
324
+ // no match
325
+ return nil, "", "", "", false
326
}
327
328
// isDNSLinkRequest returns bool that indicates if request
core/corehttp/hostname_test.go
+5
-5
@@ -105,11 +105,11 @@ func TestDNSPrefix(t *testing.T) {
105
}
106
107
func TestKnownSubdomainDetails(t *testing.T) {
108
- gwLocalhost := &config.GatewaySpec{}
109
- gwDweb := &config.GatewaySpec{}
110
- gwLong := &config.GatewaySpec{}
111
- gwWildcard1 := &config.GatewaySpec{}
112
- gwWildcard2 := &config.GatewaySpec{}
108
+ gwLocalhost := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
109
+ gwDweb := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
110
+ gwLong := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
111
+ gwWildcard1 := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
112
+ gwWildcard2 := &config.GatewaySpec{Paths: []string{"/ipfs", "/ipns", "/api"}, UseSubdomains: true}
113
114
knownGateways := prepareKnownGateways(map[string]*config.GatewaySpec{
115
"localhost": gwLocalhost,