limitResponseSize limits the size of the response body to MaxSrcFileSize (if set). First, it tries to use Content-Length header to check the limit. If Content-Length is not set, it limits the size of the response body by wrapping body reader with hard limit reader.
(r *http.Response, limit int)
| 33 | // If Content-Length is not set, it limits the size of the response body by wrapping |
| 34 | // body reader with hard limit reader. |
| 35 | func limitResponseSize(r *http.Response, limit int) (*http.Response, error) { |
| 36 | if limit == 0 { |
| 37 | return r, nil |
| 38 | } |
| 39 | |
| 40 | // If Content-Length was set, limit the size of the response body before reading it |
| 41 | size := int(r.ContentLength) |
| 42 | if size > limit { |
| 43 | return nil, newFileSizeError() |
| 44 | } |
| 45 | |
| 46 | // hard-limit the response body reader |
| 47 | r.Body = &hardLimitReadCloser{r: r.Body, left: limit} |
| 48 | |
| 49 | return r, nil |
| 50 | } |
no test coverage detected