mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
Gzip: Fix missing gzip encoding headers.
This commit is contained in:
parent
35ec61cc88
commit
f04ff063ed
4 changed files with 13 additions and 8 deletions
|
@ -57,7 +57,7 @@ outer:
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
defer gzipWriter.Close()
|
defer gzipWriter.Close()
|
||||||
gz := gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
|
gz := &gzipResponseWriter{Writer: gzipWriter, ResponseWriter: w}
|
||||||
|
|
||||||
var rw http.ResponseWriter
|
var rw http.ResponseWriter
|
||||||
// if no response filter is used
|
// if no response filter is used
|
||||||
|
@ -104,21 +104,26 @@ func newWriter(c Config, w io.Writer) (*gzip.Writer, error) {
|
||||||
type gzipResponseWriter struct {
|
type gzipResponseWriter struct {
|
||||||
io.Writer
|
io.Writer
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
|
statusCodeWritten bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteHeader wraps the underlying WriteHeader method to prevent
|
// WriteHeader wraps the underlying WriteHeader method to prevent
|
||||||
// problems with conflicting headers from proxied backends. For
|
// problems with conflicting headers from proxied backends. For
|
||||||
// example, a backend system that calculates Content-Length would
|
// example, a backend system that calculates Content-Length would
|
||||||
// be wrong because it doesn't know it's being gzipped.
|
// be wrong because it doesn't know it's being gzipped.
|
||||||
func (w gzipResponseWriter) WriteHeader(code int) {
|
func (w *gzipResponseWriter) WriteHeader(code int) {
|
||||||
w.Header().Del("Content-Length")
|
w.Header().Del("Content-Length")
|
||||||
w.Header().Set("Content-Encoding", "gzip")
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
w.Header().Set("Vary", "Accept-Encoding")
|
w.Header().Set("Vary", "Accept-Encoding")
|
||||||
w.ResponseWriter.WriteHeader(code)
|
w.ResponseWriter.WriteHeader(code)
|
||||||
|
w.statusCodeWritten = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write wraps the underlying Write method to do compression.
|
// Write wraps the underlying Write method to do compression.
|
||||||
func (w gzipResponseWriter) Write(b []byte) (int, error) {
|
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
if !w.statusCodeWritten {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
if w.Header().Get("Content-Type") == "" {
|
if w.Header().Get("Content-Type") == "" {
|
||||||
w.Header().Set("Content-Type", http.DetectContentType(b))
|
w.Header().Set("Content-Type", http.DetectContentType(b))
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ func nextFunc(shouldGzip bool) middleware.Handler {
|
||||||
if w.Header().Get("Vary") != "Accept-Encoding" {
|
if w.Header().Get("Vary") != "Accept-Encoding" {
|
||||||
return 0, fmt.Errorf("Vary must be Accept-Encoding, found %v", r.Header.Get("Vary"))
|
return 0, fmt.Errorf("Vary must be Accept-Encoding, found %v", r.Header.Get("Vary"))
|
||||||
}
|
}
|
||||||
if _, ok := w.(gzipResponseWriter); !ok {
|
if _, ok := w.(*gzipResponseWriter); !ok {
|
||||||
return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w)
|
return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w)
|
||||||
}
|
}
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
@ -103,7 +103,7 @@ func nextFunc(shouldGzip bool) middleware.Handler {
|
||||||
if w.Header().Get("Content-Encoding") == "gzip" {
|
if w.Header().Get("Content-Encoding") == "gzip" {
|
||||||
return 0, fmt.Errorf("Content-Encoding must not be gzip, found gzip")
|
return 0, fmt.Errorf("Content-Encoding must not be gzip, found gzip")
|
||||||
}
|
}
|
||||||
if _, ok := w.(gzipResponseWriter); ok {
|
if _, ok := w.(*gzipResponseWriter); ok {
|
||||||
return 0, fmt.Errorf("ResponseWriter should not be gzipResponseWriter")
|
return 0, fmt.Errorf("ResponseWriter should not be gzipResponseWriter")
|
||||||
}
|
}
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
|
|
@ -31,11 +31,11 @@ func (l LengthFilter) ShouldCompress(w http.ResponseWriter) bool {
|
||||||
type ResponseFilterWriter struct {
|
type ResponseFilterWriter struct {
|
||||||
filters []ResponseFilter
|
filters []ResponseFilter
|
||||||
shouldCompress bool
|
shouldCompress bool
|
||||||
gzipResponseWriter
|
*gzipResponseWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.
|
// NewResponseFilterWriter creates and initializes a new ResponseFilterWriter.
|
||||||
func NewResponseFilterWriter(filters []ResponseFilter, gz gzipResponseWriter) *ResponseFilterWriter {
|
func NewResponseFilterWriter(filters []ResponseFilter, gz *gzipResponseWriter) *ResponseFilterWriter {
|
||||||
return &ResponseFilterWriter{filters: filters, gzipResponseWriter: gz}
|
return &ResponseFilterWriter{filters: filters, gzipResponseWriter: gz}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestLengthFilter(t *testing.T) {
|
||||||
for j, filter := range filters {
|
for j, filter := range filters {
|
||||||
r := httptest.NewRecorder()
|
r := httptest.NewRecorder()
|
||||||
r.Header().Set("Content-Length", fmt.Sprint(ts.length))
|
r.Header().Set("Content-Length", fmt.Sprint(ts.length))
|
||||||
wWriter := NewResponseFilterWriter([]ResponseFilter{filter}, gzipResponseWriter{gzip.NewWriter(r), r})
|
wWriter := NewResponseFilterWriter([]ResponseFilter{filter}, &gzipResponseWriter{gzip.NewWriter(r), r, false})
|
||||||
if filter.ShouldCompress(wWriter) != ts.shouldCompress[j] {
|
if filter.ShouldCompress(wWriter) != ts.shouldCompress[j] {
|
||||||
t.Errorf("Test %v: Expected %v found %v", i, ts.shouldCompress[j], filter.ShouldCompress(r))
|
t.Errorf("Test %v: Expected %v found %v", i, ts.shouldCompress[j], filter.ShouldCompress(r))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue