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.
go-zero/rest/handler/maxbyteshandler.go

28 lines
622 B
Go

4 years ago
package handler
4 years ago
import (
"net/http"
"zero/rest/internal"
4 years ago
)
func MaxBytesHandler(n int64) func(http.Handler) http.Handler {
if n <= 0 {
return func(next http.Handler) http.Handler {
return next
}
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ContentLength > n {
4 years ago
internal.Errorf(r, "request entity too large, limit is %d, but got %d, rejected with code %d",
4 years ago
n, r.ContentLength, http.StatusRequestEntityTooLarge)
w.WriteHeader(http.StatusRequestEntityTooLarge)
} else {
next.ServeHTTP(w, r)
}
})
}
}