diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..57b3ae6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +testdata/* text eol=lf diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index 32cdb6c..13b6754 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -28,4 +28,5 @@ jobs: go-version-file: go.mod - name: Run tests + shell: bash run: go test -race -coverprofile=coverage.txt -covermode=atomic diff --git a/downloader.go b/downloader.go index 5b689ee..fef8bf3 100644 --- a/downloader.go +++ b/downloader.go @@ -12,6 +12,7 @@ import ( "io" "net/http" "os" + "slices" "sync" "time" ) @@ -137,20 +138,29 @@ func DownloadWithConfig(file string, reqURL string, config Config, options ...Do // in the specified file. A download resume is tried if a file shorter than the requested // url is already present. The download can be cancelled using the provided context. func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL string, config Config, options ...DownloadOptions) (*Downloader, error) { - noResume := false - for _, opt := range options { - if opt == NoResume { - noResume = true - } + noResume := slices.Contains(options, NoResume) + + // Perform a HEAD call to gather information about the server capabilities + serverAcceptsRanges := false + var size int64 + if headReq, err := http.NewRequestWithContext(ctx, "HEAD", reqURL, nil); err != nil { + return nil, fmt.Errorf("setting up HEAD request: %s", err) + } else if headResp, err := config.HttpClient.Do(headReq); err != nil { + return nil, fmt.Errorf("performing HEAD request: %s", err) + } else { + serverAcceptsRanges = (headResp.Header.Get("Accept-Ranges") == "bytes") + size = headResp.ContentLength + _ = headResp.Body.Close() } - req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) + // Setup the actual GET request + req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) if err != nil { return nil, fmt.Errorf("setting up HTTP request: %s", err) } var completed int64 - if !noResume { + if !noResume && serverAcceptsRanges { if info, err := os.Stat(file); err == nil { completed = info.Size() req.Header.Set("Range", fmt.Sprintf("bytes=%d-", completed)) @@ -182,7 +192,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin Resp: resp, out: f, completed: completed, - size: resp.ContentLength + completed, + size: size, } return d, nil } diff --git a/downloader_test.go b/downloader_test.go index ee0f703..29fcb9f 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -13,6 +13,7 @@ import ( "io" "net/http" "os" + "path/filepath" "testing" "time" @@ -25,12 +26,14 @@ func makeTmpFile(t *testing.T) string { require.NoError(t, tmp.Close()) tmpFile := tmp.Name() require.NoError(t, os.Remove(tmpFile)) + t.Cleanup(func() { + os.Remove(tmpFile) + }) return tmpFile } func TestDownload(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) d, err := Download(tmpFile, "https://go.bug.st/test.txt") require.NoError(t, err) @@ -49,7 +52,6 @@ func TestDownload(t *testing.T) { func TestResume(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) part, err := os.ReadFile("testdata/test.txt.part") require.NoError(t, err) @@ -71,9 +73,28 @@ func TestResume(t *testing.T) { require.Equal(t, file1, file2) } +func TestResumeOnAlreadyCompletedFile(t *testing.T) { + tmpFile := makeTmpFile(t) + + full, err := os.ReadFile("testdata/test.txt") + require.NoError(t, err) + require.NoError(t, os.WriteFile(tmpFile, full, 0644)) + + d, err := Download(tmpFile, "https://go.bug.st/test.txt") + require.NoError(t, err) + require.Equal(t, int64(8052), d.Completed()) + require.Equal(t, int64(8052), d.Size()) + require.NoError(t, d.Run()) + require.Equal(t, int64(8052), d.Completed()) + + // Check file content is unchanged + file2, err := os.ReadFile(tmpFile) + require.NoError(t, err) + require.Equal(t, full, file2) +} + func TestNoResume(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) part, err := os.ReadFile("testdata/test.txt.part") require.NoError(t, err) @@ -97,7 +118,6 @@ func TestNoResume(t *testing.T) { func TestInvalidRequest(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) d, err := Download(tmpFile, "asd://go.bug.st/test.txt") require.Error(t, err) @@ -112,7 +132,6 @@ func TestInvalidRequest(t *testing.T) { func TestRunAndPool(t *testing.T) { tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) d, err := Download(tmpFile, "https://downloads.arduino.cc/cores/avr-1.6.20.tar.bz2") require.NoError(t, err) @@ -130,11 +149,8 @@ func TestRunAndPool(t *testing.T) { } func TestErrorOnFileOpening(t *testing.T) { - tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) - - require.NoError(t, os.WriteFile(tmpFile, []byte{}, 0000)) - d, err := Download(tmpFile, "http://go.bug.st/test.txt") + unaccessibleFile := filepath.Join(os.TempDir(), "nonexistentdir", "test.txt") + d, err := Download(unaccessibleFile, "http://go.bug.st/test.txt") require.Error(t, err) require.Nil(t, d) } @@ -178,7 +194,7 @@ func TestApplyUserAgentHeaderUsingConfig(t *testing.T) { func TestContextCancelation(t *testing.T) { slowHandler := func(w http.ResponseWriter, r *http.Request) { - for i := 0; i < 50; i++ { + for i := range 50 { fmt.Fprintf(w, "Hello %d\n", i) w.(http.Flusher).Flush() time.Sleep(100 * time.Millisecond) @@ -189,20 +205,20 @@ func TestContextCancelation(t *testing.T) { mux.HandleFunc("/slow", slowHandler) server := &http.Server{Addr: ":8080", Handler: mux} go func() { - server.ListenAndServe() - fmt.Println("Server stopped") + err := server.ListenAndServe() + fmt.Println("Server stopped with err:", err) }() // Wait for server start time.Sleep(time.Second) tmpFile := makeTmpFile(t) - defer os.Remove(tmpFile) ctx, cancel := context.WithCancel(context.Background()) d, err := DownloadWithConfigAndContext(ctx, tmpFile, "http://127.0.0.1:8080/slow", Config{}) require.NoError(t, err) // Cancel in two seconds + startTime := time.Now() go func() { time.Sleep(2 * time.Second) cancel() @@ -211,11 +227,11 @@ func TestContextCancelation(t *testing.T) { // Run slow download max := int64(0) err = d.RunAndPoll(func(curr int64) { - fmt.Println(curr) + fmt.Println("Downloaded", curr, "bytes. Elapsed:", time.Since(startTime)) max = curr }, 100*time.Millisecond) require.EqualError(t, err, "context canceled") require.True(t, max < 400) - require.NoError(t, server.Shutdown(ctx)) + require.NoError(t, server.Shutdown(context.Background())) }