Commit 32ec39cd authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1784 from mholt/trie-fallbacks

Move fallbackHosts to vhostTrie
parents 4991d702 a197c864
...@@ -57,6 +57,16 @@ func makeTLSConfig(group []*SiteConfig) (*tls.Config, error) { ...@@ -57,6 +57,16 @@ func makeTLSConfig(group []*SiteConfig) (*tls.Config, error) {
return caddytls.MakeTLSConfig(tlsConfigs) return caddytls.MakeTLSConfig(tlsConfigs)
} }
func getFallbacks(sites []*SiteConfig) []string {
fallbacks := []string{}
for _, sc := range sites {
if sc.FallbackSite {
fallbacks = append(fallbacks, sc.Addr.Host)
}
}
return fallbacks
}
// NewServer creates a new Server instance that will listen on addr // NewServer creates a new Server instance that will listen on addr
// and will serve the sites configured in group. // and will serve the sites configured in group.
func NewServer(addr string, group []*SiteConfig) (*Server, error) { func NewServer(addr string, group []*SiteConfig) (*Server, error) {
...@@ -66,6 +76,7 @@ func NewServer(addr string, group []*SiteConfig) (*Server, error) { ...@@ -66,6 +76,7 @@ func NewServer(addr string, group []*SiteConfig) (*Server, error) {
sites: group, sites: group,
connTimeout: GracefulTimeout, connTimeout: GracefulTimeout,
} }
s.vhosts.fallbackHosts = append(s.vhosts.fallbackHosts, getFallbacks(group)...)
s.Server = makeHTTPServerWithHeaderLimit(s.Server, group) s.Server = makeHTTPServerWithHeaderLimit(s.Server, group)
s.Server.Handler = s // this is weird, but whatever s.Server.Handler = s // this is weird, but whatever
......
...@@ -52,6 +52,9 @@ type SiteConfig struct { ...@@ -52,6 +52,9 @@ type SiteConfig struct {
// preserving functionality needed for proxying, // preserving functionality needed for proxying,
// websockets, etc. // websockets, etc.
Timeouts Timeouts Timeouts Timeouts
// If true all unmatched requests will be served by this site
FallbackSite bool
} }
// Timeouts specify various timeouts for a server to use. // Timeouts specify various timeouts for a server to use.
......
...@@ -10,14 +10,15 @@ import ( ...@@ -10,14 +10,15 @@ import (
// wildcards as TLS certificates support them), then // wildcards as TLS certificates support them), then
// by longest matching path. // by longest matching path.
type vhostTrie struct { type vhostTrie struct {
edges map[string]*vhostTrie fallbackHosts []string
site *SiteConfig // site to match on this node; also known as a virtual host edges map[string]*vhostTrie
path string // the path portion of the key for the associated site site *SiteConfig // site to match on this node; also known as a virtual host
path string // the path portion of the key for the associated site
} }
// newVHostTrie returns a new vhostTrie. // newVHostTrie returns a new vhostTrie.
func newVHostTrie() *vhostTrie { func newVHostTrie() *vhostTrie {
return &vhostTrie{edges: make(map[string]*vhostTrie)} return &vhostTrie{edges: make(map[string]*vhostTrie), fallbackHosts: []string{"0.0.0.0", ""}}
} }
// Insert adds stack to t keyed by key. The key should be // Insert adds stack to t keyed by key. The key should be
...@@ -45,12 +46,6 @@ func (t *vhostTrie) insertPath(remainingPath, originalPath string, site *SiteCon ...@@ -45,12 +46,6 @@ 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
...@@ -65,7 +60,7 @@ func (t *vhostTrie) Match(key string) (*SiteConfig, string) { ...@@ -65,7 +60,7 @@ 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 fallback hosts // try the given host, then, if no match, try fallback hosts
branch := t.matchHost(host) branch := t.matchHost(host)
for _, h := range FallbackHosts { for _, h := range t.fallbackHosts {
if branch != nil { if branch != nil {
break break
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment