Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
Implement brotli encoder; improve validation of other encoders
Matthew Holt committed
Jun 13, 2019 at 11:20 UTC
b8e7453fef3dac6036403bc384eec96becff5114
6 files changed
+86
-5
cmd/caddy2/main.go
+1
@@ -7,6 +7,7 @@ import (
7
_ "github.com/caddyserver/caddy2/modules/caddyhttp"
8
_ "github.com/caddyserver/caddy2/modules/caddyhttp/caddylog"
9
_ "github.com/caddyserver/caddy2/modules/caddyhttp/encode"
10
+ _ "github.com/caddyserver/caddy2/modules/caddyhttp/encode/brotli"
11
_ "github.com/caddyserver/caddy2/modules/caddyhttp/encode/gzip"
12
_ "github.com/caddyserver/caddy2/modules/caddyhttp/encode/zstd"
13
_ "github.com/caddyserver/caddy2/modules/caddyhttp/fileserver"
go.mod
+1
@@ -4,6 +4,7 @@ go 1.12
4
5
require (
6
github.com/DataDog/zstd v1.4.0 // indirect
7
+ github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb // indirect
8
github.com/dustin/go-humanize v1.0.0
9
github.com/go-acme/lego v2.6.0+incompatible
10
github.com/google/go-cmp v0.3.0 // indirect
go.sum
+4
@@ -1,5 +1,7 @@
1
github.com/DataDog/zstd v1.4.0 h1:vhoV+DUHnRZdKW1i5UMjAk2G4JY8wN4ayRfYDNdEhwo=
2
github.com/DataDog/zstd v1.4.0/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
3
+github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb h1:Qs8/an94NFS1x2nn7rSI+skKFuw/EGfEYAGS3w/p+jc=
4
+github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb/go.mod h1:+lx6/Aqd1kLJ1GQfkvOnaZ1WGmLpMpbprPuIOOZX30U=
5
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
6
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
7
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
@@ -9,6 +11,8 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
11
github.com/go-acme/lego v2.5.0+incompatible/go.mod h1:yzMNe9CasVUhkquNvti5nAtPmG94USbYxYrZfTkIn0M=
12
github.com/go-acme/lego v2.6.0+incompatible h1:KxcEWOF5hKtgou4xIqPaXSRF9DoO4OJ90ndwdK6YH/k=
13
github.com/go-acme/lego v2.6.0+incompatible/go.mod h1:yzMNe9CasVUhkquNvti5nAtPmG94USbYxYrZfTkIn0M=
14
+github.com/golang/gddo v0.0.0-20190419222130-af0f2af80721 h1:KRMr9A3qfbVM7iV/WcLY/rL5LICqwMHLhwRXKu99fXw=
15
+github.com/golang/gddo v0.0.0-20190419222130-af0f2af80721/go.mod h1:xEhNfoBDX1hzLm2Nf80qUvZ2sVwoMZ8d6IE2SrsQfh4=
16
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
17
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
18
github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b h1:LHpBANNM/cw1PAXJtKV9dgfp6ztOKfdGXcltGmqU9aE=
modules/caddyhttp/encode/brotli/brotli.go
new
+51
@@ -0,0 +1,51 @@
1
+package caddybrotli
2
+
3
+import (
4
+ "fmt"
5
+
6
+ "github.com/andybalholm/brotli"
7
+ "github.com/caddyserver/caddy2"
8
+ "github.com/caddyserver/caddy2/modules/caddyhttp/encode"
9
+)
10
+
11
+func init() {
12
+ caddy2.RegisterModule(caddy2.Module{
13
+ Name: "http.encoders.brotli",
14
+ New: func() interface{} { return new(Brotli) },
15
+ })
16
+}
17
+
18
+// Brotli can create brotli encoders. Note that brotli
19
+// is not known for great encoding performance.
20
+type Brotli struct {
21
+ Quality *int `json:"quality,omitempty"`
22
+}
23
+
24
+// Validate validates b's configuration.
25
+func (b Brotli) Validate() error {
26
+ if b.Quality != nil {
27
+ quality := *b.Quality
28
+ if quality < brotli.BestSpeed {
29
+ return fmt.Errorf("quality too low; must be >= %d", brotli.BestSpeed)
30
+ }
31
+ if quality > brotli.BestCompression {
32
+ return fmt.Errorf("quality too high; must be <= %d", brotli.BestCompression)
33
+ }
34
+ }
35
+ return nil
36
+}
37
+
38
+// NewEncoder returns a new brotli writer.
39
+func (b Brotli) NewEncoder() encode.Encoder {
40
+ quality := brotli.DefaultCompression
41
+ if b.Quality != nil {
42
+ quality = *b.Quality
43
+ }
44
+ return brotli.NewWriterLevel(nil, quality)
45
+}
46
+
47
+// Interface guards
48
+var (
49
+ _ encode.Encoding = (*Brotli)(nil)
50
+ _ caddy2.Validator = (*Brotli)(nil)
51
+)
modules/caddyhttp/encode/gzip/gzip.go
+25
-4
@@ -3,6 +3,7 @@ package caddygzip
3
import (
4
"compress/flate"
5
"compress/gzip" // TODO: consider using https://github.com/klauspost/compress/gzip
6
+ "fmt"
7
8
"github.com/caddyserver/caddy2"
9
"github.com/caddyserver/caddy2/modules/caddyhttp/encode"
@@ -20,17 +21,37 @@ type Gzip struct {
21
Level int `json:"level,omitempty"`
22
}
23
23
-// NewEncoder returns a new gzip writer.
24
-func (g Gzip) NewEncoder() encode.Encoder {
25
- if g.Level <= flate.NoCompression {
24
+// Provision provisions g's configuration.
25
+func (g *Gzip) Provision(ctx caddy2.Context) error {
26
+ if g.Level == 0 {
27
g.Level = defaultGzipLevel
28
}
29
+ return nil
30
+}
31
+
32
+// Validate validates g's configuration.
33
+func (g Gzip) Validate() error {
34
+ if g.Level < flate.NoCompression {
35
+ return fmt.Errorf("quality too low; must be >= %d", flate.NoCompression)
36
+ }
37
if g.Level > flate.BestCompression {
29
- g.Level = flate.BestCompression
38
+ return fmt.Errorf("quality too high; must be <= %d", flate.BestCompression)
39
}
40
+ return nil
41
+}
42
+
43
+// NewEncoder returns a new gzip writer.
44
+func (g Gzip) NewEncoder() encode.Encoder {
45
writer, _ := gzip.NewWriterLevel(nil, g.Level)
46
return writer
47
}
48
49
// Informed from http://blog.klauspost.com/gzip-performance-for-go-webservers/
50
var defaultGzipLevel = 5
51
+
52
+// Interface guards
53
+var (
54
+ _ encode.Encoding = (*Gzip)(nil)
55
+ _ caddy2.Provisioner = (*Gzip)(nil)
56
+ _ caddy2.Validator = (*Gzip)(nil)
57
+)
modules/caddyhttp/encode/zstd/zstd.go
+4
-1
@@ -1,4 +1,4 @@
1
-package caddystd
1
+package caddyzstd
2
3
import (
4
"github.com/caddyserver/caddy2"
@@ -21,3 +21,6 @@ func (z Zstd) NewEncoder() encode.Encoder {
21
writer, _ := zstd.NewWriter(nil)
22
return writer
23
}
24
+
25
+// Interface guard
26
+var _ encode.Encoding = (*Zstd)(nil)