diff options
author | Matt Holt <[email protected]> | 2020-06-26 12:01:50 -0600 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-26 12:01:50 -0600 |
commit | 21c00a3cd2f702b32e80dbef2987ac2250a2c9a5 (patch) | |
tree | cf5b4ca03d1a194faa62091fe0d6252e073b6269 | |
parent | 61b7002d262ab748e3db7cc705c8290447fb5b34 (diff) | |
download | caddy-2.1.0-beta.2.tar.gz caddy-2.1.0-beta.2.zip |
caddyhttp: Better host matching for logger names (fix #3488) (#3522)v2.1.0-beta.2
First try an exact lookup like before, but if it fails, strip the port
and try again. example.com:1234 should still use a logger keyed for
example.com if there is no key example.com:1234.
-rw-r--r-- | modules/caddyhttp/server.go | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index 38555e74a..6851e986d 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -453,11 +453,27 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) *zap.Logg } func (slc ServerLogConfig) getLoggerName(host string) string { - if loggerName, ok := slc.LoggerNames[host]; ok { + tryHost := func(key string) (string, bool) { + // first try exact match + if loggerName, ok := slc.LoggerNames[key]; ok { + return loggerName, ok + } + // strip port and try again (i.e. Host header of "example.com:1234" should + // match "example.com" if there is no "example.com:1234" in the map) + hostOnly, _, err := net.SplitHostPort(key) + if err != nil { + return "", false + } + loggerName, ok := slc.LoggerNames[hostOnly] + return loggerName, ok + } + + // try the exact hostname first + if loggerName, ok := tryHost(host); ok { return loggerName } - // Try matching wildcard domains if other non-specific loggers exist + // try matching wildcard domains if other non-specific loggers exist labels := strings.Split(host, ".") for i := range labels { if labels[i] == "" { @@ -465,7 +481,7 @@ func (slc ServerLogConfig) getLoggerName(host string) string { } labels[i] = "*" wildcardHost := strings.Join(labels, ".") - if loggerName, ok := slc.LoggerNames[wildcardHost]; ok { + if loggerName, ok := tryHost(wildcardHost); ok { return loggerName } } |