1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-07 11:36:25 +02:00

Grow download buffer if Content-Length is provided

This commit is contained in:
DarthSim 2019-01-19 00:50:15 +06:00
parent d3371e95b7
commit 19cac3dd98
2 changed files with 11 additions and 4 deletions

View File

@ -29,10 +29,10 @@ func newBufPool(n int, size int) *bufPool {
func (p *bufPool) grow() {
var buf *bytes.Buffer
if p.size == 0 {
buf = new(bytes.Buffer)
} else {
buf = bytes.NewBuffer(make([]byte, p.size, p.size))
buf = new(bytes.Buffer)
if p.size > 0 {
buf.Grow(p.size)
}
p.top = &bufPoolEntry{buf: buf, next: p.top}

View File

@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
_ "image/gif"
@ -102,6 +103,12 @@ 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())
}
}
if _, err = buf.ReadFrom(res.Body); err != nil {
return ctx, cancel, newError(404, err.Error(), msgSourceImageIsUnreachable)
}