fix: request retry with error
this fixes a subtle bug with makeRequestWithRetry where an HTTP status error on a retried request will potentially not return the right err
This commit is contained in:
parent
3773fb6465
commit
cf29bd2d72
1 changed files with 31 additions and 34 deletions
|
@ -1132,49 +1132,46 @@ func GetSHA256Digest(r io.Reader) (string, int64) {
|
||||||
var errUnauthorized = fmt.Errorf("unauthorized")
|
var errUnauthorized = fmt.Errorf("unauthorized")
|
||||||
|
|
||||||
func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
|
func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
|
||||||
resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts)
|
for i := 0; i < 2; i++ {
|
||||||
if err != nil {
|
resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts)
|
||||||
if !errors.Is(err, context.Canceled) {
|
|
||||||
log.Printf("request failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case resp.StatusCode == http.StatusUnauthorized:
|
|
||||||
// Handle authentication error with one retry
|
|
||||||
auth := resp.Header.Get("www-authenticate")
|
|
||||||
authRedir := ParseAuthRedirectString(auth)
|
|
||||||
token, err := getAuthToken(ctx, authRedir)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !errors.Is(err, context.Canceled) {
|
||||||
|
log.Printf("request failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
regOpts.Token = token
|
|
||||||
if body != nil {
|
switch {
|
||||||
_, err = body.Seek(0, io.SeekStart)
|
case resp.StatusCode == http.StatusUnauthorized:
|
||||||
|
// Handle authentication error with one retry
|
||||||
|
auth := resp.Header.Get("www-authenticate")
|
||||||
|
authRedir := ParseAuthRedirectString(auth)
|
||||||
|
token, err := getAuthToken(ctx, authRedir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
regOpts.Token = token
|
||||||
|
if body != nil {
|
||||||
|
_, err = body.Seek(0, io.SeekStart)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case resp.StatusCode == http.StatusNotFound:
|
||||||
|
return nil, os.ErrNotExist
|
||||||
|
case resp.StatusCode >= http.StatusBadRequest:
|
||||||
|
responseBody, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%d: %s", resp.StatusCode, err)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("%d: %s", resp.StatusCode, responseBody)
|
||||||
|
default:
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts)
|
|
||||||
if resp.StatusCode == http.StatusUnauthorized {
|
|
||||||
return nil, errUnauthorized
|
|
||||||
}
|
|
||||||
|
|
||||||
return resp, err
|
|
||||||
case resp.StatusCode == http.StatusNotFound:
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
case resp.StatusCode >= http.StatusBadRequest:
|
|
||||||
responseBody, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%d: %s", resp.StatusCode, err)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("%d: %s", resp.StatusCode, responseBody)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return nil, errUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeRequest(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) {
|
func makeRequest(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) {
|
||||||
|
|
Loading…
Reference in a new issue