Skip to content

Commit

Permalink
Make options.hosts case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed Mar 18, 2024
1 parent 9ee0188 commit fe7a778
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/types/hosts.go
Expand Up @@ -97,7 +97,7 @@ type Hosts struct {
// NewHosts returns new Hosts from given addresses.
func NewHosts(source map[string]Host) (*Hosts, error) {
h := &Hosts{
source: source,
source: toLowerKeys(source),
n: &trieNode{
children: make(map[rune]*trieNode),
},
Expand All @@ -113,6 +113,14 @@ func NewHosts(source map[string]Host) (*Hosts, error) {
return h, nil
}

func toLowerKeys(source map[string]Host) map[string]Host {
result := make(map[string]Host, len(source))
for k, v := range source {
result[strings.ToLower(k)] = v
}
return result
}

// Regex description of domain(:port)? pattern to enforce blocks by.
// Global var to avoid compilation penalty at runtime.
// Based on regex from https://stackoverflow.com/a/106223/5427244
Expand Down
3 changes: 3 additions & 0 deletions lib/types/hosts_test.go
Expand Up @@ -49,6 +49,7 @@ func TestHosts(t *testing.T) {
"specific.wildcard.io": {IP: net.ParseIP("90.100.110.120")},
"*wildcard-2.io": {IP: net.ParseIP("13.14.15.16")},
"specific.wildcard-2.io": {IP: net.ParseIP("130.140.150.160")},
"with-UPPER-case.io": {IP: net.ParseIP("17.18.19.20")},

// IPv6
"simple-ipv6.io": {IP: net.ParseIP("aa::bb")},
Expand Down Expand Up @@ -77,6 +78,8 @@ func TestHosts(t *testing.T) {
{"only-port.io", "", NotInHosts},
{"only-port.io:443", "5.6.7.8:8443", DifferentPortMapping},
{"only-port.io:9999", "", NotGivenPortMapping},
{"with-upper-case.io", "17.18.19.20:0", EmptyPortMapping},
{"with-UPPER-case.io", "17.18.19.20:0", EmptyPortMapping},
}
runTcs(t, hosts, tcs)
})
Expand Down

0 comments on commit fe7a778

Please sign in to comment.