Skip to content

Commit

Permalink
[cpackget] Handle Cookie headers in HTTP GET requests when required
Browse files Browse the repository at this point in the history
  • Loading branch information
brondani committed Dec 13, 2024
1 parent 29d13de commit 9ef8197
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ func DownloadFile(URL string, timeout int) (string, error) {
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusForbidden {
cookie := resp.Header.Get("Set-Cookie")
if len(cookie) > 0 {
// add cookie and resend GET request
log.Debugf("Cookie: %s", cookie)
req.Header.Add("Cookie", cookie)
resp, err = client.Do(req)
if err != nil {
log.Error(err)
return "", fmt.Errorf("\"%s\": %w", URL, errs.ErrFailedDownloadingFile)
}
}
}

if resp.StatusCode != http.StatusOK {
log.Debugf("bad status: %s", resp.Status)
return "", fmt.Errorf("\"%s\": %w", URL, errs.ErrBadRequest)
Expand Down

0 comments on commit 9ef8197

Please sign in to comment.