From a197c864e874008b32c70c59e2cc2a2ad7e40486 Mon Sep 17 00:00:00 2001 From: Sergey Frolov Date: Thu, 27 Jul 2017 17:01:40 -0400 Subject: [PATCH] Move fallbackHosts to vhostTrie --- caddyhttp/httpserver/server.go | 11 +++++++++++ caddyhttp/httpserver/siteconfig.go | 3 +++ caddyhttp/httpserver/vhosttrie.go | 17 ++++++----------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/caddyhttp/httpserver/server.go b/caddyhttp/httpserver/server.go index f4f22ab5..c91212ae 100644 --- a/caddyhttp/httpserver/server.go +++ b/caddyhttp/httpserver/server.go @@ -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 diff --git a/caddyhttp/httpserver/siteconfig.go b/caddyhttp/httpserver/siteconfig.go index a091868f..9f17122c 100644 --- a/caddyhttp/httpserver/siteconfig.go +++ b/caddyhttp/httpserver/siteconfig.go @@ -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. diff --git a/caddyhttp/httpserver/vhosttrie.go b/caddyhttp/httpserver/vhosttrie.go index 4023d034..d4934707 100644 --- a/caddyhttp/httpserver/vhosttrie.go +++ b/caddyhttp/httpserver/vhosttrie.go @@ -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 }