go-zero/rest/handler/gunziphandler.go

28 lines
512 B
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package handler
2020-07-26 17:09:05 +08:00
import (
"compress/gzip"
"net/http"
"strings"
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/rest/httpx"
2020-07-26 17:09:05 +08:00
)
const gzipEncoding = "gzip"
func GunzipHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get(httpx.ContentEncoding), gzipEncoding) {
reader, err := gzip.NewReader(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body = reader
}
next.ServeHTTP(w, r)
})
}