1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-08 10:45:04 +02:00

Grow download buffer to Content-Length value

This commit is contained in:
DarthSim 2019-01-21 15:04:54 +06:00
parent 19cac3dd98
commit 6fa0c539bd

View File

@ -103,10 +103,17 @@ func readAndCheckImage(ctx context.Context, res *http.Response) (context.Context
return ctx, cancel, err
}
if cls := res.Header.Get("Content-Length"); len(cls) > 0 {
if cl, err := strconv.Atoi(cls); err == nil && cl > buf.Len() && cl > buf.Cap() {
buf.Grow(cl - buf.Len())
}
var contentLength int
if res.ContentLength > 0 {
contentLength = int(res.ContentLength)
} else {
// ContentLength wasn't set properly, trying to parse the header
contentLength, _ = strconv.Atoi(res.Header.Get("Content-Length"))
}
if contentLength > buf.Cap() {
buf.Grow(contentLength - buf.Len())
}
if _, err = buf.ReadFrom(res.Body); err != nil {