@marcel91200 / caddy Mirror · read-only / commits / 904d9cab
Fork 0

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

httpcaddyfile: Include non-standard ports when mapping logger names

If a site block has a key like "http://localhost:2016", then the log for that site must be mapped to "localhost:2016" and not just "localhost" because "localhost:2016" will be the value of the Host header of requests. But a key like "localhost:80" does not include the port since the Host header will not include ":80" because it is a standard port. Fixes https://caddy.community/t/v2-common-log-format-not-working/7352?u=matt

Matthew Holt committed Mar 30, 2020 at 18:39 UTC 904d9cab390007610a65f648d05314f5826c3979
2 files changed +16 -6
caddyconfig/httpcaddyfile/httptype.go
+15 -5
@@ -17,8 +17,10 @@ package httpcaddyfile
17 import (
18 "encoding/json"
19 "fmt"
20 + "net"
21 "reflect"
22 "sort"
23 + "strconv"
24 "strings"
25
26 "github.com/caddyserver/caddy/v2"
@@ -324,8 +326,9 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
326 // an empty string. Otherwise, if allowEmpty is false, and if sb has a key
327 // that omits the hostname (i.e. is a catch-all/empty host), then the returned
328 // list is empty, because the server block effectively matches ALL hosts.
327 -// The list may not be in a consistent order.
328 -func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock, allowEmpty bool) ([]string, error) {
329 +// The list may not be in a consistent order. If includePorts is true, then
330 +// any non-empty, non-standard ports will be included.
331 +func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock, allowEmpty, includePorts bool) ([]string, error) {
332 // first get each unique hostname
333 hostMap := make(map[string]struct{})
334 for _, sblockKey := range sb.Keys {
@@ -339,7 +342,14 @@ func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock, allowEm
342 // is empty / catch-all, which means to match all hosts
343 return []string{}, nil
344 }
342 - hostMap[addr.Host] = struct{}{}
345 + if includePorts &&
346 + addr.Port != "" &&
347 + addr.Port != strconv.Itoa(caddyhttp.DefaultHTTPPort) &&
348 + addr.Port != strconv.Itoa(caddyhttp.DefaultHTTPSPort) {
349 + hostMap[net.JoinHostPort(addr.Host, addr.Port)] = struct{}{}
350 + } else {
351 + hostMap[addr.Host] = struct{}{}
352 + }
353 }
354
355 // convert map to slice
@@ -410,7 +420,7 @@ func (st *ServerType) serversFromPairings(
420 return nil, fmt.Errorf("server block %v: compiling matcher sets: %v", sblock.block.Keys, err)
421 }
422
413 - hosts, err := st.hostsFromServerBlockKeys(sblock.block, false)
423 + hosts, err := st.hostsFromServerBlockKeys(sblock.block, false, false)
424 if err != nil {
425 return nil, err
426 }
@@ -490,7 +500,7 @@ func (st *ServerType) serversFromPairings(
500 LoggerNames: make(map[string]string),
501 }
502 }
493 - hosts, err := st.hostsFromServerBlockKeys(sblock.block, true)
503 + hosts, err := st.hostsFromServerBlockKeys(sblock.block, true, true)
504 if err != nil {
505 return nil, err
506 }
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, false)
85 + sblockHosts, err := st.hostsFromServerBlockKeys(sblock.block, false, false)
86 if err != nil {
87 return nil, warnings, err
88 }