Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make options.hosts case insensitive #3653

Merged
merged 1 commit into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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