refactor(10.2-01): nest framework packages under modules
- Move remaining beach packages and embedded admin assets\n- Rewrite framework, example, build, and gate paths
This commit is contained in:
46
modules/surf/bodylimit.go
Normal file
46
modules/surf/bodylimit.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func bodyLimit(n int64) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if n > 0 && r.Body != nil {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, n)
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func parseBodyLimit(param string) (int64, error) {
|
||||
n, err := strconv.ParseInt(param, 10, 64)
|
||||
if err != nil || n <= 0 {
|
||||
return 0, fmt.Errorf("invalid body.limit %q", param)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func routeBodyLimit(rt route, defaultBytes int64) (int64, error) {
|
||||
if rt.raw {
|
||||
return 0, nil
|
||||
}
|
||||
limit := defaultBytes
|
||||
for _, name := range rt.middleware {
|
||||
base, param, ok := strings.Cut(name, ":")
|
||||
if !ok || base != "body.limit" {
|
||||
continue
|
||||
}
|
||||
n, err := parseBodyLimit(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
return limit, nil
|
||||
}
|
||||
Reference in New Issue
Block a user