encode: Drop prefer from Caddyfile (#4156)

Followup to #4150, #4151 /cc @ueffel @polarathene

After a bit of discussion with @mholt, we decided to remove `prefer` as a subdirective and just go with using the order implicitly always. Simpler config, simpler docs, etc.

Effectively changes 7776471 and reverts a small part of f35a7fa.
This commit is contained in:
Francis Lavoie 2021-05-10 13:12:59 -04:00 committed by GitHub
parent 77764714ad
commit f5db41ce1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 61 deletions

View file

@ -3,7 +3,6 @@
# All the options # All the options
encode gzip zstd { encode gzip zstd {
minimum_length 256 minimum_length 256
prefer zstd gzip
match { match {
status 2xx 4xx 500 status 2xx 4xx 500
header Content-Type text/* header Content-Type text/*
@ -16,18 +15,10 @@ encode gzip zstd {
} }
} }
# Prefer list is implied (short way) # Long way with a block for each encoding
encode gzip zstd
# Prefer list is implied (long way)
encode { encode {
gzip 5
zstd zstd
} gzip 5
# Prefer list is turned off
encode gzip zstd {
prefer off
} }
---------- ----------
{ {
@ -66,17 +57,6 @@ encode gzip zstd {
] ]
}, },
"minimum_length": 256, "minimum_length": 256,
"prefer": [
"zstd",
"gzip"
]
},
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode",
"prefer": [ "prefer": [
"gzip", "gzip",
"zstd" "zstd"
@ -91,16 +71,9 @@ encode gzip zstd {
}, },
"handler": "encode", "handler": "encode",
"prefer": [ "prefer": [
"gzip", "zstd",
"zstd" "gzip"
] ]
},
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode"
} }
] ]
} }

View file

@ -43,7 +43,6 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
// gzip [<level>] // gzip [<level>]
// zstd // zstd
// minimum_length <length> // minimum_length <length>
// prefer off|<formats...>
// # response matcher block // # response matcher block
// match { // match {
// status <code...> // status <code...>
@ -55,8 +54,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
// //
// Specifying the formats on the first line will use those formats' defaults. // Specifying the formats on the first line will use those formats' defaults.
func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
var preferDefaults []string var prefer []string
var preferOff bool
responseMatchers := make(map[string]caddyhttp.ResponseMatcher) responseMatchers := make(map[string]caddyhttp.ResponseMatcher)
@ -74,7 +72,7 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
enc.EncodingsRaw = make(caddy.ModuleMap) enc.EncodingsRaw = make(caddy.ModuleMap)
} }
enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil) enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
preferDefaults = append(preferDefaults, arg) prefer = append(prefer, arg)
} }
for d.NextBlock(0) { for d.NextBlock(0) {
@ -88,20 +86,6 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return err return err
} }
enc.MinLength = minLength enc.MinLength = minLength
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 {
return d.ArgErr()
}
enc.Prefer = encs
case "match": case "match":
err := caddyhttp.ParseNamedResponseMatcher(d.NewFromNextSegment(), responseMatchers) err := caddyhttp.ParseNamedResponseMatcher(d.NewFromNextSegment(), responseMatchers)
if err != nil { if err != nil {
@ -124,22 +108,13 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
enc.EncodingsRaw = make(caddy.ModuleMap) enc.EncodingsRaw = make(caddy.ModuleMap)
} }
enc.EncodingsRaw[name] = caddyconfig.JSON(encoding, nil) enc.EncodingsRaw[name] = caddyconfig.JSON(encoding, nil)
preferDefaults = append(preferDefaults, name) prefer = append(prefer, name)
} }
} }
} }
// if the "prefer" subdirective wasn't specified, use // use the order in which the encoders were defined.
// the order in which the encoders were defined. enc.Prefer = prefer
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 return nil
} }