rework gzip compressor so it is lazily created for 200 OK requests only

* fix issue when panicking (before Write) gzip writer is closed, causing
header to be written and default status of 200 OK being written.
* update recovery middleware to set 500 Internal Server Error
pull/8452/head
Stuart Carnie 2017-06-03 09:11:58 +08:00
parent 9520a0b9cb
commit 55d1ba6d79
2 changed files with 105 additions and 63 deletions

104
services/httpd/gzip.go Normal file
View File

@ -0,0 +1,104 @@
package httpd
import (
"compress/gzip"
"io"
"net/http"
"strings"
"sync"
)
type lazyGzipResponseWriter struct {
io.Writer
http.ResponseWriter
http.Flusher
http.CloseNotifier
wroteHeader bool
}
// gzipFilter determines if the client can accept compressed responses, and encodes accordingly.
func gzipFilter(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
inner.ServeHTTP(w, r)
return
}
gw := &lazyGzipResponseWriter{ResponseWriter: w, Writer: w}
if f, ok := w.(http.Flusher); ok {
gw.Flusher = f
}
if cn, ok := w.(http.CloseNotifier); ok {
gw.CloseNotifier = cn
}
defer gw.Close()
inner.ServeHTTP(gw, r)
})
}
func (w *lazyGzipResponseWriter) WriteHeader(code int) {
if w.wroteHeader {
return
}
w.wroteHeader = true
if code == http.StatusOK {
w.Header().Set("Content-Encoding", "gzip")
// Add gzip compressor
if _, ok := w.Writer.(*gzip.Writer); !ok {
w.Writer = getGzipWriter(w.Writer)
}
}
w.ResponseWriter.WriteHeader(code)
}
func (w *lazyGzipResponseWriter) Write(p []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
}
return w.Writer.Write(p)
}
func (w *lazyGzipResponseWriter) Flush() {
// Flush writer, if supported
if f, ok := w.Writer.(interface {
Flush()
}); ok {
f.Flush()
}
// Flush the HTTP response
if w.Flusher != nil {
w.Flusher.Flush()
}
}
func (w *lazyGzipResponseWriter) Close() error {
if gw, ok := w.Writer.(*gzip.Writer); ok {
putGzipWriter(gw)
}
return nil
}
var gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
},
}
func getGzipWriter(w io.Writer) *gzip.Writer {
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
return gz
}
func putGzipWriter(gz *gzip.Writer) {
gz.Close()
gzipWriterPool.Put(gz)
}

View File

@ -14,7 +14,6 @@ import (
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -1110,68 +1109,6 @@ func authenticate(inner func(http.ResponseWriter, *http.Request, meta.User), h *
}) })
} }
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
// WriteHeader sets the provided code as the response status. If the
// specified status is 204 No Content, then the Content-Encoding header
// is removed from the response, to prevent clients expecting gzipped
// encoded bodies from trying to deflate an empty response.
func (w gzipResponseWriter) WriteHeader(code int) {
if code != http.StatusNoContent {
w.Header().Set("Content-Encoding", "gzip")
}
w.ResponseWriter.WriteHeader(code)
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (w gzipResponseWriter) Flush() {
w.Writer.(*gzip.Writer).Flush()
if w, ok := w.ResponseWriter.(http.Flusher); ok {
w.Flush()
}
}
func (w gzipResponseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
// gzipFilter determines if the client can accept compressed responses, and encodes accordingly.
func gzipFilter(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
inner.ServeHTTP(w, r)
return
}
gz := getGzipWriter(w)
defer putGzipWriter(gz)
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
inner.ServeHTTP(gzw, r)
})
}
var gzipWriterPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
},
}
func getGzipWriter(w io.Writer) *gzip.Writer {
gz := gzipWriterPool.Get().(*gzip.Writer)
gz.Reset(w)
return gz
}
func putGzipWriter(gz *gzip.Writer) {
gz.Close()
gzipWriterPool.Put(gz)
}
// cors responds to incoming requests and adds the appropriate cors headers // cors responds to incoming requests and adds the appropriate cors headers
// TODO: corylanou: add the ability to configure this in our config // TODO: corylanou: add the ability to configure this in our config
func cors(inner http.Handler) http.Handler { func cors(inner http.Handler) http.Handler {
@ -1246,6 +1183,7 @@ func (h *Handler) recovery(inner http.Handler, name string) http.Handler {
logLine := buildLogLine(l, r, start) logLine := buildLogLine(l, r, start)
logLine = fmt.Sprintf("%s [panic:%s] %s", logLine, err, debug.Stack()) logLine = fmt.Sprintf("%s [panic:%s] %s", logLine, err, debug.Stack())
h.CLFLogger.Println(logLine) h.CLFLogger.Println(logLine)
http.Error(w, http.StatusText(http.StatusInternalServerError), 500)
} }
}() }()