From a54f30b6c615570fe95323990f9dedca84a29c57 Mon Sep 17 00:00:00 2001 From: chrislu Date: Wed, 31 Aug 2022 00:09:23 -0700 Subject: [PATCH] s3: close response body with copying object --- weed/s3api/s3api_object_copy_handlers.go | 3 ++- weed/util/http_util.go | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/weed/s3api/s3api_object_copy_handlers.go b/weed/s3api/s3api_object_copy_handlers.go index a5e8f128c..0e6c5ab89 100644 --- a/weed/s3api/s3api_object_copy_handlers.go +++ b/weed/s3api/s3api_object_copy_handlers.go @@ -174,11 +174,12 @@ func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Req srcUrl := fmt.Sprintf("http://%s%s/%s%s", s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject)) - dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false), rangeHeader) + resp, dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false), rangeHeader) if err != nil { s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource) return } + defer util.CloseResponse(resp) defer dataReader.Close() glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl) diff --git a/weed/util/http_util.go b/weed/util/http_util.go index 783ff2379..883dffe60 100644 --- a/weed/util/http_util.go +++ b/weed/util/http_util.go @@ -370,11 +370,11 @@ func readEncryptedUrl(fileUrl string, cipherKey []byte, isContentCompressed bool return false, nil } -func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (io.ReadCloser, error) { +func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (*http.Response, io.ReadCloser, error) { req, err := http.NewRequest("GET", fileUrl, nil) if err != nil { - return nil, err + return nil, nil, err } if rangeHeader != "" { req.Header.Add("Range", rangeHeader) @@ -388,10 +388,11 @@ func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (io.R r, err := client.Do(req) if err != nil { - return nil, err + return nil, nil, err } if r.StatusCode >= 400 { - return nil, fmt.Errorf("%s: %s", fileUrl, r.Status) + CloseResponse(r) + return nil, nil, fmt.Errorf("%s: %s", fileUrl, r.Status) } var reader io.ReadCloser @@ -399,12 +400,11 @@ func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (io.R switch contentEncoding { case "gzip": reader, err = gzip.NewReader(r.Body) - defer reader.Close() default: reader = r.Body } - return reader, nil + return r, reader, nil } func CloseResponse(resp *http.Response) {