Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (41 sloc) 1008 Bytes
package util
import (
"net/http"
"compress/gzip"
"strings"
)
type CloseableResponseWriter interface {
http.ResponseWriter
Close()
}
type gzipResponseWriter struct {
http.ResponseWriter
*gzip.Writer
}
func (this gzipResponseWriter) Write(data []byte) (int, error){
return this.Writer.Write(data)
}
func (this gzipResponseWriter) Close() {
this.Writer.Close()
}
func (this gzipResponseWriter) Header() http.Header {
return this.ResponseWriter.Header()
}
type closeableResponseWriter struct {
http.ResponseWriter
}
func (this closeableResponseWriter) Close() {
}
func GetResponseWriter(w http.ResponseWriter, req *http.Request) CloseableResponseWriter{
// Accept-Encoding告知服务器采用何种压缩方式
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gRW := gzipResponseWriter{
ResponseWriter: w,
Writer: gzip.NewWriter(w),
}
return gRW
} else {
return closeableResponseWriter{ResponseWriter: w}
}
}