bodylimit

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 4 Imported by: 0

README

Body limit

arkoren.dev/core/http/middleware/bodylimit caps request body sizes for a router or group.

On an existing r router, limit uploads to 1 MiB:

r.Use(bodylimit.Middleware(func(limiter *bodylimit.Builder) {
	limiter.Limit(1 << 20)
})) // 1 MiB

A declared Content-Length over the limit gets 413 before the handler runs; streamed bodies fail the read once past the limit. The request body helpers (request.JSON, request.Bytes, ...) use this limit instead of their 10 MiB default.

Without a callback, Middleware() uses request.DefaultMaxBodySize. Zero or negative Limit values also select that default; they do not disable the ceiling. The wrapped body is a hard ceiling: request options and nested middleware cannot raise it. Mount routes that accept larger bodies outside this middleware.

A nil Body is empty input, so request.JSON returns request.ErrInvalidJSON (400). The declared Content-Length is still checked first. The middleware replaces the context and body on a request copy, leaving the caller's request fields unchanged. A non-nil body stream remains shared: downstream reads consume it, and closing the wrapped body closes the original stream.

See the Body Limit guide for request-helper behavior and streaming limits.

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

func (limiter *Builder) Limit(limit int64) *Builder

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL