properly close http response

This commit is contained in:
Chris Lu 2020-02-14 09:09:15 -08:00
parent 9b6296e77a
commit cf5064d702
5 changed files with 18 additions and 10 deletions

View file

@ -71,6 +71,7 @@ func downloadToFile(server, fileId, saveDir string) error {
} }
f, err := os.OpenFile(path.Join(saveDir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) f, err := os.OpenFile(path.Join(saveDir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil { if err != nil {
io.Copy(ioutil.Discard, rc)
return err return err
} }
defer f.Close() defer f.Close()

View file

@ -48,6 +48,7 @@ func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request
writeErrorResponse(w, ErrInvalidCopySource, r.URL) writeErrorResponse(w, ErrInvalidCopySource, r.URL)
return return
} }
defer dataReader.Close()
etag, errCode := s3a.putToFiler(r, dstUrl, dataReader) etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
@ -129,6 +130,7 @@ func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Req
writeErrorResponse(w, ErrInvalidCopySource, r.URL) writeErrorResponse(w, ErrInvalidCopySource, r.URL)
return return
} }
defer dataReader.Close()
etag, errCode := s3a.putToFiler(r, dstUrl, dataReader) etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)

View file

@ -44,6 +44,7 @@ func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request)
if rAuthType == authTypeStreamingSigned { if rAuthType == authTypeStreamingSigned {
dataReader = newSignV4ChunkedReader(r) dataReader = newSignV4ChunkedReader(r)
} }
defer dataReader.Close()
uploadUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s", uploadUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
s3a.option.Filer, s3a.option.BucketsPath, bucket, object, bucket) s3a.option.Filer, s3a.option.BucketsPath, bucket, object, bucket)
@ -156,7 +157,7 @@ func passThroughResponse(proxyResonse *http.Response, w http.ResponseWriter) {
io.Copy(w, proxyResonse.Body) io.Copy(w, proxyResonse.Body)
} }
func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.ReadCloser) (etag string, code ErrorCode) { func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader io.Reader) (etag string, code ErrorCode) {
hash := md5.New() hash := md5.New()
var body io.Reader = io.TeeReader(dataReader, hash) var body io.Reader = io.TeeReader(dataReader, hash)
@ -179,8 +180,6 @@ func (s3a *S3ApiServer) putToFiler(r *http.Request, uploadUrl string, dataReader
resp, postErr := client.Do(proxyReq) resp, postErr := client.Do(proxyReq)
dataReader.Close()
if postErr != nil { if postErr != nil {
glog.Errorf("post to filer: %v", postErr) glog.Errorf("post to filer: %v", postErr)
return "", ErrInternalError return "", ErrInternalError

View file

@ -199,6 +199,7 @@ func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Requ
if rAuthType == authTypeStreamingSigned { if rAuthType == authTypeStreamingSigned {
dataReader = newSignV4ChunkedReader(r) dataReader = newSignV4ChunkedReader(r)
} }
defer dataReader.Close()
uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s", uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
s3a.option.Filer, s3a.genUploadsFolder(bucket), uploadID, partID-1, bucket) s3a.option.Filer, s3a.genUploadsFolder(bucket), uploadID, partID-1, bucket)

View file

@ -35,13 +35,13 @@ func PostBytes(url string, body []byte) ([]byte, error) {
return nil, fmt.Errorf("Post to %s: %v", url, err) return nil, fmt.Errorf("Post to %s: %v", url, err)
} }
defer r.Body.Close() defer r.Body.Close()
if r.StatusCode >= 400 {
return nil, fmt.Errorf("%s: %s", url, r.Status)
}
b, err := ioutil.ReadAll(r.Body) b, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("Read response body: %v", err) 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 return b, nil
} }
@ -88,7 +88,7 @@ func Head(url string) (http.Header, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer r.Body.Close() defer closeResp(r)
if r.StatusCode >= 400 { if r.StatusCode >= 400 {
return nil, fmt.Errorf("%s: %s", url, r.Status) 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 { if err != nil {
return err return err
} }
defer r.Body.Close() defer closeResp(r)
if r.StatusCode != 200 { if r.StatusCode != 200 {
return fmt.Errorf("%s: %s", url, r.Status) 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 { if err != nil {
return err return err
} }
defer r.Body.Close() defer closeResp(r)
if r.StatusCode != 200 { if r.StatusCode != 200 {
return fmt.Errorf("%s: %s", url, r.Status) 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 { if err != nil {
return 0, err return 0, err
} }
defer r.Body.Close() defer closeResp(r)
if r.StatusCode >= 400 { if r.StatusCode >= 400 {
return 0, fmt.Errorf("%s: %s", fileUrl, r.Status) 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 return r.Body, nil
} }
func closeResp(resp *http.Response) {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}