mirror of
https://git.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-25 13:21:28 +00:00
Set timeout and response size limit for the http client
This commit is contained in:
parent
60ccc9686a
commit
ad38855261
|
@ -11,7 +11,6 @@ import (
|
||||||
|
|
||||||
// AppConfig is a setting for registering applications.
|
// AppConfig is a setting for registering applications.
|
||||||
type AppConfig struct {
|
type AppConfig struct {
|
||||||
http.Client
|
|
||||||
Server string
|
Server string
|
||||||
ClientName string
|
ClientName string
|
||||||
|
|
||||||
|
@ -62,7 +61,7 @@ func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error
|
||||||
}
|
}
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
resp, err := appConfig.Do(req)
|
resp, err := httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
41
mastodon/http.go
Normal file
41
mastodon/http.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package mastodon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type lr struct {
|
||||||
|
io.ReadCloser
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
resp.Body = &lr{http.MaxBytesReader(nil, resp.Body, 8<<20), r}
|
||||||
|
}
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpClient = &http.Client{
|
||||||
|
Transport: &transport{
|
||||||
|
t: http.DefaultTransport,
|
||||||
|
},
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
}
|
|
@ -168,12 +168,13 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return json.NewDecoder(resp.Body).Decode(&res)
|
return json.NewDecoder(resp.Body).Decode(&res)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient return new mastodon API client.
|
// NewClient return new mastodon API client.
|
||||||
func NewClient(config *Config) *Client {
|
func NewClient(config *Config) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
Client: http.DefaultClient,
|
Client: httpClient,
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue