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: Specify default access log for a server (fix #3185)
Matthew Holt committed
Mar 24, 2020 at 13:21 UTC
2acb208e325c16a0f73a81957f9094198e97809c
3 files changed
+17
-12
caddyconfig/httpcaddyfile/httptype.go
+10
-10
@@ -319,11 +319,13 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
319
}
320
321
// hostsFromServerBlockKeys returns a list of all the non-empty hostnames
322
-// found in the keys of the server block sb. If sb has a key that omits
323
-// the hostname (i.e. is a catch-all/empty host), then the returned list
324
-// is empty, because the server block effectively matches ALL hosts.
322
+// found in the keys of the server block sb, unless allowEmpty is true, in
323
+// which case a key with no host (e.g. ":443") will be added to the list as
324
+// an empty string. Otherwise, if allowEmpty is false, and if sb has a key
325
+// that omits the hostname (i.e. is a catch-all/empty host), then the returned
326
+// list is empty, because the server block effectively matches ALL hosts.
327
// The list may not be in a consistent order.
326
-func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock) ([]string, error) {
328
+func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock, allowEmpty bool) ([]string, error) {
329
// first get each unique hostname
330
hostMap := make(map[string]struct{})
331
for _, sblockKey := range sb.Keys {
@@ -332,7 +334,7 @@ func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock) ([]stri
334
return nil, fmt.Errorf("parsing server block key: %v", err)
335
}
336
addr = addr.Normalize()
335
- if addr.Host == "" {
337
+ if addr.Host == "" && !allowEmpty {
338
// server block contains a key like ":443", i.e. the host portion
339
// is empty / catch-all, which means to match all hosts
340
return []string{}, nil
@@ -408,7 +410,7 @@ func (st *ServerType) serversFromPairings(
410
return nil, fmt.Errorf("server block %v: compiling matcher sets: %v", sblock.block.Keys, err)
411
}
412
411
- hosts, err := st.hostsFromServerBlockKeys(sblock.block)
413
+ hosts, err := st.hostsFromServerBlockKeys(sblock.block, false)
414
if err != nil {
415
return nil, err
416
}
@@ -488,14 +490,12 @@ func (st *ServerType) serversFromPairings(
490
LoggerNames: make(map[string]string),
491
}
492
}
491
- hosts, err := st.hostsFromServerBlockKeys(sblock.block)
493
+ hosts, err := st.hostsFromServerBlockKeys(sblock.block, true)
494
if err != nil {
495
return nil, err
496
}
497
for _, h := range hosts {
496
- if ncl.name != "" {
497
- srv.Logs.LoggerNames[h] = ncl.name
498
- }
498
+ srv.Logs.LoggerNames[h] = ncl.name
499
}
500
}
501
}
caddyconfig/httpcaddyfile/tlsapp.go
+1
-1
@@ -82,7 +82,7 @@ func (st ServerType) buildTLSApp(
82
// get values that populate an automation policy for this block
83
var ap *caddytls.AutomationPolicy
84
85
- sblockHosts, err := st.hostsFromServerBlockKeys(sblock.block)
85
+ sblockHosts, err := st.hostsFromServerBlockKeys(sblock.block, false)
86
if err != nil {
87
return nil, warnings, err
88
}
modules/caddyhttp/server.go
+6
-1
@@ -172,7 +172,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
172
173
logger := accLog
174
if s.Logs != nil && s.Logs.LoggerNames != nil {
175
- logger = logger.Named(s.Logs.LoggerNames[r.Host])
175
+ if loggerName, ok := s.Logs.LoggerNames[r.Host]; ok {
176
+ logger = logger.Named(loggerName)
177
+ } else {
178
+ // see if there's a default log name to attach to
179
+ logger = logger.Named(s.Logs.LoggerNames[""])
180
+ }
181
}
182
183
log := logger.Info