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 ¶
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 ¶
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 ¶
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) Is ¶
func (*LimitError) Is(target error) bool
Is reports whether target is ErrTooLarge.