@marcel91200 / caddy Mirror · read-only / commits / fee0b38b
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 14, 05:15 UTC

Fix encoder name bug; remove unused field in encode middleware struct

Matthew Holt committed Jun 29, 2019 at 16:57 UTC fee0b38b4842403a28d664385a1ff1075e8891d2
4 files changed +31 -21
modules/caddyhttp/encode/brotli/brotli.go
+5 -1
@@ -35,6 +35,10 @@ func (b Brotli) Validate() error {
35 return nil
36 }
37
38 +// AcceptEncoding returns the name of the encoding as
39 +// used in the Accept-Encoding request headers.
40 +func (Brotli) AcceptEncoding() string { return "br" }
41 +
42 // NewEncoder returns a new brotli writer.
43 func (b Brotli) NewEncoder() encode.Encoder {
44 quality := brotli.DefaultCompression
@@ -46,6 +50,6 @@ func (b Brotli) NewEncoder() encode.Encoder {
50
51 // Interface guards
52 var (
49 - _ encode.Encoding = (*Brotli)(nil)
53 + _ encode.Encoding = (*Brotli)(nil)
54 _ caddy.Validator = (*Brotli)(nil)
55 )
modules/caddyhttp/encode/encode.go
+17 -19
@@ -1,8 +1,8 @@
1 // Package encode implements an encoder middleware for Caddy. The initial
2 // enhancements related to Accept-Encoding, minimum content length, and
3 // buffer/writer pools were adapted from https://github.com/xi2/httpgzip
4 -// then modified heavily to accommodate modular encoders. Code borrowed
5 -// from that repository is Copyright (c) 2015 The Httpgzip Authors.
4 +// then modified heavily to accommodate modular encoders and fix bugs.
5 +// Code borrowed from that repository is Copyright (c) 2015 The Httpgzip Authors.
6 package encode
7
8 import (
@@ -33,14 +33,11 @@ type Encode struct {
33 Prefer []string `json:"prefer,omitempty"`
34 MinLength int `json:"minimum_length,omitempty"`
35
36 - Encodings map[string]Encoding `json:"-"`
37 -
36 writerPools map[string]*sync.Pool // TODO: these pools do not get reused through config reloads...
37 }
38
39 // Provision provisions enc.
40 func (enc *Encode) Provision(ctx caddy.Context) error {
43 - enc.Encodings = make(map[string]Encoding)
41 enc.writerPools = make(map[string]*sync.Pool)
42
43 for modName, rawMsg := range enc.EncodingsRaw {
@@ -49,9 +46,8 @@ func (enc *Encode) Provision(ctx caddy.Context) error {
46 return fmt.Errorf("loading encoder module '%s': %v", modName, err)
47 }
48 encoder := val.(Encoding)
52 - enc.Encodings[modName] = encoder
49
54 - enc.writerPools[modName] = &sync.Pool{
50 + enc.writerPools[encoder.AcceptEncoding()] = &sync.Pool{
51 New: func() interface{} {
52 return encoder.NewEncoder()
53 },
@@ -159,17 +155,6 @@ func (rw *responseWriter) Write(p []byte) (int, error) {
155 return n, err
156 }
157
162 -// init should be called before we write a response, if rw.buf has contents.
163 -func (rw *responseWriter) init() {
164 - if rw.Header().Get("Content-Encoding") == "" && rw.buf.Len() >= rw.config.MinLength {
165 - rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder)
166 - rw.w.Reset(rw.ResponseWriter)
167 - rw.Header().Del("Content-Length") // https://github.com/golang/go/issues/14975
168 - rw.Header().Set("Content-Encoding", rw.encodingName)
169 - }
170 - rw.Header().Del("Accept-Ranges") // we don't know ranges for dynamically-encoded content
171 -}
172 -
158 // Close writes any remaining buffered response and
159 // deallocates any active resources.
160 func (rw *responseWriter) Close() error {
@@ -213,6 +198,17 @@ func (rw *responseWriter) Close() error {
198 return err
199 }
200
201 +// init should be called before we write a response, if rw.buf has contents.
202 +func (rw *responseWriter) init() {
203 + if rw.Header().Get("Content-Encoding") == "" && rw.buf.Len() >= rw.config.MinLength {
204 + rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder)
205 + rw.w.Reset(rw.ResponseWriter)
206 + rw.Header().Del("Content-Length") // https://github.com/golang/go/issues/14975
207 + rw.Header().Set("Content-Encoding", rw.encodingName)
208 + }
209 + rw.Header().Del("Accept-Ranges") // we don't know ranges for dynamically-encoded content
210 +}
211 +
212 // acceptedEncodings returns the list of encodings that the
213 // client supports, in descending order of preference. If
214 // the Sec-WebSocket-Key header is present then non-identity
@@ -288,8 +284,10 @@ type Encoder interface {
284 Reset(io.Writer)
285 }
286
291 -// Encoding is a type which can create encoders of its kind.
287 +// Encoding is a type which can create encoders of its kind
288 +// and return the name used in the Accept-Encoding header.
289 type Encoding interface {
290 + AcceptEncoding() string
291 NewEncoder() Encoder
292 }
293
modules/caddyhttp/encode/gzip/gzip.go
+5 -1
@@ -40,6 +40,10 @@ func (g Gzip) Validate() error {
40 return nil
41 }
42
43 +// AcceptEncoding returns the name of the encoding as
44 +// used in the Accept-Encoding request headers.
45 +func (Gzip) AcceptEncoding() string { return "gzip" }
46 +
47 // NewEncoder returns a new gzip writer.
48 func (g Gzip) NewEncoder() encode.Encoder {
49 writer, _ := gzip.NewWriterLevel(nil, g.Level)
@@ -51,7 +55,7 @@ var defaultGzipLevel = 5
55
56 // Interface guards
57 var (
54 - _ encode.Encoding = (*Gzip)(nil)
58 + _ encode.Encoding = (*Gzip)(nil)
59 _ caddy.Provisioner = (*Gzip)(nil)
60 _ caddy.Validator = (*Gzip)(nil)
61 )
modules/caddyhttp/encode/zstd/zstd.go
+4
@@ -16,6 +16,10 @@ func init() {
16 // Zstd can create zstd encoders.
17 type Zstd struct{}
18
19 +// AcceptEncoding returns the name of the encoding as
20 +// used in the Accept-Encoding request headers.
21 +func (Zstd) AcceptEncoding() string { return "zstd" }
22 +
23 // NewEncoder returns a new gzip writer.
24 func (z Zstd) NewEncoder() encode.Encoder {
25 writer, _ := zstd.NewWriter(nil)