From 77764714ad83d5c6e4db2da4b8945701f146bad0 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 10 May 2021 12:06:38 -0400 Subject: [PATCH] encode: Default to order the formats are enabled for `prefer` in Caddyfile (#4151) --- .../caddyfile_adapt/encode_options.txt | 46 +++++++++++++++++++ modules/caddyhttp/encode/caddyfile.go | 25 +++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/caddytest/integration/caddyfile_adapt/encode_options.txt b/caddytest/integration/caddyfile_adapt/encode_options.txt index a43c7e02..9459008a 100644 --- a/caddytest/integration/caddyfile_adapt/encode_options.txt +++ b/caddytest/integration/caddyfile_adapt/encode_options.txt @@ -1,5 +1,6 @@ :80 +# All the options encode gzip zstd { minimum_length 256 prefer zstd gzip @@ -14,6 +15,20 @@ encode gzip zstd { header Content-Type image/svg+xml* } } + +# Prefer list is implied (short way) +encode gzip zstd + +# Prefer list is implied (long way) +encode { + gzip 5 + zstd +} + +# Prefer list is turned off +encode gzip zstd { + prefer off +} ---------- { "apps": { @@ -55,6 +70,37 @@ encode gzip zstd { "zstd", "gzip" ] + }, + { + "encodings": { + "gzip": {}, + "zstd": {} + }, + "handler": "encode", + "prefer": [ + "gzip", + "zstd" + ] + }, + { + "encodings": { + "gzip": { + "level": 5 + }, + "zstd": {} + }, + "handler": "encode", + "prefer": [ + "gzip", + "zstd" + ] + }, + { + "encodings": { + "gzip": {}, + "zstd": {} + }, + "handler": "encode" } ] } diff --git a/modules/caddyhttp/encode/caddyfile.go b/modules/caddyhttp/encode/caddyfile.go index b236520e..d00a16a2 100644 --- a/modules/caddyhttp/encode/caddyfile.go +++ b/modules/caddyhttp/encode/caddyfile.go @@ -43,7 +43,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) // gzip [] // zstd // minimum_length -// prefer +// prefer off| // # response matcher block // match { // status @@ -55,7 +55,11 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) // // Specifying the formats on the first line will use those formats' defaults. func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + var preferDefaults []string + var preferOff bool + responseMatchers := make(map[string]caddyhttp.ResponseMatcher) + for d.Next() { for _, arg := range d.RemainingArgs() { mod, err := caddy.GetModule("http.encoders." + arg) @@ -70,6 +74,7 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { enc.EncodingsRaw = make(caddy.ModuleMap) } enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil) + preferDefaults = append(preferDefaults, arg) } for d.NextBlock(0) { @@ -86,6 +91,11 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { case "prefer": var encs []string for d.NextArg() { + // if one of the values is "off", then + // we'll skip setting the prefer list. + if d.Val() == "off" { + preferOff = true + } encs = append(encs, d.Val()) } if len(encs) == 0 { @@ -114,10 +124,23 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { enc.EncodingsRaw = make(caddy.ModuleMap) } enc.EncodingsRaw[name] = caddyconfig.JSON(encoding, nil) + preferDefaults = append(preferDefaults, name) } } } + // if the "prefer" subdirective wasn't specified, use + // the order in which the encoders were defined. + if len(enc.Prefer) == 0 { + enc.Prefer = preferDefaults + } + + // if "prefer off" was set, then we'll not use the default + // behaviour of the order in which they were defined. + if preferOff { + enc.Prefer = nil + } + return nil }