aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorr <r@freesoftwareextremist.com>2023-09-24 10:38:28 +0000
committerr <r@freesoftwareextremist.com>2023-09-24 10:38:28 +0000
commit6707a01a846c459aa2f4bb51ff725193e4a7a6d3 (patch)
tree1c3f589cee95983872d75ca21da88c13d83a3637
parentcba88f94a24ad8b0c04d8fcbf2500d45156ce20f (diff)
downloadbloat-6707a01a846c459aa2f4bb51ff725193e4a7a6d3.tar.gz
bloat-6707a01a846c459aa2f4bb51ff725193e4a7a6d3.zip
Use a custom LimitedReader instead of http.MaxBytesReader
Fixes compatibility with older Go versions.
-rw-r--r--mastodon/http.go14
1 files changed, 9 insertions, 5 deletions
diff --git a/mastodon/http.go b/mastodon/http.go
index ca4c771..7d1c1c4 100644
--- a/mastodon/http.go
+++ b/mastodon/http.go
@@ -9,15 +9,19 @@ import (
type lr struct {
io.ReadCloser
+ n int64
r *http.Request
}
func (r *lr) Read(p []byte) (n int, err error) {
- n, err = r.ReadCloser.Read(p)
- // override the generic error returned by the MaxBytesReader
- if _, ok := err.(*http.MaxBytesError); ok {
- err = fmt.Errorf("%s \"%s\": response body too large", r.r.Method, r.r.URL)
+ if r.n <= 0 {
+ return 0, fmt.Errorf("%s \"%s\": response body too large", r.r.Method, r.r.URL)
+ }
+ if int64(len(p)) > r.n {
+ p = p[0:r.n]
}
+ n, err = r.ReadCloser.Read(p)
+ r.n -= int64(n)
return
}
@@ -28,7 +32,7 @@ type transport struct {
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := t.t.RoundTrip(r)
if resp != nil && resp.Body != nil {
- resp.Body = &lr{http.MaxBytesReader(nil, resp.Body, 8<<20), r}
+ resp.Body = &lr{resp.Body, 8 << 20, r}
}
return resp, err
}