Commit a197c864 authored by Sergey Frolov's avatar Sergey Frolov

Move fallbackHosts to vhostTrie

parent 4991d702
......@@ -57,6 +57,16 @@ func makeTLSConfig(group []*SiteConfig) (*tls.Config, error) {
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
// and will serve the sites configured in group.
func NewServer(addr string, group []*SiteConfig) (*Server, error) {
......@@ -66,6 +76,7 @@ func NewServer(addr string, group []*SiteConfig) (*Server, error) {
sites: group,
connTimeout: GracefulTimeout,
}
s.vhosts.fallbackHosts = append(s.vhosts.fallbackHosts, getFallbacks(group)...)
s.Server = makeHTTPServerWithHeaderLimit(s.Server, group)
s.Server.Handler = s // this is weird, but whatever
......
......@@ -52,6 +52,9 @@ type SiteConfig struct {
// preserving functionality needed for proxying,
// websockets, etc.
Timeouts Timeouts
// If true all unmatched requests will be served by this site
FallbackSite bool
}
// Timeouts specify various timeouts for a server to use.
......
......@@ -10,14 +10,15 @@ import (
// wildcards as TLS certificates support them), then
// by longest matching path.
type vhostTrie struct {
edges map[string]*vhostTrie
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
fallbackHosts []string
edges map[string]*vhostTrie
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.
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
......@@ -45,12 +46,6 @@ 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
......@@ -65,7 +60,7 @@ func (t *vhostTrie) Match(key string) (*SiteConfig, string) {
host, path := t.splitHostPath(key)
// try the given host, then, if no match, try fallback hosts
branch := t.matchHost(host)
for _, h := range FallbackHosts {
for _, h := range t.fallbackHosts {
if branch != nil {
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