mirror of
https://git.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-24 21:01:28 +00:00
Use a custom LimitedReader instead of http.MaxBytesReader
Fixes compatibility with older Go versions.
This commit is contained in:
parent
cba88f94a2
commit
6707a01a84
|
@ -9,15 +9,19 @@ import (
|
||||||
|
|
||||||
type lr struct {
|
type lr struct {
|
||||||
io.ReadCloser
|
io.ReadCloser
|
||||||
|
n int64
|
||||||
r *http.Request
|
r *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *lr) Read(p []byte) (n int, err error) {
|
func (r *lr) Read(p []byte) (n int, err error) {
|
||||||
n, err = r.ReadCloser.Read(p)
|
if r.n <= 0 {
|
||||||
// override the generic error returned by the MaxBytesReader
|
return 0, fmt.Errorf("%s \"%s\": response body too large", r.r.Method, r.r.URL)
|
||||||
if _, ok := err.(*http.MaxBytesError); ok {
|
|
||||||
err = 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +32,7 @@ type transport struct {
|
||||||
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
|
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||||
resp, err := t.t.RoundTrip(r)
|
resp, err := t.t.RoundTrip(r)
|
||||||
if resp != nil && resp.Body != nil {
|
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
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue