Skip to content

Commit

Permalink
encode: Default to order the formats are enabled for prefer in Cadd…
Browse files Browse the repository at this point in the history
…yfile
  • Loading branch information
francislavoie committed May 7, 2021
1 parent d4b2f1b commit 0c1d3e3
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 67 deletions.
178 changes: 112 additions & 66 deletions caddytest/integration/caddyfile_adapt/encode_options.txt
Original file line number Diff line number Diff line change
@@ -1,66 +1,112 @@
:80

encode gzip zstd {
minimum_length 256
prefer zstd gzip
match {
status 2xx 4xx 500
header Content-Type text/*
header Content-Type application/json*
header Content-Type application/javascript*
header Content-Type application/xhtml+xml*
header Content-Type application/atom+xml*
header Content-Type application/rss+xml*
header Content-Type image/svg+xml*
}
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"handle": [
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode",
"match": {
"headers": {
"Content-Type": [
"text/*",
"application/json*",
"application/javascript*",
"application/xhtml+xml*",
"application/atom+xml*",
"application/rss+xml*",
"image/svg+xml*"
]
},
"status_code": [
2,
4,
500
]
},
"minimum_length": 256,
"prefer": [
"zstd",
"gzip"
]
}
]
}
]
}
}
}
}
}
:80

# All the options
encode gzip zstd {
minimum_length 256
prefer zstd gzip
match {
status 2xx 4xx 500
header Content-Type text/*
header Content-Type application/json*
header Content-Type application/javascript*
header Content-Type application/xhtml+xml*
header Content-Type application/atom+xml*
header Content-Type application/rss+xml*
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": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"handle": [
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode",
"match": {
"headers": {
"Content-Type": [
"text/*",
"application/json*",
"application/javascript*",
"application/xhtml+xml*",
"application/atom+xml*",
"application/rss+xml*",
"image/svg+xml*"
]
},
"status_code": [
2,
4,
500
]
},
"minimum_length": 256,
"prefer": [
"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"
}
]
}
]
}
}
}
}
}
25 changes: 24 additions & 1 deletion modules/caddyhttp/encode/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
// gzip [<level>]
// zstd
// minimum_length <length>
// prefer <formats...>
// prefer off|<formats...>
// # response matcher block
// match {
// status <code...>
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 0c1d3e3

Please sign in to comment.