From c44ba3be9f281c5aec2985adebaafbcb217419ee Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Thu, 8 Feb 2024 16:49:24 +0100 Subject: [PATCH] Use a HEAD instead of GET Even if Go only downloads the body for a GET request when the body-reader is used; it is still wrong and a HEAD request should be used instead. Additionaly some code polishing is done. Signed-off-by: Volker Theile --- pkg/sync/handler.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/sync/handler.go b/pkg/sync/handler.go index e558f3fd..521b89bf 100644 --- a/pkg/sync/handler.go +++ b/pkg/sync/handler.go @@ -34,7 +34,7 @@ func (h *HTTPHandler) GetSizeFromURL(url string) (size int64, err error) { ctx, cancel := context.WithTimeout(context.Background(), types.HTTPTimeout) defer cancel() - rr, err := http.NewRequestWithContext(ctx, "GET", url, nil) + rr, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil) if err != nil { return 0, err } @@ -50,11 +50,12 @@ func (h *HTTPHandler) GetSizeFromURL(url string) (size int64, err error) { return 0, fmt.Errorf("expected status code 200 from %s, got %s", url, resp.Status) } - if resp.Header.Get("Content-Length") == "" { + contentLength := resp.Header.Get("Content-Length") + if contentLength == "" { // -1 indicates unknown size size = -1 } else { - size, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64) + size, err = strconv.ParseInt(contentLength, 10, 64) if err != nil { return 0, err } @@ -67,7 +68,7 @@ func (h *HTTPHandler) DownloadFromURL(ctx context.Context, url, filePath string, ctx, cancel := context.WithCancel(ctx) defer cancel() - rr, err := http.NewRequestWithContext(ctx, "GET", url, nil) + rr, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return 0, err }