2015-12-07 05:01:03 +01:00
|
|
|
package src
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-06-12 16:06:02 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-12-07 05:01:03 +01:00
|
|
|
)
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// PerformDownload does the actual download via unscoped GET request.
|
|
|
|
func (c *Client) PerformDownload(url string, headers map[string]string) (out io.ReadCloser, err error) {
|
2015-12-07 05:01:03 +01:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
// Set any extra headers
|
|
|
|
for k, v := range headers {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2015-12-07 05:01:03 +01:00
|
|
|
//c.setRequestScope(req)
|
|
|
|
|
2016-02-15 17:43:18 +01:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2015-12-07 05:01:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-10 12:29:57 +02:00
|
|
|
_, isRanging := req.Header["Range"]
|
|
|
|
if !(resp.StatusCode == http.StatusOK || (isRanging && resp.StatusCode == http.StatusPartialContent)) {
|
2015-12-30 14:30:57 +01:00
|
|
|
defer CheckClose(resp.Body, &err)
|
2015-12-07 05:01:03 +01:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("download error [%d]: %s", resp.StatusCode, string(body[:]))
|
2015-12-07 05:01:03 +01:00
|
|
|
}
|
|
|
|
return resp.Body, err
|
|
|
|
}
|