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
encode: Default to order the formats are enabled for `prefer` in Caddyfile (#4151)
Francis Lavoie committed
May 10, 2021 at 12:06 UTC
77764714ad83d5c6e4db2da4b8945701f146bad0
2 files changed
+70
-1
caddytest/integration/caddyfile_adapt/encode_options.txt
+46
@@ -1,5 +1,6 @@
1
:80
2
3
+# All the options
4
encode gzip zstd {
5
minimum_length 256
6
prefer zstd gzip
@@ -14,6 +15,20 @@ encode gzip zstd {
15
header Content-Type image/svg+xml*
16
}
17
}
18
+
19
+# Prefer list is implied (short way)
20
+encode gzip zstd
21
+
22
+# Prefer list is implied (long way)
23
+encode {
24
+ gzip 5
25
+ zstd
26
+}
27
+
28
+# Prefer list is turned off
29
+encode gzip zstd {
30
+ prefer off
31
+}
32
----------
33
{
34
"apps": {
@@ -55,6 +70,37 @@ encode gzip zstd {
70
"zstd",
71
"gzip"
72
]
73
+ },
74
+ {
75
+ "encodings": {
76
+ "gzip": {},
77
+ "zstd": {}
78
+ },
79
+ "handler": "encode",
80
+ "prefer": [
81
+ "gzip",
82
+ "zstd"
83
+ ]
84
+ },
85
+ {
86
+ "encodings": {
87
+ "gzip": {
88
+ "level": 5
89
+ },
90
+ "zstd": {}
91
+ },
92
+ "handler": "encode",
93
+ "prefer": [
94
+ "gzip",
95
+ "zstd"
96
+ ]
97
+ },
98
+ {
99
+ "encodings": {
100
+ "gzip": {},
101
+ "zstd": {}
102
+ },
103
+ "handler": "encode"
104
}
105
]
106
}
modules/caddyhttp/encode/caddyfile.go
+24
-1
@@ -43,7 +43,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
43
// gzip [<level>]
44
// zstd
45
// minimum_length <length>
46
-// prefer <formats...>
46
+// prefer off|<formats...>
47
// # response matcher block
48
// match {
49
// status <code...>
@@ -55,7 +55,11 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
55
//
56
// Specifying the formats on the first line will use those formats' defaults.
57
func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
58
+ var preferDefaults []string
59
+ var preferOff bool
60
+
61
responseMatchers := make(map[string]caddyhttp.ResponseMatcher)
62
+
63
for d.Next() {
64
for _, arg := range d.RemainingArgs() {
65
mod, err := caddy.GetModule("http.encoders." + arg)
@@ -70,6 +74,7 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
74
enc.EncodingsRaw = make(caddy.ModuleMap)
75
}
76
enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
77
+ preferDefaults = append(preferDefaults, arg)
78
}
79
80
for d.NextBlock(0) {
@@ -86,6 +91,11 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
91
case "prefer":
92
var encs []string
93
for d.NextArg() {
94
+ // if one of the values is "off", then
95
+ // we'll skip setting the prefer list.
96
+ if d.Val() == "off" {
97
+ preferOff = true
98
+ }
99
encs = append(encs, d.Val())
100
}
101
if len(encs) == 0 {
@@ -114,10 +124,23 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
124
enc.EncodingsRaw = make(caddy.ModuleMap)
125
}
126
enc.EncodingsRaw[name] = caddyconfig.JSON(encoding, nil)
127
+ preferDefaults = append(preferDefaults, name)
128
}
129
}
130
}
131
132
+ // if the "prefer" subdirective wasn't specified, use
133
+ // the order in which the encoders were defined.
134
+ if len(enc.Prefer) == 0 {
135
+ enc.Prefer = preferDefaults
136
+ }
137
+
138
+ // if "prefer off" was set, then we'll not use the default
139
+ // behaviour of the order in which they were defined.
140
+ if preferOff {
141
+ enc.Prefer = nil
142
+ }
143
+
144
return nil
145
}
146