@marcel91200 / caddy Mirror · read-only / commits / 569ecdbd
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: Ensure hosts to skip for logs can always be collected (#4258)

* httpcaddyfile: ensure hosts to skip can always be collected Previously, some hosts that should be skipped in logging would be missed as the current logic would only collect them after encountering the first server that would log. This change makes sure the ServerLogConfig is initialized before iterating over the server blocks. * httpcaddyfile: add test case for skip hosts behavior

Frederik Ring committed Aug 2, 2021 at 22:15 UTC 569ecdbd02a3bdf8589f785cca022240973dea4d
2 files changed +85 -3
caddyconfig/httpcaddyfile/httptype.go
+10 -3
@@ -522,6 +522,16 @@ func (st *ServerType) serversFromPairings(
522 }
523 }
524
525 + // if needed, the ServerLogConfig is initialized beforehand so
526 + // that all server blocks can populate it with data, even when not
527 + // coming with a log directive
528 + for _, sblock := range p.serverBlocks {
529 + if len(sblock.pile["custom_log"]) != 0 {
530 + srv.Logs = new(caddyhttp.ServerLogConfig)
531 + break
532 + }
533 + }
534 +
535 // create a subroute for each site in the server block
536 for _, sblock := range p.serverBlocks {
537 matcherSetsEnc, err := st.compileEncodedMatcherSets(sblock)
@@ -636,9 +646,6 @@ func (st *ServerType) serversFromPairings(
646 sblockLogHosts := sblock.hostsFromKeys(true)
647 for _, cval := range sblock.pile["custom_log"] {
648 ncl := cval.Value.(namedCustomLog)
639 - if srv.Logs == nil {
640 - srv.Logs = new(caddyhttp.ServerLogConfig)
641 - }
649 if sblock.hasHostCatchAllKey() {
650 // all requests for hosts not able to be listed should use
651 // this log because it's a catch-all-hosts server block
caddytest/integration/caddyfile_adapt/log_skip_hosts.txt new
+75
@@ -0,0 +1,75 @@
1 +one.example.com {
2 + log
3 +}
4 +
5 +two.example.com {
6 +}
7 +
8 +three.example.com {
9 +}
10 +
11 +example.com {
12 +}
13 +----------
14 +{
15 + "apps": {
16 + "http": {
17 + "servers": {
18 + "srv0": {
19 + "listen": [
20 + ":443"
21 + ],
22 + "routes": [
23 + {
24 + "match": [
25 + {
26 + "host": [
27 + "three.example.com"
28 + ]
29 + }
30 + ],
31 + "terminal": true
32 + },
33 + {
34 + "match": [
35 + {
36 + "host": [
37 + "one.example.com"
38 + ]
39 + }
40 + ],
41 + "terminal": true
42 + },
43 + {
44 + "match": [
45 + {
46 + "host": [
47 + "two.example.com"
48 + ]
49 + }
50 + ],
51 + "terminal": true
52 + },
53 + {
54 + "match": [
55 + {
56 + "host": [
57 + "example.com"
58 + ]
59 + }
60 + ],
61 + "terminal": true
62 + }
63 + ],
64 + "logs": {
65 + "skip_hosts": [
66 + "three.example.com",
67 + "two.example.com",
68 + "example.com"
69 + ]
70 + }
71 + }
72 + }
73 + }
74 + }
75 +}