From 4017f94b5663da9e89a009ff4571bbb9a66c51bd Mon Sep 17 00:00:00 2001 From: magical Date: Sat, 1 Jan 2022 00:51:10 +0000 Subject: [PATCH] move MaxBytesReader to another file --- cache.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ mastodon.go | 56 ---------------------------------------------- 2 files changed, 64 insertions(+), 56 deletions(-) create mode 100644 cache.go diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..a0baef9 --- /dev/null +++ b/cache.go @@ -0,0 +1,64 @@ +package main + +import ( + "errors" + "io" +) + +type HTTPCache struct { +} + +// MaxBytesReader is similar to io.LimitReader but is intended for +// limiting the size of incoming request bodies. In contrast to +// io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a +// non-EOF error for a Read beyond the limit, and closes the +// underlying reader when its Close method is called. +// +// MaxBytesReader prevents clients from accidentally or maliciously +// sending a large request and wasting server resources. +// +// Based on http.MaxBytesReader +func MaxBytesReader(r io.ReadCloser, n int64) io.ReadCloser { + if n < 0 { // Treat negative limits as equivalent to 0. + n = 0 + } + return &maxBytesReader{r: r, n: n} +} + +type maxBytesReader struct { + r io.ReadCloser // underlying reader + n int64 // max bytes remaining + err error // sticky error +} + +func (l *maxBytesReader) Read(p []byte) (n int, err error) { + if l.err != nil { + return 0, l.err + } + if len(p) == 0 { + return 0, nil + } + // If they asked for a 32KB byte read but only 5 bytes are + // remaining, no need to read 32KB. 6 bytes will answer the + // question of the whether we hit the limit or go past it. + if int64(len(p)) > l.n+1 { + p = p[:l.n+1] + } + n, err = l.r.Read(p) + + if int64(n) <= l.n { + l.n -= int64(n) + l.err = err + return n, err + } + + n = int(l.n) + l.n = 0 + + l.err = errors.New("http: response body too large") + return n, l.err +} + +func (l *maxBytesReader) Close() error { + return l.r.Close() +} diff --git a/mastodon.go b/mastodon.go index b2fde05..bd8698f 100644 --- a/mastodon.go +++ b/mastodon.go @@ -3,7 +3,6 @@ package main import ( "bytes" "context" - "errors" "fmt" "io" "log" @@ -208,58 +207,3 @@ func text(s *goquery.Selection) string { return buf.String() } - -// MaxBytesReader is similar to io.LimitReader but is intended for -// limiting the size of incoming request bodies. In contrast to -// io.LimitReader, MaxBytesReader's result is a ReadCloser, returns a -// non-EOF error for a Read beyond the limit, and closes the -// underlying reader when its Close method is called. -// -// MaxBytesReader prevents clients from accidentally or maliciously -// sending a large request and wasting server resources. -// -// Based on http.MaxBytesReader -func MaxBytesReader(r io.ReadCloser, n int64) io.ReadCloser { - if n < 0 { // Treat negative limits as equivalent to 0. - n = 0 - } - return &maxBytesReader{r: r, n: n} -} - -type maxBytesReader struct { - r io.ReadCloser // underlying reader - n int64 // max bytes remaining - err error // sticky error -} - -func (l *maxBytesReader) Read(p []byte) (n int, err error) { - if l.err != nil { - return 0, l.err - } - if len(p) == 0 { - return 0, nil - } - // If they asked for a 32KB byte read but only 5 bytes are - // remaining, no need to read 32KB. 6 bytes will answer the - // question of the whether we hit the limit or go past it. - if int64(len(p)) > l.n+1 { - p = p[:l.n+1] - } - n, err = l.r.Read(p) - - if int64(n) <= l.n { - l.n -= int64(n) - l.err = err - return n, err - } - - n = int(l.n) - l.n = 0 - - l.err = errors.New("http: response body too large") - return n, l.err -} - -func (l *maxBytesReader) Close() error { - return l.r.Close() -}