Documentation
¶
Overview ¶
Package bodylimit caps request body reads for a router or group. Declared oversized bodies receive 413 before the next handler runs; bodies that exceed the ceiling while streaming fail the handler's read. The default ceiling is request.DefaultMaxBodySize, also selected by a zero or negative configured limit.
Nil bodies are treated as empty input, so request.JSON returns request.ErrInvalidJSON (400). The caller's request fields stay unchanged; non-nil body streams remain shared with the downstream handler.
Configure a smaller ceiling before registering upload routes:
routes := router.New()
routes.Use(bodylimit.Middleware(func(limiter *bodylimit.Builder) {
limiter.Limit(1 << 20)
}))
Request helper options and nested middleware cannot raise an existing ceiling. Mount routes requiring larger bodies outside that middleware.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(configure ...BuilderFunc) router.Middleware
Middleware returns middleware that caps request bodies for a whole router or group. A request whose Content-Length exceeds the limit is rejected with request.ErrBodyTooLarge (413) before the next handler runs. Otherwise the limit is stored in the request context, where the request body helpers read it instead of request.DefaultMaxBodySize, and the body is wrapped in http.MaxBytesReader, so a body that turns out larger while streaming (or has no Content-Length) fails the handler's read.
A nil body is treated as empty input. The middleware replaces the context and body only on a shallow request copy; it does not read or close the body. A non-nil body stream remains shared: downstream reads consume it, and closing the wrapped body closes the original stream.
The wrapped body is a hard ceiling: neither request.MaxSize nor request.Unlimited, nor a nested bodylimit middleware, can raise it. Mount routes that accept larger bodies outside this middleware.
Example:
r := router.New()
r.Use(bodylimit.Middleware(func(limiter *bodylimit.Builder) {
limiter.Limit(1 << 20)
})) // 1 MiB
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder configures Middleware before publication. Callbacks run directly once, synchronously in order on one fresh draft; nil entries panic. Retaining the draft cannot change published settings. Methods return the same draft.
Example:
r := router.New()
r.Use(bodylimit.Middleware(func(limiter *bodylimit.Builder) {
limiter.Limit(1 << 20)
}))
func (*Builder) Limit ¶
Limit is the maximum body size in bytes. Zero or negative uses request.DefaultMaxBodySize.
The setting replaces the previous value on this construction draft. Publication keeps independent settings; functions and services stay shared.
Example:
r := router.New()
r.Use(bodylimit.Middleware(func(limiter *bodylimit.Builder) {
limiter.Limit(1024)
}))
type BuilderFunc ¶
type BuilderFunc = func(*Builder)
BuilderFunc edits the construction draft shared by Middleware's callbacks. Callbacks run synchronously in order; a supplied nil callback panics, while expanding a nil or empty callback slice makes no edits. Zero or negative limits select the default ceiling after all callbacks have completed.
Example:
configure := bodylimit.BuilderFunc(func(limiter *bodylimit.Builder) {
limiter.Limit(1 << 20)
})
routes := router.New()
routes.Use(bodylimit.Middleware(configure))
Example ¶
ExampleBuilderFunc rejects a declared oversized body before route handling, demonstrating the configured ceiling through an HTTP response.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"arkoren.dev/core/http/middleware/bodylimit"
"arkoren.dev/core/http/router"
)
func main() {
configure := bodylimit.BuilderFunc(func(limiter *bodylimit.Builder) {
limiter.Limit(1024)
})
routes := router.New()
routes.Use(bodylimit.Middleware(configure))
request := httptest.NewRequest(http.MethodPost, "/upload", strings.NewReader(strings.Repeat("x", 1025)))
recorder := httptest.NewRecorder()
routes.ServeHTTP(recorder, request)
fmt.Println(recorder.Code)
}
Output: 413