boundedio

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package boundedio reads a stream into memory within a limit. It is the one place that reads a body, a file or an object whose size the caller does not trust: Read for any reader, ReadBody and LimitBody for the body of an HTTP request. A stream with more than the limit is refused with ErrTooLarge, carried by a LimitError that names the limit and never the content.

Index

Constants

This section is empty.

Variables

View Source
var ErrTooLarge = errors.New("too large")

ErrTooLarge is the error of a stream that has more bytes than the limit.

Functions

func LimitBody

func LimitBody(w http.ResponseWriter, r *http.Request, limit int64)

LimitBody makes the body of the request fail after limit bytes with a *http.MaxBytesError, and tells the server to close the connection after the answer. Use it before a decoder that reads the body as a stream; TooLarge recognises the error it returns.

func Read

func Read(r io.Reader, limit int64) ([]byte, error)

Read reads r to its end and returns what it read. It reads at most limit bytes: a stream with one byte more is a *LimitError, and nothing is returned. A read error is returned as it is.

func ReadBody

func ReadBody(w http.ResponseWriter, body io.ReadCloser, limit int64) ([]byte, error)

ReadBody reads the body of a request within limit, like Read, and has the server close the connection when the body is longer (see http.MaxBytesReader). A body that is too large is a *LimitError; any other error is the error of the read.

func TooLarge

func TooLarge(err error) bool

TooLarge reports whether err is a stream with more than its limit: a *LimitError, or the *http.MaxBytesError of a body that LimitBody bounded.

Types

type LimitError

type LimitError struct {
	Limit int64
}

LimitError is the error of a stream with more than Limit bytes. It matches ErrTooLarge with errors.Is. It holds the limit only, never what was read.

func (*LimitError) Error

func (e *LimitError) Error() string

Error describes the limit.

func (*LimitError) Is

func (*LimitError) Is(target error) bool

Is reports whether target is ErrTooLarge.

Jump to

Keyboard shortcuts

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