Skip to content

Commit

Permalink
rounding of the PR
Browse files Browse the repository at this point in the history
* added integration tests for new caddyfile directives
* improved various doc strings (punctuation and typos)
* added json tag for file_server precompress order and encode matcher
  • Loading branch information
ueffel committed Mar 13, 2021
1 parent 67ab2d0 commit 9cd73eb
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 11 deletions.
66 changes: 66 additions & 0 deletions caddytest/integration/caddyfile_adapt/encode_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
: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"
]
}
]
}
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
:80

file_server {
precompressed zstd br gzip
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"handle": [
{
"handler": "file_server",
"hide": [
".\\Caddyfile"
],
"precompressed": {
"br": {},
"gzip": {},
"zstd": {}
},
"precompressed_order": [
"zstd",
"br",
"gzip"
]
}
]
}
]
}
}
}
}
}
4 changes: 2 additions & 2 deletions modules/caddyhttp/encode/brotli/brotli_precompressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func init() {
caddy.RegisterModule(BrotliPrecompressed{})
}

// BrotliPrecompressed provides the file extension for files precompressed with brotli encoding
// BrotliPrecompressed provides the file extension for files precompressed with brotli encoding.
type BrotliPrecompressed struct{}

// CaddyModule returns the Caddy module information.
Expand All @@ -24,7 +24,7 @@ func (BrotliPrecompressed) CaddyModule() caddy.ModuleInfo {
// used in the Accept-Encoding request headers.
func (BrotliPrecompressed) AcceptEncoding() string { return "br" }

// Suffix returns the filename suffix of precomressed files
// Suffix returns the filename suffix of precompressed files.
func (BrotliPrecompressed) Suffix() string { return ".br" }

// Interface guards
Expand Down
10 changes: 6 additions & 4 deletions modules/caddyhttp/encode/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ type Encode struct {
// Only encode responses that are at least this many bytes long.
MinLength int `json:"minimum_length,omitempty"`

Matcher *caddyhttp.ResponseMatcher
// Only encode responses that match against this ResponseMmatcher.
// The default is a collection of text-based Content-Type headers.
Matcher *caddyhttp.ResponseMatcher `json:"match,omitempty"`

writerPools map[string]*sync.Pool // TODO: these pools do not get reused through config reloads...
}
Expand Down Expand Up @@ -188,7 +190,7 @@ func (rw *responseWriter) WriteHeader(status int) {
rw.statusCode = status
}

// Match determines if encoding should be done based on the ResponseMatcher
// Match determines, if encoding should be done based on the ResponseMatcher.
func (enc *Encode) Match(rw *responseWriter) bool {
return enc.Matcher.Match(rw.statusCode, rw.Header())
}
Expand Down Expand Up @@ -392,8 +394,8 @@ type Encoding interface {
NewEncoder() Encoder
}

// Precompressed is a type which returns filename suffix of precomressed
// file and the name used in the Accept-Encoding header.
// Precompressed is a type which returns filename suffix of precompressed
// file and Accept-Encoding header to use when serving this file.
type Precompressed interface {
AcceptEncoding() string
Suffix() string
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/encode/gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (g Gzip) Validate() error {
// used in the Accept-Encoding request headers.
func (Gzip) AcceptEncoding() string { return "gzip" }

// Suffix returns the filename suffix of precomressed files
// Suffix returns the filename suffix of precompressed files.
func (Gzip) Suffix() string { return ".gz" }

// NewEncoder returns a new gzip writer.
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/encode/gzip/gzip_precompressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func init() {
caddy.RegisterModule(GzipPrecompressed{})
}

// GzipPrecompressed provides the file extension for files precompressed with gzip encoding
// GzipPrecompressed provides the file extension for files precompressed with gzip encoding.
type GzipPrecompressed struct {
Gzip
}
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/encode/zstd/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (z *Zstd) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// used in the Accept-Encoding request headers.
func (Zstd) AcceptEncoding() string { return "zstd" }

// Suffix returns the filename suffix of precomressed files
// Suffix returns the filename suffix of precompressed files.
func (Zstd) Suffix() string { return ".zst" }

// NewEncoder returns a new gzip writer.
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/encode/zstd/zstd_precompressed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func init() {
caddy.RegisterModule(ZstdPrecompressed{})
}

// ZstdPrecompressed provides the file extension for files precompressed with zstandard encoding
// ZstdPrecompressed provides the file extension for files precompressed with zstandard encoding.
type ZstdPrecompressed struct {
Zstd
}
Expand Down
2 changes: 1 addition & 1 deletion modules/caddyhttp/fileserver/staticfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type FileServer struct {
// If the client has no strong preference (q-factor), choose these encodings in order.
// If no order specified here, the first encoding from the Accept-Encoding header
// that both client and server support is used
PrecompressedOrder []string
PrecompressedOrder []string `json:"precompressed_order,omitempty"`

precompressors map[string]encode.Precompressed

Expand Down

0 comments on commit 9cd73eb

Please sign in to comment.