From b7ea0b44fc5ad453314c0a460640e0ca6eb31385 Mon Sep 17 00:00:00 2001 From: Giovanni Tirloni Date: Wed, 13 Jun 2018 17:42:27 -0300 Subject: [PATCH] HTTPDownloader - Fix invalid error handling --- common/download.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/common/download.go b/common/download.go index 74bef6313..9d2fa45f4 100644 --- a/common/download.go +++ b/common/download.go @@ -278,20 +278,27 @@ func (d *HTTPDownloader) Download(dst *os.File, src *url.URL) error { } resp, err := httpClient.Do(req) - if err == nil && (resp.StatusCode >= 200 && resp.StatusCode < 300) { - // If the HEAD request succeeded, then attempt to set the range - // query if we can. - 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()) + if err == nil { + + if resp.StatusCode >= 200 && resp.StatusCode < 300 { + // If the HEAD request succeeded, then attempt to set the range + // query if we can. + 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()) + } } } } - } else if err != nil || (resp.StatusCode >= 400 && resp.StatusCode < 600) { - return fmt.Errorf("%s", resp.Status) + + if resp.StatusCode >= 400 && resp.StatusCode < 600 { + return fmt.Errorf("Received HTTP error: %s", resp.Status) + } + } else { + return err } // Set the request to GET now, and redo the query to download