mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-06 23:50:01 +00:00
Add dashboard to steve
This commit is contained in:
70
pkg/responsewriter/gzip.go
Normal file
70
pkg/responsewriter/gzip.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package responsewriter
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type wrapWriter struct {
|
||||
gzipResponseWriter
|
||||
|
||||
code int
|
||||
}
|
||||
|
||||
type gzipResponseWriter struct {
|
||||
io.Writer
|
||||
http.ResponseWriter
|
||||
}
|
||||
|
||||
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
// Header logic is kept here in case the user does not use WriteHeader
|
||||
g.Header().Set("Content-Encoding", "gzip")
|
||||
g.Header().Del("Content-Length")
|
||||
|
||||
return g.Writer.Write(b)
|
||||
}
|
||||
|
||||
// Close uses gzip to write gzip footer if message is gzip encoded
|
||||
func (g gzipResponseWriter) Close(writer *gzip.Writer) {
|
||||
if g.Header().Get("Content-Encoding") == "gzip" {
|
||||
writer.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// WriteHeader sets gzip encoding and removes length. Should always be used when using gzip writer.
|
||||
func (g gzipResponseWriter) WriteHeader(statusCode int) {
|
||||
g.Header().Set("Content-Encoding", "gzip")
|
||||
g.Header().Del("Content-Length")
|
||||
g.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
// Gzip creates a gzip writer if gzip encoding is accepted.
|
||||
func Gzip(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
gz := gzip.NewWriter(w)
|
||||
|
||||
gzw := &wrapWriter{gzipResponseWriter{Writer: gz, ResponseWriter: w}, http.StatusOK}
|
||||
defer gzw.Close(gz)
|
||||
|
||||
// Content encoding will be set once Write or WriteHeader is called, to avoid gzipping empty messages
|
||||
handler.ServeHTTP(gzw, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Hijack must be implemented to properly chain with handlers expecting a hijacker handler to be passed
|
||||
func (g *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if hijacker, ok := g.ResponseWriter.(http.Hijacker); ok {
|
||||
return hijacker.Hijack()
|
||||
}
|
||||
return nil, nil, fmt.Errorf("Upstream ResponseWriter of type %v does not implement http.Hijacker", reflect.TypeOf(g.ResponseWriter))
|
||||
}
|
Reference in New Issue
Block a user