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: Add nil check (fixes #3248 and fixes #3250)
Matthew Holt committed
Apr 10, 2020 at 08:12 UTC
999ab22b8cbe3721eff6ae57526024a17f7cee4e
2 files changed
+13
-5
modules/caddyhttp/fileserver/command.go
+1
-1
@@ -85,7 +85,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
85
86
if domain != "" {
87
route.MatcherSetsRaw = []caddy.ModuleMap{
88
- caddy.ModuleMap{
88
+ {
89
"host": caddyconfig.JSON(caddyhttp.MatchHost{domain}, nil),
90
},
91
}
modules/caddyhttp/server.go
+12
-4
@@ -170,8 +170,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
170
repl.Set("http.response.latency", latency)
171
172
logger := accLog
173
- if loggerName := s.Logs.getLoggerName(r.Host); loggerName != "" {
174
- logger = logger.Named(loggerName)
173
+ if s.Logs != nil {
174
+ logger = s.Logs.wrapLogger(logger, r.Host)
175
}
176
177
log := logger.Info
@@ -200,8 +200,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
200
if err != nil {
201
// prepare the error log
202
logger := errLog
203
- if loggerName := s.Logs.getLoggerName(r.Host); loggerName != "" {
204
- logger = logger.Named(loggerName)
203
+ if s.Logs != nil {
204
+ logger = s.Logs.wrapLogger(logger, r.Host)
205
}
206
207
// get the values that will be used to log the error
@@ -378,6 +378,14 @@ type ServerLogConfig struct {
378
LoggerNames map[string]string `json:"logger_names,omitempty"`
379
}
380
381
+// wrapLogger wraps logger in a logger named according to user preferences for the given host.
382
+func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) *zap.Logger {
383
+ if loggerName := slc.getLoggerName(host); loggerName != "" {
384
+ return logger.Named(loggerName)
385
+ }
386
+ return logger
387
+}
388
+
389
func (slc ServerLogConfig) getLoggerName(host string) string {
390
if loggerName, ok := slc.LoggerNames[host]; ok {
391
return loggerName