rename ngin to rest
This commit is contained in:
27
rest/handler/maxbyteshandler.go
Normal file
27
rest/handler/maxbyteshandler.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"zero/rest/internal"
|
||||
)
|
||||
|
||||
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 {
|
||||
internal.Errorf(r, "request entity too large, limit is %d, but got %d, rejected with code %d",
|
||||
n, r.ContentLength, http.StatusRequestEntityTooLarge)
|
||||
w.WriteHeader(http.StatusRequestEntityTooLarge)
|
||||
} else {
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user