From f9b41e2c28d629d443ba4929876a2e3f32b6a298 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 8 Jan 2026 11:42:30 +0100 Subject: [PATCH 1/8] Remove useless Done channel --- downloader.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/downloader.go b/downloader.go index 503fa71..400dbc9 100644 --- a/downloader.go +++ b/downloader.go @@ -19,7 +19,6 @@ import ( // Downloader is an asynchronous downloader type Downloader struct { URL string - Done chan struct{} Resp *http.Response out *os.File completed int64 @@ -36,7 +35,6 @@ func (d *Downloader) close() { if d.Resp != nil { d.Resp.Body.Close() } - close(d.Done) } // Size return the size of the download (or -1 if the server doesn't provide it) @@ -50,14 +48,17 @@ func (d *Downloader) RunAndPoll(poll func(current int64), interval time.Duration t := time.NewTicker(interval) defer t.Stop() - go d.Run() + res := make(chan error, 1) + go func() { + res <- d.Run() + }() for { select { case <-t.C: poll(d.Completed()) - case <-d.Done: + case err := <-res: poll(d.Completed()) - return d.Error() + return err } } } @@ -172,7 +173,6 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin // Size matches: assume the file is already downloaded return &Downloader{ URL: reqURL, - Done: make(chan struct{}), Resp: headResp, completed: remoteSize, size: remoteSize, @@ -230,7 +230,6 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin return &Downloader{ URL: reqURL, - Done: make(chan struct{}), Resp: resp, out: f, completed: completed, From efd1ae355fb826dd579e0d2eb7d10bfe3546bae1 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 7 Jan 2026 16:45:47 +0100 Subject: [PATCH 2/8] Do error checks on HEAD request output consume --- downloader.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/downloader.go b/downloader.go index 400dbc9..dfbc68a 100644 --- a/downloader.go +++ b/downloader.go @@ -156,12 +156,18 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin } remoteSize := headResp.ContentLength // -1 if server doesn't send Content-Length serverCanResume := (headResp.Header.Get("Accept-Ranges") == "bytes") && (remoteSize != -1) + if _, err := io.Copy(io.Discard, headResp.Body); err != nil { + return nil, err + } + if err := headResp.Body.Close(); err != nil { + return nil, err + } + + // Perform acceptance checks var acceptError error if config.AcceptFunc != nil { acceptError = config.AcceptFunc(headResp) } - _, _ = io.Copy(io.Discard, headResp.Body) - _ = headResp.Body.Close() if acceptError != nil { return nil, acceptError } From 6e4e981ae736a900edc958cebfd5036c9773bdf7 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 7 Jan 2026 16:52:12 +0100 Subject: [PATCH 3/8] Moved HEAD request in his own function This will help next commits, since we can use the internal HttpClient.Timeout field to perform the global timeout. --- downloader.go | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/downloader.go b/downloader.go index dfbc68a..4272647 100644 --- a/downloader.go +++ b/downloader.go @@ -125,6 +125,29 @@ func DownloadWithConfig(file string, reqURL string, config Config) (*Downloader, return DownloadWithConfigAndContext(context.Background(), file, reqURL, config) } +func doHeadRequest(ctx context.Context, reqURL string, config Config) (*http.Response, error) { + headReq, err := http.NewRequestWithContext(ctx, "HEAD", reqURL, nil) + if err != nil { + return nil, fmt.Errorf("setting up HEAD request: %s", err) + } + if config.ExtraHeaders != nil { + for k, v := range config.ExtraHeaders { + headReq.Header.Set(k, v) + } + } + headResp, err := config.HttpClient.Do(headReq) + if err != nil { + return nil, fmt.Errorf("performing HEAD request: %s", err) + } + if _, err := io.Copy(io.Discard, headResp.Body); err != nil { + return nil, err + } + if err := headResp.Body.Close(); err != nil { + return nil, err + } + return headResp, nil +} + // DownloadWithConfigAndContext applies an additional configuration to the http client and // returns an asynchronous downloader that will download the specified url // in the specified file. @@ -141,27 +164,12 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin } // Perform a HEAD call to gather information about the server capabilities and remote file - headReq, err := http.NewRequestWithContext(ctx, "HEAD", reqURL, nil) + headResp, err := doHeadRequest(ctx, reqURL, config) if err != nil { - return nil, fmt.Errorf("setting up HEAD request: %s", err) - } - if config.ExtraHeaders != nil { - for k, v := range config.ExtraHeaders { - headReq.Header.Set(k, v) - } - } - headResp, err := config.HttpClient.Do(headReq) - if err != nil { - return nil, fmt.Errorf("performing HEAD request: %s", err) + return nil, err } remoteSize := headResp.ContentLength // -1 if server doesn't send Content-Length serverCanResume := (headResp.Header.Get("Accept-Ranges") == "bytes") && (remoteSize != -1) - if _, err := io.Copy(io.Discard, headResp.Body); err != nil { - return nil, err - } - if err := headResp.Body.Close(); err != nil { - return nil, err - } // Perform acceptance checks var acceptError error From 7e64c23917bcaf4a815dd2235165c3b87cde18f0 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 8 Jan 2026 12:11:00 +0100 Subject: [PATCH 4/8] Small unit-test adjustments --- downloader_test.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/downloader_test.go b/downloader_test.go index dc690b6..a81e134 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -12,6 +12,7 @@ import ( "fmt" "io" "net/http" + "net/http/httptest" "os" "path/filepath" "testing" @@ -145,9 +146,9 @@ func TestRunAndPool(t *testing.T) { prevCurr = curr callCount++ } - _ = d.RunAndPoll(callback, time.Millisecond) + require.NoError(t, d.RunAndPoll(callback, time.Millisecond)) fmt.Printf("callback called %d times\n", callCount) - require.True(t, callCount > 10) + require.Greater(t, callCount, 10) require.Equal(t, int64(4897949), d.Completed()) } @@ -197,8 +198,12 @@ func TestApplyUserAgentHeaderUsingConfig(t *testing.T) { func TestContextCancelation(t *testing.T) { slowHandler := func(w http.ResponseWriter, r *http.Request) { + if r.Method == "HEAD" { + w.Header().Set("Content-Length", "500") + return + } for i := range 50 { - fmt.Fprintf(w, "Hello %d\n", i) + fmt.Fprintf(w, "Hello %02d.\n", i) w.(http.Flusher).Flush() time.Sleep(100 * time.Millisecond) } @@ -206,18 +211,13 @@ func TestContextCancelation(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/slow", slowHandler) - server := &http.Server{Addr: ":8080", Handler: mux} - go func() { - err := server.ListenAndServe() - fmt.Println("Server stopped with err:", err) - }() - // Wait for server start - time.Sleep(time.Second) + server := httptest.NewServer(mux) + t.Cleanup(server.Close) tmpFile := makeTmpFile(t) ctx, cancel := context.WithCancel(context.Background()) - d, err := downloader.DownloadWithConfigAndContext(ctx, tmpFile, "http://127.0.0.1:8080/slow", downloader.Config{}) + d, err := downloader.DownloadWithConfigAndContext(ctx, tmpFile, server.URL+"/slow", downloader.Config{}) require.NoError(t, err) // Cancel in two seconds @@ -234,7 +234,6 @@ func TestContextCancelation(t *testing.T) { max = curr }, 100*time.Millisecond) require.EqualError(t, err, "context canceled") - require.True(t, max < 400) - - require.NoError(t, server.Shutdown(context.Background())) + require.True(t, max < 210) + fmt.Println("Context canceled successfully") } From 524843ceebd61a2197deaa8f3dd5b2fbef96c261 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 8 Jan 2026 12:44:46 +0100 Subject: [PATCH 5/8] Added timeout on HEAD call --- downloader.go | 4 ++++ downloader_config.go | 4 ++++ downloader_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/downloader.go b/downloader.go index 4272647..fd2f1a5 100644 --- a/downloader.go +++ b/downloader.go @@ -25,6 +25,7 @@ type Downloader struct { completedLock sync.Mutex size int64 err error + timeout time.Duration } // Close the download @@ -135,6 +136,8 @@ func doHeadRequest(ctx context.Context, reqURL string, config Config) (*http.Res headReq.Header.Set(k, v) } } + // Should complete the HEAD call within the inactivity timeout + config.HttpClient.Timeout = config.InactivityTimeout headResp, err := config.HttpClient.Do(headReq) if err != nil { return nil, fmt.Errorf("performing HEAD request: %s", err) @@ -248,5 +251,6 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin out: f, completed: completed, size: remoteSize, + timeout: config.InactivityTimeout, }, nil } diff --git a/downloader_config.go b/downloader_config.go index f954af7..b41c396 100644 --- a/downloader_config.go +++ b/downloader_config.go @@ -9,6 +9,7 @@ package downloader import ( "net/http" "sync" + "time" ) // Config contains the configuration for the downloader @@ -26,6 +27,9 @@ type Config struct { // DoNotErrorOnNon2xxStatusCode set to true to not return an error // if the server returns a non-2xx status code. DoNotErrorOnNon2xxStatusCode bool + // InactivityTimeout is the duration after which, if no data is received, + // the download is aborted. If set to 0, no timeout is applied. + InactivityTimeout time.Duration } var defaultConfig Config = Config{} diff --git a/downloader_test.go b/downloader_test.go index a81e134..b0c5602 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -237,3 +237,29 @@ func TestContextCancelation(t *testing.T) { require.True(t, max < 210) fmt.Println("Context canceled successfully") } + +func TestTimeoutOnHEADCall(t *testing.T) { + timeoutHandler := func(w http.ResponseWriter, r *http.Request) { + time.Sleep(3 * time.Second) + fmt.Fprintln(w, "This is a slow response") + } + mux := http.NewServeMux() + mux.HandleFunc("/timeout", timeoutHandler) + server := httptest.NewServer(mux) + t.Cleanup(server.Close) + + tmpFile := makeTmpFile(t) + + config := downloader.Config{ + InactivityTimeout: 1 * time.Second, + } + + startTime := time.Now() + d, err := downloader.DownloadWithConfig(tmpFile, server.URL+"/timeout", config) + require.Error(t, err) + require.Nil(t, d) + require.Contains(t, err.Error(), "context deadline exceeded") + elapsed := time.Since(startTime) + require.True(t, elapsed < 5*time.Second) + fmt.Println("Download aborted due to timeout as expected after", elapsed) +} From 43f954466c7cea890e7002745c66099c3beae416 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 8 Jan 2026 13:09:29 +0100 Subject: [PATCH 6/8] Implemented inactivity timeout on GET --- downloader.go | 29 ++++++++++++++++ downloader_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/downloader.go b/downloader.go index fd2f1a5..87873fb 100644 --- a/downloader.go +++ b/downloader.go @@ -26,6 +26,7 @@ type Downloader struct { size int64 err error timeout time.Duration + cancel context.CancelCauseFunc } // Close the download @@ -36,6 +37,9 @@ func (d *Downloader) close() { if d.Resp != nil { d.Resp.Body.Close() } + if d.cancel != nil { + d.cancel(nil) + } } // Size return the size of the download (or -1 if the server doesn't provide it) @@ -78,6 +82,14 @@ func (d *Downloader) Run() error { } in := d.Resp.Body + var timeoutTimer *time.Timer + if d.timeout > 0 { + timeoutTimer = time.AfterFunc(d.timeout, func() { + d.cancel(os.ErrDeadlineExceeded) + }) + defer timeoutTimer.Stop() + } + buff := [4096]byte{} for { n, err := in.Read(buff[:]) @@ -86,6 +98,10 @@ func (d *Downloader) Run() error { d.completedLock.Lock() d.completed += int64(n) d.completedLock.Unlock() + if d.timeout > 0 { + // Extend inactivity timeout deadline + timeoutTimer.Reset(d.timeout) + } } if err == io.EOF { break @@ -203,8 +219,17 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin } // Perform the actual GET request + ctx, cancel := context.WithCancelCause(ctx) + // Setup inactivity timeout for the GET request + if config.InactivityTimeout > 0 { + timer := time.AfterFunc(config.InactivityTimeout, func() { + cancel(os.ErrDeadlineExceeded) + }) + defer timer.Stop() + } req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) if err != nil { + cancel(nil) return nil, fmt.Errorf("setting up HTTP request: %s", err) } if config.ExtraHeaders != nil { @@ -218,6 +243,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin } resp, err := config.HttpClient.Do(req) if err != nil { + cancel(nil) return nil, err } @@ -225,6 +251,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin if !config.DoNotErrorOnNon2xxStatusCode { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { _ = resp.Body.Close() + cancel(nil) return &Downloader{ URL: reqURL, Resp: resp, // Return response for further inspection @@ -242,6 +269,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin f, err := os.OpenFile(file, flags, 0644) if err != nil { _ = resp.Body.Close() + cancel(nil) return nil, fmt.Errorf("opening %s for writing: %s", file, err) } @@ -251,6 +279,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin out: f, completed: completed, size: remoteSize, + cancel: cancel, timeout: config.InactivityTimeout, }, nil } diff --git a/downloader_test.go b/downloader_test.go index b0c5602..4015215 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -263,3 +263,88 @@ func TestTimeoutOnHEADCall(t *testing.T) { require.True(t, elapsed < 5*time.Second) fmt.Println("Download aborted due to timeout as expected after", elapsed) } + +func TestTimeoutOnGETCall(t *testing.T) { + slowHandler := func(w http.ResponseWriter, r *http.Request) { + if r.Method == "HEAD" { + w.Header().Set("Content-Length", "5000") + return + } + time.Sleep(3 * time.Second) // Delay response sending any data + } + mux := http.NewServeMux() + mux.HandleFunc("/slow", slowHandler) + server := httptest.NewServer(mux) + t.Cleanup(server.Close) + + tmpFile := makeTmpFile(t) + + config := downloader.Config{ + InactivityTimeout: 1 * time.Second, + } + + startTime := time.Now() + d, err := downloader.DownloadWithConfig(tmpFile, server.URL+"/slow", config) + require.Error(t, err) + require.Nil(t, d) + require.Contains(t, err.Error(), "i/o timeout") + elapsed := time.Since(startTime) + require.True(t, elapsed < 5*time.Second) + fmt.Println("Download aborted due to timeout as expected after", elapsed) +} + +func TestInactivityTimeout(t *testing.T) { + // Create a handler that sends some data, then becomes inactive + inactiveHandler := func(w http.ResponseWriter, r *http.Request) { + if r.Method == "HEAD" { + // For HEAD requests, just return Content-Length + w.Header().Set("Content-Length", "50") + return + } + w.(http.Flusher).Flush() // Ensure headers are sent immediately + + // Send initial burst of data (500 ms) + for range 5 { + time.Sleep(100 * time.Millisecond) + fmt.Fprintf(w, "AAAAA") + w.(http.Flusher).Flush() + } + // Rest inactive for 1000 ms + time.Sleep(1000 * time.Millisecond) + // Send more data (500 ms) + for range 5 { + time.Sleep(100 * time.Millisecond) + fmt.Fprintf(w, "AAAAA") + w.(http.Flusher).Flush() + } + // 2000 ms total time + } + + // Start test server + mux := http.NewServeMux() + mux.HandleFunc("/inactive", inactiveHandler) + server := httptest.NewServer(mux) + t.Cleanup(server.Close) + + // Request download with inactivity timeout of 500ms + tmpFile := makeTmpFile(t) + d, err := downloader.DownloadWithConfigAndContext( + t.Context(), tmpFile, server.URL+"/inactive", + downloader.Config{ + InactivityTimeout: 500 * time.Millisecond, + }, + ) + require.NoError(t, err) + + // Run the download, should timeout due to inactivity + startTime := time.Now() + err = d.Run() + elapsed := time.Since(startTime) + + // Check that we got a timeout error + require.Error(t, err) + require.Contains(t, err.Error(), "i/o timeout") + + // Check that it took around 1 second (initial burst of data 500ms + 500ms second timeout) + require.InEpsilon(t, 1000*time.Millisecond, elapsed, 0.05, "Elapsed time should be around 1 second, but is %v", elapsed) +} From 73eb8c81171be8afef05d182f93e71acb71360fd Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 9 Jan 2026 17:35:06 +0100 Subject: [PATCH 7/8] Added WatchDog abstraction --- downloader.go | 43 ++++++++++++------------------------------- watchdog.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 watchdog.go diff --git a/downloader.go b/downloader.go index 87873fb..674f83b 100644 --- a/downloader.go +++ b/downloader.go @@ -26,7 +26,7 @@ type Downloader struct { size int64 err error timeout time.Duration - cancel context.CancelCauseFunc + wdog watchdog } // Close the download @@ -37,9 +37,7 @@ func (d *Downloader) close() { if d.Resp != nil { d.Resp.Body.Close() } - if d.cancel != nil { - d.cancel(nil) - } + d.wdog.Cancel() } // Size return the size of the download (or -1 if the server doesn't provide it) @@ -72,8 +70,6 @@ func (d *Downloader) RunAndPoll(poll func(current int64), interval time.Duration // This method can be run in a goroutine to perform an asynchronous download; // it will close the Done channel when the download is completed or an error occurs. func (d *Downloader) Run() error { - defer d.close() - d.completedLock.Lock() skip := (d.completed == d.size) d.completedLock.Unlock() @@ -81,15 +77,9 @@ func (d *Downloader) Run() error { return d.Error() } - in := d.Resp.Body - var timeoutTimer *time.Timer - if d.timeout > 0 { - timeoutTimer = time.AfterFunc(d.timeout, func() { - d.cancel(os.ErrDeadlineExceeded) - }) - defer timeoutTimer.Stop() - } + defer d.close() + in := d.Resp.Body buff := [4096]byte{} for { n, err := in.Read(buff[:]) @@ -98,10 +88,8 @@ func (d *Downloader) Run() error { d.completedLock.Lock() d.completed += int64(n) d.completedLock.Unlock() - if d.timeout > 0 { - // Extend inactivity timeout deadline - timeoutTimer.Reset(d.timeout) - } + // Extend inactivity timeout deadline + d.wdog.Kick() } if err == io.EOF { break @@ -219,17 +207,11 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin } // Perform the actual GET request - ctx, cancel := context.WithCancelCause(ctx) // Setup inactivity timeout for the GET request - if config.InactivityTimeout > 0 { - timer := time.AfterFunc(config.InactivityTimeout, func() { - cancel(os.ErrDeadlineExceeded) - }) - defer timer.Stop() - } + ctx, wdog := newWatchdog(ctx, config.InactivityTimeout) req, err := http.NewRequestWithContext(ctx, "GET", reqURL, nil) if err != nil { - cancel(nil) + wdog.Cancel() return nil, fmt.Errorf("setting up HTTP request: %s", err) } if config.ExtraHeaders != nil { @@ -243,7 +225,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin } resp, err := config.HttpClient.Do(req) if err != nil { - cancel(nil) + wdog.Cancel() return nil, err } @@ -251,7 +233,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin if !config.DoNotErrorOnNon2xxStatusCode { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { _ = resp.Body.Close() - cancel(nil) + wdog.Cancel() return &Downloader{ URL: reqURL, Resp: resp, // Return response for further inspection @@ -269,7 +251,7 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin f, err := os.OpenFile(file, flags, 0644) if err != nil { _ = resp.Body.Close() - cancel(nil) + wdog.Cancel() return nil, fmt.Errorf("opening %s for writing: %s", file, err) } @@ -279,7 +261,6 @@ func DownloadWithConfigAndContext(ctx context.Context, file string, reqURL strin out: f, completed: completed, size: remoteSize, - cancel: cancel, - timeout: config.InactivityTimeout, + wdog: wdog, }, nil } diff --git a/watchdog.go b/watchdog.go new file mode 100644 index 0000000..3d166f3 --- /dev/null +++ b/watchdog.go @@ -0,0 +1,50 @@ +// +// Copyright 2025 Cristian Maglie. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// + +package downloader + +import ( + "context" + "os" + "time" +) + +type watchdog struct { + ctx context.Context + cancel context.CancelCauseFunc + timer *time.Timer + timeout time.Duration +} + +func newWatchdog(parent context.Context, timeout time.Duration) (context.Context, watchdog) { + ctx, cancel := context.WithCancelCause(parent) + var timer *time.Timer + if timeout > 0 { + timer = time.AfterFunc(timeout, func() { + // Cancel the context with a clear, standard error. + cancel(os.ErrDeadlineExceeded) + }) + } + return ctx, watchdog{ + ctx: ctx, + cancel: cancel, + timer: timer, + timeout: timeout, + } +} + +func (wd *watchdog) Kick() { + if wd.timeout > 0 { + wd.timer.Reset(wd.timeout) + } +} + +func (wd *watchdog) Cancel() { + if wd.timeout > 0 { + wd.timer.Stop() + } + wd.cancel(nil) +} From b2f4d93aa03e35d9c7a8d88a46fc53b04d41de19 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 9 Jan 2026 17:36:31 +0100 Subject: [PATCH 8/8] Inline call to internal close method --- downloader.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/downloader.go b/downloader.go index 674f83b..90a5a1c 100644 --- a/downloader.go +++ b/downloader.go @@ -29,17 +29,6 @@ type Downloader struct { wdog watchdog } -// Close the download -func (d *Downloader) close() { - if d.out != nil { - d.out.Close() - } - if d.Resp != nil { - d.Resp.Body.Close() - } - d.wdog.Cancel() -} - // Size return the size of the download (or -1 if the server doesn't provide it) func (d *Downloader) Size() int64 { return d.size @@ -77,7 +66,11 @@ func (d *Downloader) Run() error { return d.Error() } - defer d.close() + defer func() { + d.out.Close() + d.Resp.Body.Close() + d.wdog.Cancel() + }() in := d.Resp.Body buff := [4096]byte{}