Skip to content

Commit

Permalink
Use a HEAD instead of GET
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
votdev authored and innobead committed Feb 20, 2024
1 parent afe5ae8 commit c44ba3b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/sync/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit c44ba3b

Please sign in to comment.