@marcel91200 / caddy Mirror · read-only / commits / d0e209e1
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 12, 04:30 UTC

encode: good defaults (#6737)

* feat: good default for encode * fix tests and add a new one

Kévin Dunglas committed Dec 11, 2024 at 00:48 UTC d0e209e1da6cd6d7d61e83b851f2913ee31454f8
2 files changed +34 -15
caddytest/integration/caddyfile_adapt/encode_options.caddyfiletest
+13
@@ -21,6 +21,8 @@ encode {
21 zstd
22 gzip 5
23 }
24 +
25 +encode
26 ----------
27 {
28 "apps": {
@@ -76,6 +78,17 @@ encode {
78 "zstd",
79 "gzip"
80 ]
81 + },
82 + {
83 + "encodings": {
84 + "gzip": {},
85 + "zstd": {}
86 + },
87 + "handler": "encode",
88 + "prefer": [
89 + "zstd",
90 + "gzip"
91 + ]
92 }
93 ]
94 }
modules/caddyhttp/encode/caddyfile.go
+21 -15
@@ -57,21 +57,7 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
57 d.Next() // consume directive name
58
59 prefer := []string{}
60 - for _, arg := range d.RemainingArgs() {
61 - mod, err := caddy.GetModule("http.encoders." + arg)
62 - if err != nil {
63 - return d.Errf("finding encoder module '%s': %v", mod, err)
64 - }
65 - encoding, ok := mod.New().(Encoding)
66 - if !ok {
67 - return d.Errf("module %s is not an HTTP encoding", mod)
68 - }
69 - if enc.EncodingsRaw == nil {
70 - enc.EncodingsRaw = make(caddy.ModuleMap)
71 - }
72 - enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
73 - prefer = append(prefer, arg)
74 - }
60 + remainingArgs := d.RemainingArgs()
61
62 responseMatchers := make(map[string]caddyhttp.ResponseMatcher)
63 for d.NextBlock(0) {
@@ -111,6 +97,26 @@ func (enc *Encode) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
97 }
98 }
99
100 + if len(prefer) == 0 && len(remainingArgs) == 0 {
101 + remainingArgs = []string{"zstd", "gzip"}
102 + }
103 +
104 + for _, arg := range remainingArgs {
105 + mod, err := caddy.GetModule("http.encoders." + arg)
106 + if err != nil {
107 + return d.Errf("finding encoder module '%s': %v", mod, err)
108 + }
109 + encoding, ok := mod.New().(Encoding)
110 + if !ok {
111 + return d.Errf("module %s is not an HTTP encoding", mod)
112 + }
113 + if enc.EncodingsRaw == nil {
114 + enc.EncodingsRaw = make(caddy.ModuleMap)
115 + }
116 + enc.EncodingsRaw[arg] = caddyconfig.JSON(encoding, nil)
117 + prefer = append(prefer, arg)
118 + }
119 +
120 // use the order in which the encoders were defined.
121 enc.Prefer = prefer
122