Skip to content

Commit

Permalink
Merge pull request #1781 from mholt/global-fallback-hosts
Browse files Browse the repository at this point in the history
httpserver: Add global FallbackHosts for vhost matching
  • Loading branch information
mholt committed Jul 26, 2017
2 parents 76a2827 + c830740 commit 4991d70
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions caddyhttp/httpserver/vhosttrie.go
Expand Up @@ -45,6 +45,12 @@ func (t *vhostTrie) insertPath(remainingPath, originalPath string, site *SiteCon
t.edges[ch].insertPath(remainingPath[1:], originalPath, site)
}

// When matching a site, the given host will be tried first.
// Then, FallbackHosts will be tried in order.
// Default FallbackHosts are following wildcards,
// which could be modified by plugins and directives.
var FallbackHosts = []string{"0.0.0.0", ""}

// Match returns the virtual host (site) in v with
// the closest match to key. If there was a match,
// it returns the SiteConfig and the path portion of
Expand All @@ -57,13 +63,13 @@ func (t *vhostTrie) insertPath(remainingPath, originalPath string, site *SiteCon
// A typical key will be in the form "host" or "host/path".
func (t *vhostTrie) Match(key string) (*SiteConfig, string) {
host, path := t.splitHostPath(key)
// try the given host, then, if no match, try wildcard hosts
// try the given host, then, if no match, try fallback hosts
branch := t.matchHost(host)
if branch == nil {
branch = t.matchHost("0.0.0.0")
}
if branch == nil {
branch = t.matchHost("")
for _, h := range FallbackHosts {
if branch != nil {
break
}
branch = t.matchHost(h)
}
if branch == nil {
return nil, ""
Expand Down

0 comments on commit 4991d70

Please sign in to comment.