Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

yandex: Fix socket leaks

Nick Craig-Wood9ade179407c1
diff --git a/yandex/api/client.go b/yandex/api/client.go
index a2d727b3b..5e3025748 100644
--- a/yandex/api/client.go
+++ b/yandex/api/client.go
@@ -107,7 +107,7 @@ func runRequest(client *Client, req *http.Request) ([]byte, error) {
return runRequestWithErrorHandler(client, req, defaultErrorHandler)
}
-func runRequestWithErrorHandler(client *Client, req *http.Request, errorHandler ErrorHandler) ([]byte, error) {
+func runRequestWithErrorHandler(client *Client, req *http.Request, errorHandler ErrorHandler) (out []byte, err error) {
resp, err := client.HTTPClient.Do(req)
if err != nil {
return nil, err
diff --git a/yandex/api/custom_property.go b/yandex/api/custom_property.go
index f9b28fee4..9bddf2015 100644
--- a/yandex/api/custom_property.go
+++ b/yandex/api/custom_property.go
@@ -28,7 +28,7 @@ func (c *Client) SetCustomProperty(remotePath string, property string, value str
}
//SetCustomPropertyRequest will make an CustomProperty request and return a URL to CustomProperty data to.
-func (c *Client) SetCustomPropertyRequest(remotePath string, body io.Reader) error {
+func (c *Client) SetCustomPropertyRequest(remotePath string, body io.Reader) (err error) {
values := url.Values{}
values.Add("path", remotePath)
req, err := c.scopedRequest("PATCH", "/v1/disk/resources?"+values.Encode(), body)
@@ -43,6 +43,7 @@ func (c *Client) SetCustomPropertyRequest(remotePath string, body io.Reader) err
if err := CheckAPIError(resp); err != nil {
return err
}
+ defer CheckClose(resp.Body, &err)
//If needed we can read response and check if custom_property is set.
diff --git a/yandex/api/download.go b/yandex/api/download.go
index 6eed0c15f..10c01d952 100644
--- a/yandex/api/download.go
+++ b/yandex/api/download.go
@@ -23,7 +23,7 @@ func (c *Client) Download(remotePath string) (io.ReadCloser, error) { //io.Write
}
// DownloadRequest will make an download request and return a URL to download data to.
-func (c *Client) DownloadRequest(remotePath string) (*DownloadResponse, error) {
+func (c *Client) DownloadRequest(remotePath string) (ur *DownloadResponse, err error) {
values := url.Values{}
values.Add("path", remotePath)
@@ -41,7 +41,7 @@ func (c *Client) DownloadRequest(remotePath string) (*DownloadResponse, error) {
}
defer CheckClose(resp.Body, &err)
- ur, err := ParseDownloadResponse(resp.Body)
+ ur, err = ParseDownloadResponse(resp.Body)
if err != nil {
return nil, err
}
diff --git a/yandex/api/error.go b/yandex/api/error.go
index d29137155..70e1475ab 100644
--- a/yandex/api/error.go
+++ b/yandex/api/error.go
@@ -41,20 +41,20 @@ func ProccessErrorResponse(data io.Reader) (*ErrorResponse, error) {
}
// CheckAPIError is a convenient function to turn erroneous
-// API response into go error.
-func CheckAPIError(resp *http.Response) error {
+// API response into go error. It closes the Body on error.
+func CheckAPIError(resp *http.Response) (err error) {
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return nil
}
+ defer CheckClose(resp.Body, &err)
+
errorResponse, err := ProccessErrorResponse(resp.Body)
if err != nil {
return err
}
errorResponse.StatusCode = resp.StatusCode
- defer CheckClose(resp.Body, &err)
-
return errorResponse
}
diff --git a/yandex/api/performdownload.go b/yandex/api/performdownload.go
index 590f7f042..5d4100c7f 100644
--- a/yandex/api/performdownload.go
+++ b/yandex/api/performdownload.go
@@ -8,7 +8,7 @@ import (
)
// PerformDownload does the actual download via unscoped PUT request.
-func (c *Client) PerformDownload(url string) (io.ReadCloser, error) {
+func (c *Client) PerformDownload(url string) (out io.ReadCloser, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
@@ -22,11 +22,11 @@ func (c *Client) PerformDownload(url string) (io.ReadCloser, error) {
}
if resp.StatusCode != 200 {
+ defer CheckClose(resp.Body, &err)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
- defer CheckClose(resp.Body, &err)
return nil, fmt.Errorf("download error [%d]: %s", resp.StatusCode, string(body[:]))
}
return resp.Body, err
diff --git a/yandex/api/performupload.go b/yandex/api/performupload.go
index d6bda4aed..2941e7a17 100644
--- a/yandex/api/performupload.go
+++ b/yandex/api/performupload.go
@@ -10,7 +10,7 @@ import (
)
// PerformUpload does the actual upload via unscoped PUT request.
-func (c *Client) PerformUpload(url string, data io.Reader) error {
+func (c *Client) PerformUpload(url string, data io.Reader) (err error) {
req, err := http.NewRequest("PUT", url, data)
if err != nil {
return err
diff --git a/yandex/api/upload.go b/yandex/api/upload.go
index a31ec3341..b2eff2f84 100644
--- a/yandex/api/upload.go
+++ b/yandex/api/upload.go
@@ -31,7 +31,7 @@ func (c *Client) Upload(data io.Reader, remotePath string, overwrite bool) error
}
// UploadRequest will make an upload request and return a URL to upload data to.
-func (c *Client) UploadRequest(remotePath string, overwrite bool) (*UploadResponse, error) {
+func (c *Client) UploadRequest(remotePath string, overwrite bool) (ur *UploadResponse, err error) {
values := url.Values{}
values.Add("path", remotePath)
values.Add("overwrite", strconv.FormatBool(overwrite))
@@ -50,7 +50,7 @@ func (c *Client) UploadRequest(remotePath string, overwrite bool) (*UploadRespon
}
defer CheckClose(resp.Body, &err)
- ur, err := ParseUploadResponse(resp.Body)
+ ur, err = ParseUploadResponse(resp.Body)
if err != nil {
return nil, err
}