diff --git a/checksum.go b/checksum.go index 76fa43087..62a910f71 100644 --- a/checksum.go +++ b/checksum.go @@ -49,8 +49,9 @@ func checksumHashAndValue(u *url.URL) (checksumHash hash.Hash, checksumValue []b return nil, nil, fmt.Errorf( "unsupported checksum type: %s", checksumType) } + file := v[idx+1:] - checkSums, err := checksumsFromFile(v[idx+1:], u) + checkSums, err := checksumsFromFile(file, u) if err != nil { return nil, nil, err } @@ -60,11 +61,12 @@ func checksumHashAndValue(u *url.URL) (checksumHash hash.Hash, checksumValue []b if fn, found := checksummers[checksumType]; found { checksumHash = fn() checksumValue, err = hex.DecodeString(checksumValueString) - return + return checksumHash, checksumValue, err } } - return + return nil, nil, fmt.Errorf( + "Could not find a supported checksum in %s: %s", file, checksumType) } func checksumsFromFile(checksumFile string, src *url.URL) (checkSums map[string]string, err error) { @@ -114,11 +116,15 @@ func checksumsFromFile(checksumFile string, src *url.URL) (checkSums map[string] // filename matches src ? if filename == option { res[checksumType] = checksumValue + break } } } - return + if len(res) == 0 { + err = fmt.Errorf("Could not find a checksum for %s in %s", filename, checksumFile) + } + return res, err } // parseChecksumLine takes a line from a checksum file and returns @@ -150,7 +156,7 @@ func parseChecksumLine(checksumFilename, line string) (checksumType, checksumVal for ctype := range checksummers { // guess checksum type from filename - if strings.ContainsAny(checksumFilename, ctype) { + if strings.Contains(strings.ToLower(checksumFilename), ctype) { checksumType = ctype } } diff --git a/checksum_test.go b/checksum_test.go index 67db982f8..ca42d0d5e 100644 --- a/checksum_test.go +++ b/checksum_test.go @@ -17,7 +17,7 @@ func Test_parseChecksumLine(t *testing.T) { }{ {"gnu SHA256SUMS", args{ - "http://old-releases.ubuntu.com/releases/14.04.1/SHA256SUMS", + "http://old-releases.ubuntu.com/releases/14.04.1/SHA512SUMS", "d9a217e80fb6dc2576d5ccca4c44376c25e6016de47a48e07345678d660fac51 *ubuntu-14.04-desktop-amd64+mac.iso", }, "sha512", diff --git a/source.go b/source.go index c63f2bbaf..2c722427d 100644 --- a/source.go +++ b/source.go @@ -6,9 +6,9 @@ import ( "strings" ) -// SourceDirSubdir takes a source and returns a tuple of the URL without -// the subdir and the URL with the subdir. -func SourceDirSubdir(src string) (string, string) { +// SourceDirSubdir takes a source and returns the URL without +// the subdir and the subdir. +func SourceDirSubdir(src string) (url string, subdir string) { // Calcaulate an offset to avoid accidentally marking the scheme // as the dir. var offset int @@ -16,14 +16,20 @@ func SourceDirSubdir(src string) (string, string) { offset = idx + 3 } + // URL might contains another url in query parameters + stop := len(src) + if idx := strings.Index(src, "?"); idx > -1 { + stop = idx + } + // First see if we even have an explicit subdir - idx := strings.Index(src[offset:], "//") + idx := strings.Index(src[offset:stop], "//") if idx == -1 { return src, "" } idx += offset - subdir := src[idx+2:] + subdir = src[idx+2:] src = src[:idx] // Next, check if we have query parameters and push them onto the diff --git a/source_test.go b/source_test.go index 213324a98..c65f50f1b 100644 --- a/source_test.go +++ b/source_test.go @@ -28,6 +28,14 @@ func TestSourceDirSubdir(t *testing.T) { "https://hashicorp.com/path//*?archive=foo", "https://hashicorp.com/path?archive=foo", "*", }, + { + "https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", + "https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", "", + }, + { + "https://hashicorp.com/path//*?checksum=file:http://url.com/....iso.sha256", + "https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", "*", + }, { "file://foo//bar", "file://foo", "bar",