Skip to content

Commit

Permalink
Fixed an issue causing Jar decompression to fail when downloaded usin…
Browse files Browse the repository at this point in the history
…g chunked encoding (#124)
  • Loading branch information
djcass44 committed Nov 8, 2023
1 parent 3320fcf commit 0ab98b3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion remote_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ func closeBody(resp *http.Response) func() {
}

func decompressResponse(bodyBytes []byte, contentLength int64, cacheLocator CacheLocator, downloadURL string) error {
zipReader, err := zip.NewReader(bytes.NewReader(bodyBytes), contentLength)
size := contentLength
// if the content length is not set (i.e. chunked encoding),
// we need to use the length of the bodyBytes otherwise
// the unzip operation will fail
if contentLength < 0 {
size = int64(len(bodyBytes))
}
zipReader, err := zip.NewReader(bytes.NewReader(bodyBytes), size)
if err != nil {
return errorFetchingPostgres(err)
}
Expand Down
47 changes: 47 additions & 0 deletions remote_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"archive/zip"
"crypto/sha256"
"encoding/hex"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -370,3 +372,48 @@ func Test_defaultRemoteFetchStrategyWithExistingDownload(t *testing.T) {
out2, err := os.ReadFile(cacheLocation)
assert.Equal(t, out1, out2)
}

func Test_defaultRemoteFetchStrategy_whenContentLengthNotSet(t *testing.T) {
jarFile, cleanUp := createTempZipArchive()
defer cleanUp()

cacheLocation := filepath.Join(filepath.Dir(jarFile), "extract_location", "cache.jar")

bytes, err := os.ReadFile(jarFile)
if err != nil {
require.NoError(t, err)
}
contentHash := sha256.Sum256(bytes)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.RequestURI, ".sha256") {
w.WriteHeader(200)
if _, err := w.Write([]byte(hex.EncodeToString(contentHash[:]))); err != nil {
panic(err)
}

return
}

f, err := os.Open(jarFile)
if err != nil {
panic(err)
}

// stream the file back so that Go uses
// chunked encoding and never sets Content-Length
_, _ = io.Copy(w, f)
}))
defer server.Close()

remoteFetchStrategy := defaultRemoteFetchStrategy(server.URL+"/maven2",
testVersionStrategy(),
func() (s string, b bool) {
return cacheLocation, false
})

err = remoteFetchStrategy()

assert.NoError(t, err)
assert.FileExists(t, cacheLocation)
}

0 comments on commit 0ab98b3

Please sign in to comment.