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

add support for wildcards in allowed_hosts, add tests for allowsHost() #73

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/flashmob/go-guerrilla/envelope"
"github.com/flashmob/go-guerrilla/log"
"github.com/flashmob/go-guerrilla/response"
"path/filepath"
)

const (
Expand Down Expand Up @@ -244,6 +245,18 @@ func (server *server) allowsHost(host string) bool {
if _, ok := server.hosts.table[strings.ToLower(host)]; ok {
return true
}
if _, ok := server.hosts.table["*"]; ok {
return true
}
for pattern := range server.hosts.table {
matched, err := filepath.Match(pattern, host)
if err != nil {
return false
}
if matched {
return true
}
}
return false
}

Expand Down
37 changes: 37 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ func TestHandleClient(t *testing.T) {
wg.Wait() // wait for handleClient to exit
}

func TestAllowsHosts(t *testing.T) {
s := server{}
allowedHosts := []string{
"spam4.me",
"grr.la",
"newhost.com",
"example.*",
"*.test",
"wild*.card",
"multiple*wild*cards.*",
}
s.setAllowedHosts(allowedHosts)

testTable := map[string]bool{
"spam4.me": true,
"dont.match": false,
"example.com": true,
"another.example.com": false,
"anything.test": true,
"wild.card": true,
"wild.card.com": false,
"multipleXwildXcards.com": true,
}

for host, allows := range testTable {
if res := s.allowsHost(host); res != allows {
t.Error(host, ": expected", allows, "but got", res)
}
}

// only wildcard - should match anything
s.setAllowedHosts([]string{"*"})
if !s.allowsHost("match.me") {
t.Error("match.me: expected true but got false")
}
}

// TODO
// - test github issue #44 and #42
// - test other commands
Expand Down