Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
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.
Matt Holt committed
Jun 26, 2020 at 12:01 UTC
21c00a3cd2f702b32e80dbef2987ac2250a2c9a5
1 file changed
+19
-3
modules/caddyhttp/server.go
+19
-3
@@ -453,11 +453,27 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) *zap.Logg
453
}
454
455
func (slc ServerLogConfig) getLoggerName(host string) string {
456
- if loggerName, ok := slc.LoggerNames[host]; ok {
456
+ tryHost := func(key string) (string, bool) {
457
+ // first try exact match
458
+ if loggerName, ok := slc.LoggerNames[key]; ok {
459
+ return loggerName, ok
460
+ }
461
+ // strip port and try again (i.e. Host header of "example.com:1234" should
462
+ // match "example.com" if there is no "example.com:1234" in the map)
463
+ hostOnly, _, err := net.SplitHostPort(key)
464
+ if err != nil {
465
+ return "", false
466
+ }
467
+ loggerName, ok := slc.LoggerNames[hostOnly]
468
+ return loggerName, ok
469
+ }
470
+
471
+ // try the exact hostname first
472
+ if loggerName, ok := tryHost(host); ok {
473
return loggerName
474
}
475
460
- // Try matching wildcard domains if other non-specific loggers exist
476
+ // try matching wildcard domains if other non-specific loggers exist
477
labels := strings.Split(host, ".")
478
for i := range labels {
479
if labels[i] == "" {
@@ -465,7 +481,7 @@ func (slc ServerLogConfig) getLoggerName(host string) string {
481
}
482
labels[i] = "*"
483
wildcardHost := strings.Join(labels, ".")
468
- if loggerName, ok := slc.LoggerNames[wildcardHost]; ok {
484
+ if loggerName, ok := tryHost(wildcardHost); ok {
485
return loggerName
486
}
487
}