Skip to content

Commit

Permalink
Merge pull request #273 from semmet95/fix/filename-remove-query-params
Browse files Browse the repository at this point in the history
adds logic to remove http query params from destination file name
  • Loading branch information
dmikusa authored Sep 15, 2023
2 parents 5ec54e0 + aaf573f commit fda98b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
12 changes: 10 additions & 2 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
actual BuildpackDependency
artifact string
file string
hasher = sha256.New()
uri = dependency.URI
uriSha string
)

for d, u := range d.Mappings {
Expand All @@ -177,12 +179,18 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
}
}

// downloaded artifact will have the uri sha as its filename
hasher.Write([]byte(uri))
uriSha = hex.EncodeToString(hasher.Sum(nil))

if dependency.SHA256 == "" {
d.Logger.Headerf("%s Dependency has no SHA256. Skipping cache.",
color.New(color.FgYellow, color.Bold).Sprint("Warning:"))

d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), uri)
artifact = filepath.Join(d.DownloadPath, filepath.Base(uri))

artifact = filepath.Join(d.DownloadPath, uriSha)

if err := d.download(uri, artifact, mods...); err != nil {
return nil, fmt.Errorf("unable to download %s\n%w", uri, err)
}
Expand Down Expand Up @@ -219,7 +227,7 @@ func (d *DependencyCache) Artifact(dependency BuildpackDependency, mods ...Reque
}

d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), uri)
artifact = filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(uri))
artifact = filepath.Join(d.DownloadPath, dependency.SHA256, uriSha)
if err := d.download(uri, artifact, mods...); err != nil {
return nil, fmt.Errorf("unable to download %s\n%w", uri, err)
}
Expand Down
32 changes: 32 additions & 0 deletions dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package libpak_test

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -155,6 +158,7 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
downloadPath string
dependency libpak.BuildpackDependency
dependencyCache libpak.DependencyCache
hasher hash.Hash
server *ghttp.Server
)

Expand All @@ -164,6 +168,8 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
cachePath = t.TempDir()
Expect(err).NotTo(HaveOccurred())

hasher = sha256.New()

downloadPath = t.TempDir()
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -317,6 +323,32 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
Expect(io.ReadAll(a)).To(Equal([]byte("alternate-fixture")))
})

it("sets downloaded file name to uri's sha256", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "test-fixture"))

hasher.Write([]byte(dependency.URI))

a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
Expect(filepath.Base(a.Name())).To(Equal(hex.EncodeToString(hasher.Sum(nil))))
})

it("sets downloaded file name to uri's sha256 with empty SHA256 and query parameters in the uri", func() {
dependency.SHA256 = ""
dependency.URI = fmt.Sprintf("%s/test-path?param1=value1&param2=value2", server.URL())
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "alternate-fixture"))

hasher.Write([]byte(dependency.URI))

a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(io.ReadAll(a)).To(Equal([]byte("alternate-fixture")))
Expect(filepath.Base(a.Name())).To(Equal(hex.EncodeToString(hasher.Sum(nil))))
})

it("sets User-Agent", func() {
server.AppendHandlers(ghttp.CombineHandlers(
ghttp.VerifyHeaderKV("User-Agent", "test-user-agent"),
Expand Down

0 comments on commit fda98b4

Please sign in to comment.