From b5b31ebfb754fb0653b86a2e7c4ba6d92ca71d65 Mon Sep 17 00:00:00 2001 From: DarthSim Date: Tue, 26 Aug 2025 20:40:02 +0300 Subject: [PATCH] Remove imagefetcher.bodyReader; Don't cancel request on error in FetchImage --- imagefetcher/request.go | 40 ++++------------------------------------ 1 file changed, 4 insertions(+), 36 deletions(-) diff --git a/imagefetcher/request.go b/imagefetcher/request.go index add89a1c..5c90912d 100644 --- a/imagefetcher/request.go +++ b/imagefetcher/request.go @@ -72,19 +72,12 @@ func (r *Request) Send() (*http.Response, error) { func (r *Request) FetchImage() (*http.Response, error) { res, err := r.Send() if err != nil { - r.cancel() return nil, err } - // Closes the response body and cancels request context - cancel := func() { - res.Body.Close() - r.cancel() - } - // If the source image was not modified, close the body and NotModifiedError if res.StatusCode == http.StatusNotModified { - cancel() + res.Body.Close() return nil, newNotModifiedError(res.Header) } @@ -93,29 +86,22 @@ func (r *Request) FetchImage() (*http.Response, error) { if res.StatusCode == http.StatusPartialContent { err = checkPartialContentResponse(res) if err != nil { - cancel() + res.Body.Close() return nil, err } } else if res.StatusCode != http.StatusOK { body := extractErraticBody(res) - cancel() + res.Body.Close() return nil, newImageResponseStatusError(res.StatusCode, body) } // If the response is gzip encoded, wrap it in a gzip reader err = wrapGzipBody(res) if err != nil { - cancel() + res.Body.Close() return nil, err } - // Wrap the response body in a bodyReader to ensure the request context - // is cancelled when the body is closed - res.Body = &bodyReader{ - body: res.Body, - request: r, - } - return res, nil } @@ -188,24 +174,6 @@ func wrapGzipBody(res *http.Response) error { return nil } -// bodyReader is a wrapper around io.ReadCloser which closes original request context -// when the body is closed. -type bodyReader struct { - body io.ReadCloser // The body to read from - request *Request -} - -// Read reads data from the response body into the provided byte slice -func (r *bodyReader) Read(p []byte) (int, error) { - return r.body.Read(p) -} - -// Close closes the response body and cancels the request context -func (r *bodyReader) Close() error { - defer r.request.cancel() - return r.body.Close() -} - // gzipReadCloser is a wrapper around gzip.Reader which also closes the original body type gzipReadCloser struct { *gzip.Reader