You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
489 B
Go
28 lines
489 B
Go
4 years ago
|
package handler
|
||
4 years ago
|
|
||
|
import (
|
||
|
"compress/gzip"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
|
||
4 years ago
|
"zero/ngin/httpx"
|
||
4 years ago
|
)
|
||
|
|
||
|
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)
|
||
|
})
|
||
|
}
|