body

package
v0.18.4 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RequestBufferer

type RequestBufferer struct{}

RequestBufferer reads entire request body before send to next middleware

func BufferRequest

func BufferRequest() *RequestBufferer

BufferRequest creates new request bufferer

Example

Read the entire request body before forwarding it upstream. Small bodies are held in memory and larger ones spilled to a temp file, so the upstream always sees a known Content-Length. Useful for upstreams that can't handle chunked or slow-trickling request bodies.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/body"
)

func main() {
	s := parapet.New()
	s.Use(body.BufferRequest())
}

func (RequestBufferer) ServeHandler

func (m RequestBufferer) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

type RequestLimiter

type RequestLimiter struct {
	Size           int64
	LimitedHandler http.Handler
}

RequestLimiter limits request body size

func LimitRequest

func LimitRequest(size int64) *RequestLimiter

LimitRequest creates new request limiter

Example

Reject request bodies larger than the given size. Requests advertising a Content-Length over the limit are refused outright; chunked requests with no declared length are streamed and cut off once the limit is exceeded. A size of -1 disables the limit.

package main

import (
	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/body"
)

func main() {
	s := parapet.New()
	s.Use(body.LimitRequest(10 << 20)) // cap request bodies at 10 MiB
}
Example (CustomHandler)

Replace the default "413 Request Entity Too Large" response with a custom handler invoked when a request exceeds the limit.

package main

import (
	"net/http"

	"github.com/moonrhythm/parapet"
	"github.com/moonrhythm/parapet/pkg/body"
)

func main() {
	m := body.LimitRequest(1 << 20)
	m.LimitedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "upload too big, max 1 MiB", http.StatusRequestEntityTooLarge)
	})

	s := parapet.New()
	s.Use(m)
}

func (RequestLimiter) ServeHandler

func (m RequestLimiter) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

Jump to

Keyboard shortcuts

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