From 21c00a3cd2f702b32e80dbef2987ac2250a2c9a5 Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Fri, 26 Jun 2020 12:01:50 -0600 Subject: caddyhttp: Better host matching for logger names (fix #3488) (#3522) 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. --- modules/caddyhttp/server.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'modules/caddyhttp/server.go') diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index 38555e7..6851e98 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 } } -- cgit v1.2.3