mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 09:40:24 +08:00
28 lines
493 B
Go
28 lines
493 B
Go
|
package httphandler
|
||
|
|
||
|
import (
|
||
|
"compress/gzip"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
|
||
|
"zero/core/httpx"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
})
|
||
|
}
|