@marcel91200 / caddy Mirror · read-only / commits / 63afffc2
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: Proper log config with catch-all blocks (fix #3878)

Matthew Holt committed Nov 24, 2020 at 16:36 UTC 63afffc2e317579a5bc618d5441ac238127114a0
2 files changed +96 -7
caddyconfig/httpcaddyfile/httptype.go
+16 -7
@@ -450,6 +450,15 @@ func (st *ServerType) serversFromPairings(
450 var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
451 autoHTTPSWillAddConnPolicy := autoHTTPS != "off"
452
453 + // if a catch-all server block (one which accepts all hostnames) exists in this pairing,
454 + // we need to know that so that we can configure logs properly (see #3878)
455 + var catchAllSblockExists bool
456 + for _, sblock := range p.serverBlocks {
457 + if len(sblock.hostsFromKeys(false)) == 0 {
458 + catchAllSblockExists = true
459 + }
460 + }
461 +
462 // create a subroute for each site in the server block
463 for _, sblock := range p.serverBlocks {
464 matcherSetsEnc, err := st.compileEncodedMatcherSets(sblock)
@@ -563,13 +572,13 @@ func (st *ServerType) serversFromPairings(
572 } else {
573 // map each host to the user's desired logger name
574 for _, h := range sblockLogHosts {
566 - // if the custom logger name is non-empty, add it to
567 - // the map; otherwise, only map to an empty logger
568 - // name if the server block has a catch-all host (in
569 - // which case only requests with mapped hostnames will
570 - // be access-logged, so it'll be necessary to add them
571 - // to the map even if they use default logger)
572 - if ncl.name != "" || len(hosts) == 0 {
575 + // if the custom logger name is non-empty, add it to the map;
576 + // otherwise, only map to an empty logger name if this or
577 + // another site block on this server has a catch-all host (in
578 + // which case only requests with mapped hostnames will be
579 + // access-logged, so it'll be necessary to add them to the
580 + // map even if they use default logger)
581 + if ncl.name != "" || catchAllSblockExists {
582 if srv.Logs.LoggerNames == nil {
583 srv.Logs.LoggerNames = make(map[string]string)
584 }
caddytest/integration/caddyfile_adapt/log_except_catchall_blocks.txt new
+80
@@ -0,0 +1,80 @@
1 +http://localhost:2020 {
2 + log
3 + file_server
4 +}
5 +
6 +:2020 {
7 + respond 418
8 +}
9 +----------
10 +{
11 + "apps": {
12 + "http": {
13 + "servers": {
14 + "srv0": {
15 + "listen": [
16 + ":2020"
17 + ],
18 + "routes": [
19 + {
20 + "match": [
21 + {
22 + "host": [
23 + "localhost"
24 + ]
25 + }
26 + ],
27 + "handle": [
28 + {
29 + "handler": "subroute",
30 + "routes": [
31 + {
32 + "handle": [
33 + {
34 + "handler": "file_server",
35 + "hide": [
36 + "./Caddyfile"
37 + ]
38 + }
39 + ]
40 + }
41 + ]
42 + }
43 + ],
44 + "terminal": true
45 + },
46 + {
47 + "handle": [
48 + {
49 + "handler": "subroute",
50 + "routes": [
51 + {
52 + "handle": [
53 + {
54 + "handler": "static_response",
55 + "status_code": 418
56 + }
57 + ]
58 + }
59 + ]
60 + }
61 + ],
62 + "terminal": true
63 + }
64 + ],
65 + "automatic_https": {
66 + "skip": [
67 + "localhost"
68 + ]
69 + },
70 + "logs": {
71 + "logger_names": {
72 + "localhost:2020": ""
73 + },
74 + "skip_unmapped_hosts": true
75 + }
76 + }
77 + }
78 + }
79 + }
80 +}
\ No newline at end of file