aboutsummaryrefslogtreecommitdiff
path: root/weed/util/http_util.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2020-02-14 09:09:15 -0800
committerChris Lu <chris.lu@gmail.com>2020-02-14 09:09:15 -0800
commitcf5064d7025c6b74ac721b8914a76d5f1d155836 (patch)
treebec2f98b7970c518f11353eaa5dd609e5821b8f1 /weed/util/http_util.go
parent9b6296e77a66533664507c833209aefd5de56337 (diff)
downloadseaweedfs-cf5064d7025c6b74ac721b8914a76d5f1d155836.tar.xz
seaweedfs-cf5064d7025c6b74ac721b8914a76d5f1d155836.zip
properly close http response
Diffstat (limited to 'weed/util/http_util.go')
-rw-r--r--weed/util/http_util.go19
1 files changed, 12 insertions, 7 deletions
diff --git a/weed/util/http_util.go b/weed/util/http_util.go
index 08007a038..38202f4de 100644
--- a/weed/util/http_util.go
+++ b/weed/util/http_util.go
@@ -35,13 +35,13 @@ func PostBytes(url string, body []byte) ([]byte, error) {
return nil, fmt.Errorf("Post to %s: %v", url, err)
}
defer r.Body.Close()
- if r.StatusCode >= 400 {
- return nil, fmt.Errorf("%s: %s", url, r.Status)
- }
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("Read response body: %v", err)
}
+ if r.StatusCode >= 400 {
+ return nil, fmt.Errorf("%s: %s", url, r.Status)
+ }
return b, nil
}
@@ -88,7 +88,7 @@ func Head(url string) (http.Header, error) {
if err != nil {
return nil, err
}
- defer r.Body.Close()
+ defer closeResp(r)
if r.StatusCode >= 400 {
return nil, fmt.Errorf("%s: %s", url, r.Status)
}
@@ -130,7 +130,7 @@ func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachB
if err != nil {
return err
}
- defer r.Body.Close()
+ defer closeResp(r)
if r.StatusCode != 200 {
return fmt.Errorf("%s: %s", url, r.Status)
}
@@ -153,7 +153,7 @@ func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) e
if err != nil {
return err
}
- defer r.Body.Close()
+ defer closeResp(r)
if r.StatusCode != 200 {
return fmt.Errorf("%s: %s", url, r.Status)
}
@@ -262,7 +262,7 @@ func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte
if err != nil {
return 0, err
}
- defer r.Body.Close()
+ defer closeResp(r)
if r.StatusCode >= 400 {
return 0, fmt.Errorf("%s: %s", fileUrl, r.Status)
}
@@ -307,3 +307,8 @@ func ReadUrlAsReaderCloser(fileUrl string, rangeHeader string) (io.ReadCloser, e
return r.Body, nil
}
+
+func closeResp(resp *http.Response) {
+ io.Copy(ioutil.Discard, resp.Body)
+ resp.Body.Close()
+}