0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Add global FallbackHosts for vhost matching

This commit is contained in:
Sergey Frolov 2017-07-25 16:10:51 -04:00
parent ea245b5af5
commit c8307409c9

View file

@ -45,6 +45,12 @@ func (t *vhostTrie) insertPath(remainingPath, originalPath string, site *SiteCon
t.edges[ch].insertPath(remainingPath[1:], originalPath, site) 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 // Match returns the virtual host (site) in v with
// the closest match to key. If there was a match, // the closest match to key. If there was a match,
// it returns the SiteConfig and the path portion of // it returns the SiteConfig and the path portion of
@ -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". // A typical key will be in the form "host" or "host/path".
func (t *vhostTrie) Match(key string) (*SiteConfig, string) { func (t *vhostTrie) Match(key string) (*SiteConfig, string) {
host, path := t.splitHostPath(key) 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) branch := t.matchHost(host)
if branch == nil { for _, h := range FallbackHosts {
branch = t.matchHost("0.0.0.0") if branch != nil {
break
} }
if branch == nil { branch = t.matchHost(h)
branch = t.matchHost("")
} }
if branch == nil { if branch == nil {
return nil, "" return nil, ""