Skip to content

Commit

Permalink
Request size middleware (#808) (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
farazfazli committed Mar 31, 2023
1 parent 51068a7 commit fd8a51e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions middleware/request_size.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import (
"net/http"
)

// RequestSize is a middleware that will limit request sizes to a specified
// number of bytes. It uses MaxBytesReader to do so.
func RequestSize(bytes int64) func(http.Handler) http.Handler {
f := func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, bytes)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
return f
}

0 comments on commit fd8a51e

Please sign in to comment.