1package webapi
2
3// similar between ../moxio/limitreader.go and ../webapi/limitreader.go
4
5import (
6 "errors"
7 "io"
8)
9
10var errLimit = errors.New("input exceeds maximum size") // Returned by limitReader.
11
12// limitReader reads up to Limit bytes, returning an error if more bytes are
13// read. LimitReader can be used to enforce a maximum input length.
14type limitReader struct {
15 R io.Reader
16 Limit int64
17}
18
19// Read reads bytes from the underlying reader.
20func (r *limitReader) Read(buf []byte) (int, error) {
21 n, err := r.R.Read(buf)
22 if n > 0 {
23 r.Limit -= int64(n)
24 if r.Limit < 0 {
25 return 0, errLimit
26 }
27 }
28 return n, err
29}
30