Merge pull request #6381 from gtirloni/issue-6380

HTTPDownloader - Fix invalid error handling
This commit is contained in:
M. Marsh 2018-06-15 12:26:39 -07:00 committed by GitHub
commit dcb870bcbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 12 deletions

View File

@ -278,20 +278,24 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
} }
resp, err := httpClient.Do(req) resp, err := httpClient.Do(req)
if err == nil && (resp.StatusCode >= 200 && resp.StatusCode < 300) { if err != nil {
// If the HEAD request succeeded, then attempt to set the range log.Printf("[DEBUG] (download) Error making HTTP HEAD request: %s", err.Error())
// query if we can. } else {
if resp.Header.Get("Accept-Ranges") == "bytes" { if resp.StatusCode >= 200 && resp.StatusCode < 300 {
if fi, err := dst.Stat(); err == nil { // If the HEAD request succeeded, then attempt to set the range
if _, err = dst.Seek(0, os.SEEK_END); err == nil { // query if we can.
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", fi.Size())) if resp.Header.Get("Accept-Ranges") == "bytes" {
if fi, err := dst.Stat(); err == nil {
if _, err = dst.Seek(0, os.SEEK_END); err == nil {
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", fi.Size()))
d.current = uint64(fi.Size()) d.current = uint64(fi.Size())
}
} }
} }
} else {
log.Printf("[DEBUG] (download) Unexpected HTTP response during HEAD request: %s", resp.Status)
} }
} else if err != nil || (resp.StatusCode >= 400 && resp.StatusCode < 600) {
return fmt.Errorf("%s", resp.Status)
} }
// Set the request to GET now, and redo the query to download // Set the request to GET now, and redo the query to download
@ -300,8 +304,10 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error {
resp, err = httpClient.Do(req) resp, err = httpClient.Do(req)
if err != nil { if err != nil {
return err return err
} else if err != nil || (resp.StatusCode >= 400 && resp.StatusCode < 600) { } else {
return fmt.Errorf("%s", resp.Status) if resp.StatusCode >= 400 && resp.StatusCode < 600 {
return fmt.Errorf("Error making HTTP GET request: %s", resp.Status)
}
} }
d.total = d.current + uint64(resp.ContentLength) d.total = d.current + uint64(resp.ContentLength)