Przeglądaj źródła

loosen http status code checks

Michael Yang 1 rok temu
rodzic
commit
246dc65417
5 zmienionych plików z 16 dodań i 17 usunięć
  1. 2 2
      api/client.go
  2. 1 1
      server/auth.go
  3. 1 1
      server/download.go
  4. 8 8
      server/images.go
  5. 4 5
      server/upload.go

+ 2 - 2
api/client.go

@@ -29,7 +29,7 @@ type Client struct {
 }
 }
 
 
 func checkError(resp *http.Response, body []byte) error {
 func checkError(resp *http.Response, body []byte) error {
-	if resp.StatusCode >= 200 && resp.StatusCode < 400 {
+	if resp.StatusCode < http.StatusBadRequest {
 		return nil
 		return nil
 	}
 	}
 
 
@@ -165,7 +165,7 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f
 			return fmt.Errorf(errorResponse.Error)
 			return fmt.Errorf(errorResponse.Error)
 		}
 		}
 
 
-		if response.StatusCode >= 400 {
+		if response.StatusCode >= http.StatusBadRequest {
 			return StatusError{
 			return StatusError{
 				StatusCode:   response.StatusCode,
 				StatusCode:   response.StatusCode,
 				Status:       response.Status,
 				Status:       response.Status,

+ 1 - 1
server/auth.go

@@ -109,7 +109,7 @@ func getAuthToken(ctx context.Context, redirData AuthRedirect, regOpts *Registry
 	}
 	}
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
-	if resp.StatusCode != http.StatusOK {
+	if resp.StatusCode >= http.StatusBadRequest {
 		body, _ := io.ReadAll(resp.Body)
 		body, _ := io.ReadAll(resp.Body)
 		return "", fmt.Errorf("on pull registry responded with code %d: %s", resp.StatusCode, body)
 		return "", fmt.Errorf("on pull registry responded with code %d: %s", resp.StatusCode, body)
 	}
 	}

+ 1 - 1
server/download.go

@@ -168,7 +168,7 @@ func doDownload(ctx context.Context, opts downloadOpts, f *FileDownload) error {
 	}
 	}
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
-	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
+	if resp.StatusCode >= http.StatusBadRequest {
 		body, _ := io.ReadAll(resp.Body)
 		body, _ := io.ReadAll(resp.Body)
 		return fmt.Errorf("%w: on download registry responded with code %d: %v", errDownload, resp.StatusCode, string(body))
 		return fmt.Errorf("%w: on download registry responded with code %d: %v", errDownload, resp.StatusCode, string(body))
 	}
 	}

+ 8 - 8
server/images.go

@@ -1086,11 +1086,11 @@ func pullModelManifest(ctx context.Context, mp ModelPath, regOpts *RegistryOptio
 	}
 	}
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
-	// Check for success: For a successful upload, the Docker registry will respond with a 201 Created
-	if resp.StatusCode != http.StatusOK {
+	if resp.StatusCode >= http.StatusBadRequest {
 		if resp.StatusCode == http.StatusNotFound {
 		if resp.StatusCode == http.StatusNotFound {
 			return nil, fmt.Errorf("model not found")
 			return nil, fmt.Errorf("model not found")
 		}
 		}
+
 		body, _ := io.ReadAll(resp.Body)
 		body, _ := io.ReadAll(resp.Body)
 		return nil, fmt.Errorf("on pull registry responded with code %d: %s", resp.StatusCode, body)
 		return nil, fmt.Errorf("on pull registry responded with code %d: %s", resp.StatusCode, body)
 	}
 	}
@@ -1151,7 +1151,7 @@ func checkBlobExistence(ctx context.Context, mp ModelPath, digest string, regOpt
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
 	// Check for success: If the blob exists, the Docker registry will respond with a 200 OK
 	// Check for success: If the blob exists, the Docker registry will respond with a 200 OK
-	return resp.StatusCode == http.StatusOK, nil
+	return resp.StatusCode < http.StatusBadRequest, nil
 }
 }
 
 
 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) {
@@ -1165,10 +1165,8 @@ func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.UR
 
 
 		status = resp.Status
 		status = resp.Status
 
 
-		switch resp.StatusCode {
-		case http.StatusAccepted, http.StatusCreated:
-			return resp, nil
-		case http.StatusUnauthorized:
+		switch {
+		case resp.StatusCode == http.StatusUnauthorized:
 			auth := resp.Header.Get("www-authenticate")
 			auth := resp.Header.Get("www-authenticate")
 			authRedir := ParseAuthRedirectString(auth)
 			authRedir := ParseAuthRedirectString(auth)
 			token, err := getAuthToken(ctx, authRedir, regOpts)
 			token, err := getAuthToken(ctx, authRedir, regOpts)
@@ -1184,9 +1182,11 @@ func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.UR
 			}
 			}
 
 
 			continue
 			continue
-		default:
+		case resp.StatusCode >= http.StatusBadRequest:
 			body, _ := io.ReadAll(resp.Body)
 			body, _ := io.ReadAll(resp.Body)
 			return nil, fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
 			return nil, fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
+		default:
+			return resp, nil
 		}
 		}
 	}
 	}
 
 

+ 4 - 5
server/upload.go

@@ -111,9 +111,8 @@ func uploadBlobChunked(ctx context.Context, mp ModelPath, requestURL *url.URL, l
 			}
 			}
 			defer resp.Body.Close()
 			defer resp.Body.Close()
 
 
-			switch resp.StatusCode {
-			case http.StatusAccepted, http.StatusCreated:
-			case http.StatusUnauthorized:
+			switch {
+			case resp.StatusCode == http.StatusUnauthorized:
 				auth := resp.Header.Get("www-authenticate")
 				auth := resp.Header.Get("www-authenticate")
 				authRedir := ParseAuthRedirectString(auth)
 				authRedir := ParseAuthRedirectString(auth)
 				token, err := getAuthToken(ctx, authRedir, regOpts)
 				token, err := getAuthToken(ctx, authRedir, regOpts)
@@ -127,7 +126,7 @@ func uploadBlobChunked(ctx context.Context, mp ModelPath, requestURL *url.URL, l
 				}
 				}
 
 
 				continue
 				continue
-			default:
+			case resp.StatusCode >= http.StatusBadRequest:
 				body, _ := io.ReadAll(resp.Body)
 				body, _ := io.ReadAll(resp.Body)
 				return fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
 				return fmt.Errorf("on upload registry responded with code %d: %s", resp.StatusCode, body)
 			}
 			}
@@ -158,7 +157,7 @@ func uploadBlobChunked(ctx context.Context, mp ModelPath, requestURL *url.URL, l
 	}
 	}
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
-	if resp.StatusCode != http.StatusCreated {
+	if resp.StatusCode >= http.StatusBadRequest {
 		body, _ := io.ReadAll(resp.Body)
 		body, _ := io.ReadAll(resp.Body)
 		return fmt.Errorf("on finish upload registry responded with code %d: %v", resp.StatusCode, string(body))
 		return fmt.Errorf("on finish upload registry responded with code %d: %v", resp.StatusCode, string(body))
 	}
 	}