git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/httpx/middlewarex/max_body_size.go (about)

     1  package middlewarex
     2  
     3  import "net/http"
     4  
     5  func MaxBodySize(maxSize int64) func(next http.Handler) http.Handler {
     6  	return func(next http.Handler) http.Handler {
     7  		fn := func(w http.ResponseWriter, r *http.Request) {
     8  			r.Body = http.MaxBytesReader(w, r.Body, maxSize)
     9  			next.ServeHTTP(w, r)
    10  		}
    11  		return http.HandlerFunc(fn)
    12  	}
    13  }