ba9d5540b8
* Introduce response_limit for datasource responses * Fix lint * Fix tests * Add case where limit <= 0 - added parametrized tests * Add max_bytes_reader.go * Use new httpclient.MaxBytesReader instead of net/http one * Fixes according to reviewer's comments * Add tests for max_bytes_reader * Add small piece in configuration.md * Further fixes according to reviewer's comments * Fix linting - fix test
29 lines
833 B
Go
29 lines
833 B
Go
package httpclientprovider
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
|
)
|
|
|
|
// ResponseLimitMiddlewareName is the middleware name used by ResponseLimitMiddleware.
|
|
const ResponseLimitMiddlewareName = "response-limit"
|
|
|
|
func ResponseLimitMiddleware(limit int64) sdkhttpclient.Middleware {
|
|
return sdkhttpclient.NamedMiddlewareFunc(ResponseLimitMiddlewareName, func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
|
|
if limit <= 0 {
|
|
return next
|
|
}
|
|
return sdkhttpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
res, err := next.RoundTrip(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res.Body = httpclient.MaxBytesReader(res.Body, limit)
|
|
return res, nil
|
|
})
|
|
})
|
|
}
|