Don't fallback between compression methods available unless we get strictly HTTP 404. #129 #125

Prior to that, some real errors could have been masked away by that fallback.
This commit is contained in:
Andrey Smirnov
2014-10-24 08:51:04 +04:00
parent d2d21c3df7
commit 7be2ef8b85
3 changed files with 39 additions and 29 deletions
+17 -3
View File
@@ -16,6 +16,17 @@ import (
"strings"
)
// HTTPError is download error connected to HTTP code
type HTTPError struct {
Code int
URL string
}
// Error
func (e *HTTPError) Error() string {
return fmt.Sprintf("HTTP code %d while fetching %s", e.Code, e.URL)
}
// Check interface
var (
_ aptly.Downloader = (*downloaderImpl)(nil)
@@ -139,7 +150,7 @@ func (downloader *downloaderImpl) handleTask(task *downloadTask) {
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
task.result <- fmt.Errorf("HTTP code %d while fetching %s", resp.StatusCode, task.url)
task.result <- &HTTPError{Code: resp.StatusCode, URL: task.url}
return
}
@@ -307,13 +318,16 @@ func DownloadTryCompression(downloader aptly.Downloader, url string, expectedChe
}
if err != nil {
continue
if err1, ok := err.(*HTTPError); ok && err1.Code == 404 {
continue
}
return nil, nil, err
}
var uncompressed io.Reader
uncompressed, err = method.transformation(file)
if err != nil {
continue
return nil, nil, err
}
return uncompressed, file, err
+9 -13
View File
@@ -229,7 +229,7 @@ func (s *DownloaderSuite) TestDownloadTryCompression(c *C) {
// bzip2 not available, but gz is
buf = make([]byte, 4)
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.bz2", &HTTPError{Code: 404})
d.ExpectResponse("http://example.com/file.gz", gzipData)
r, file, err = DownloadTryCompression(d, "http://example.com/file", expectedChecksums, false)
c.Assert(err, IsNil)
@@ -241,8 +241,8 @@ func (s *DownloaderSuite) TestDownloadTryCompression(c *C) {
// bzip2 & gzip not available, but raw is
buf = make([]byte, 4)
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.gz", errors.New("404"))
d.ExpectError("http://example.com/file.bz2", &HTTPError{Code: 404})
d.ExpectError("http://example.com/file.gz", &HTTPError{Code: 404})
d.ExpectResponse("http://example.com/file", rawData)
r, file, err = DownloadTryCompression(d, "http://example.com/file", expectedChecksums, false)
c.Assert(err, IsNil)
@@ -254,14 +254,10 @@ func (s *DownloaderSuite) TestDownloadTryCompression(c *C) {
// gzip available, but broken
buf = make([]byte, 4)
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.bz2", &HTTPError{Code: 404})
d.ExpectResponse("http://example.com/file.gz", "x")
d.ExpectResponse("http://example.com/file", "recovered")
r, file, err = DownloadTryCompression(d, "http://example.com/file", nil, false)
c.Assert(err, IsNil)
defer file.Close()
io.ReadFull(r, buf)
c.Assert(string(buf), Equals, "reco")
c.Assert(err, ErrorMatches, "unexpected EOF")
c.Assert(d.Empty(), Equals, true)
}
@@ -271,15 +267,15 @@ func (s *DownloaderSuite) TestDownloadTryCompressionErrors(c *C) {
c.Assert(err, ErrorMatches, "unexpected request.*")
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.gz", errors.New("404"))
d.ExpectError("http://example.com/file.bz2", &HTTPError{Code: 404})
d.ExpectError("http://example.com/file.gz", &HTTPError{Code: 404})
d.ExpectError("http://example.com/file", errors.New("403"))
_, _, err = DownloadTryCompression(d, "http://example.com/file", nil, false)
c.Assert(err, ErrorMatches, "403")
d = NewFakeDownloader()
d.ExpectError("http://example.com/file.bz2", errors.New("404"))
d.ExpectError("http://example.com/file.gz", errors.New("404"))
d.ExpectError("http://example.com/file.bz2", &HTTPError{Code: 404})
d.ExpectError("http://example.com/file.gz", &HTTPError{Code: 404})
d.ExpectResponse("http://example.com/file", rawData)
_, _, err = DownloadTryCompression(d, "http://example.com/file", map[string]utils.ChecksumInfo{"file": utils.ChecksumInfo{Size: 7}}, false)
c.Assert(err, ErrorMatches, "checksums don't match.*")