diff options
| author | Burcu Dogan <jbd@google.com> | 2015-10-01 15:01:50 -0400 |
|---|---|---|
| committer | Burcu Dogan <jbd@google.com> | 2015-10-01 20:52:54 +0000 |
| commit | d96a3a2d113909a470d2fd039018e1852a8d72b8 (patch) | |
| tree | 1693ee062148772f1122c83a09e12c6608d95922 /src | |
| parent | f1b25c4ccfdd72745407f846e55399b194a23c90 (diff) | |
| download | go-d96a3a2d113909a470d2fd039018e1852a8d72b8.tar.xz | |
net: make /etc/hosts lookups case-insensitive
The native Go host resolver was behaving differently than libc
and the entries in the /etc/hosts were handled in a case sensitive
way. In order to be compatible with libc's resolver, /etc/hosts
lookups must be case-insensitive.
Fixes #12806.
Change-Id: I3c14001abffadf7458fd1a027c91e6438a87f285
Reviewed-on: https://go-review.googlesource.com/15321
Run-TryBot: Burcu Dogan <jbd@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/net/hosts.go | 14 | ||||
| -rw-r--r-- | src/net/hosts_test.go | 1 | ||||
| -rw-r--r-- | src/net/testdata/hosts | 2 |
3 files changed, 13 insertions, 4 deletions
diff --git a/src/net/hosts.go b/src/net/hosts.go index 27958c7cc5..aba2cea787 100644 --- a/src/net/hosts.go +++ b/src/net/hosts.go @@ -60,9 +60,11 @@ func readHosts() { continue } for i := 1; i < len(f); i++ { - h := f[i] - hs[h] = append(hs[h], addr) - is[addr] = append(is[addr], h) + h := []byte(f[i]) + lowerASCIIBytes(h) + lh := string(h) + hs[lh] = append(hs[lh], addr) + is[addr] = append(is[addr], lh) } } // Update the data cache. @@ -80,7 +82,11 @@ func lookupStaticHost(host string) []string { defer hosts.Unlock() readHosts() if len(hosts.byName) != 0 { - if ips, ok := hosts.byName[host]; ok { + // TODO(jbd,bradfitz): avoid this alloc if host is already all lowercase? + // or linear scan the byName map if it's small enough? + lowerHost := []byte(host) + lowerASCIIBytes(lowerHost) + if ips, ok := hosts.byName[string(lowerHost)]; ok { return ips } } diff --git a/src/net/hosts_test.go b/src/net/hosts_test.go index aca64c38b0..99569cd016 100644 --- a/src/net/hosts_test.go +++ b/src/net/hosts_test.go @@ -74,6 +74,7 @@ var lookupStaticAddrTests = []struct { {"255.255.255.255", []string{"broadcasthost"}}, {"127.0.0.2", []string{"odin"}}, {"127.0.0.3", []string{"odin"}}, + {"127.0.0.4", []string{"bor"}}, {"::2", []string{"odin"}}, {"127.1.1.1", []string{"thor"}}, {"127.1.1.2", []string{"ullr", "ullrhost"}}, diff --git a/src/net/testdata/hosts b/src/net/testdata/hosts index b601763898..4b8abb431c 100644 --- a/src/net/testdata/hosts +++ b/src/net/testdata/hosts @@ -1,6 +1,8 @@ 255.255.255.255 broadcasthost 127.0.0.2 odin 127.0.0.3 odin # inline comment +# case insensitivity +127.0.0.4 Bor ::2 odin 127.1.1.1 thor # aliases |
