2023-09-17 09:44:02 +00:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type lr struct {
|
|
|
|
io.ReadCloser
|
2023-09-24 10:38:28 +00:00
|
|
|
n int64
|
2023-09-17 09:44:02 +00:00
|
|
|
r *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *lr) Read(p []byte) (n int, err error) {
|
2023-09-24 10:38:28 +00:00
|
|
|
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]
|
2023-09-17 09:44:02 +00:00
|
|
|
}
|
2023-09-24 10:38:28 +00:00
|
|
|
n, err = r.ReadCloser.Read(p)
|
|
|
|
r.n -= int64(n)
|
2023-09-17 09:44:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type transport struct {
|
|
|
|
t http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
resp, err := t.t.RoundTrip(r)
|
|
|
|
if resp != nil && resp.Body != nil {
|
2023-09-24 10:38:28 +00:00
|
|
|
resp.Body = &lr{resp.Body, 8 << 20, r}
|
2023-09-17 09:44:02 +00:00
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var httpClient = &http.Client{
|
|
|
|
Transport: &transport{
|
|
|
|
t: http.DefaultTransport,
|
|
|
|
},
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}
|