1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

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.
This commit is contained in:
Matt Holt 2020-06-26 12:01:50 -06:00 committed by GitHub
parent 61b7002d26
commit 21c00a3cd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
}
}