37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gzip.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
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) {
return g.Writer.Write(b)
}
// should always be used when using gzip to overwrite outdated header info
func (g gzipResponseWriter) WriteHeader(statusCode int) {
g.Header().Set("Content-Encoding", "gzip")
g.Header().Del("Content-Length")
g.ResponseWriter.WriteHeader(statusCode)
}
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)
defer gz.Close()
gzw := &wrapWriter{gzipResponseWriter{Writer: gz, ResponseWriter: w}, http.StatusOK}
// setting content-encoding is kept here in case the user does not use WriteHeader
gzw.Header().Set("Content-Encoding", "gzip")
handler.ServeHTTP(gzw, r)
})
}
// Must implement Hijacker 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))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.3-rc7

搜索帮助