From 4cbb6fa1992f0ef4ab3d96772752a81ae2e12f90 Mon Sep 17 00:00:00 2001 From: divinerapier Date: Thu, 26 Dec 2019 11:28:43 +0800 Subject: [PATCH] feat: drains http body if buffer is too small Signed-off-by: divinerapier --- weed/util/http_util.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/weed/util/http_util.go b/weed/util/http_util.go index 025cdee7b..684b4d247 100644 --- a/weed/util/http_util.go +++ b/weed/util/http_util.go @@ -235,8 +235,16 @@ func ReadUrl(fileUrl string, offset int64, size int, buf []byte, isReadRange boo if err != nil { return n, err } + if n == int64(len(buf)) { + break + } } - + // drains the response body to avoid memory leak + data, err := ioutil.ReadAll(reader) + if len(data) != 0 { + err = fmt.Errorf("buffer size is too small. remains %d", len(data)) + } + return n, err } func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte)) (int64, error) {