github.com/lingyao2333/mo-zero@v1.4.1/rest/handler/gunziphandler.go (about) 1 package handler 2 3 import ( 4 "compress/gzip" 5 "net/http" 6 "strings" 7 8 "github.com/lingyao2333/mo-zero/rest/httpx" 9 ) 10 11 const gzipEncoding = "gzip" 12 13 // GunzipHandler returns a middleware to gunzip http request body. 14 func GunzipHandler(next http.Handler) http.Handler { 15 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 16 if strings.Contains(r.Header.Get(httpx.ContentEncoding), gzipEncoding) { 17 reader, err := gzip.NewReader(r.Body) 18 if err != nil { 19 w.WriteHeader(http.StatusBadRequest) 20 return 21 } 22 23 r.Body = reader 24 } 25 26 next.ServeHTTP(w, r) 27 }) 28 }