aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/encode/caddyfile.go
diff options
context:
space:
mode:
authorKévin Dunglas <[email protected]>2024-12-11 00:48:30 +0100
committerGitHub <[email protected]>2024-12-10 16:48:30 -0700
commitd0e209e1da6cd6d7d61e83b851f2913ee31454f8 (patch)
tree26edec5b50e8c391361926379fccee06503dfe48 /modules/caddyhttp/encode/caddyfile.go
parent290cfea08f2486fb86dbc11eec96d79751180eda (diff)
downloadcaddy-d0e209e1da6cd6d7d61e83b851f2913ee31454f8.tar.gz
caddy-d0e209e1da6cd6d7d61e83b851f2913ee31454f8.zip
encode: good defaults (#6737)
* feat: good default for encode * fix tests and add a new one
Diffstat (limited to 'modules/caddyhttp/encode/caddyfile.go')
-rw-r--r--modules/caddyhttp/encode/caddyfile.go36
1 files changed, 21 insertions, 15 deletions
diff --git a/modules/caddyhttp/encode/caddyfile.go b/modules/caddyhttp/encode/caddyfile.go
index e8ea4b807..8b8657080 100644
--- a/modules/caddyhttp/encode/caddyfile.go
+++ b/modules/caddyhttp/encode/caddyfile.go
@@ -57,21 +57,7 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Next() // consume directive name
prefer := []string{}
- for _, arg := range d.RemainingArgs() {
- mod, err := caddy.GetModule("http.encoders." + arg)
- if err != nil {
- return d.Errf("finding encoder module '%s': %v", mod, err)
- }
- encoding, ok := mod.New().(Encoding)
- if !ok {
- return d.Errf("module %s is not an HTTP encoding", mod)
- }
- if enc.EncodingsRaw == nil {
- enc.EncodingsRaw = make(caddy.ModuleMap)
- }
- enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
- prefer = append(prefer, arg)
- }
+ remainingArgs := d.RemainingArgs()
responseMatchers := make(map[string]caddyhttp.ResponseMatcher)
for d.NextBlock(0) {
@@ -111,6 +97,26 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}
}
+ if len(prefer) == 0 && len(remainingArgs) == 0 {
+ remainingArgs = []string{"zstd", "gzip"}
+ }
+
+ for _, arg := range remainingArgs {
+ mod, err := caddy.GetModule("http.encoders." + arg)
+ if err != nil {
+ return d.Errf("finding encoder module '%s': %v", mod, err)
+ }
+ encoding, ok := mod.New().(Encoding)
+ if !ok {
+ return d.Errf("module %s is not an HTTP encoding", mod)
+ }
+ if enc.EncodingsRaw == nil {
+ enc.EncodingsRaw = make(caddy.ModuleMap)
+ }
+ enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
+ prefer = append(prefer, arg)
+ }
+
// use the order in which the encoders were defined.
enc.Prefer = prefer