Sfoglia il codice sorgente

registry: v1Endpoint.ping: add fast-path for X-Docker-Registry-Standalone

This function was making a request to the `_ping` endpoint, which (if
implemented) would return a JSON response, which we unmarshal (the only
field we use from the response is the `Standalone` field).

However, if the response had a `X-Docker-Registry-Standalone`, that header
took precedence, and would overwrite the earlier `Standalone` value we
obtained from the JSON response.

This patch adds a fast-path for situations where the header is present,
in which case we can skip handling the JSON response altogether.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 1 anno fa
parent
commit
e9ad878df6
1 ha cambiato i file con 10 aggiunte e 9 eliminazioni
  1. 10 9
      registry/search_endpoint_v1.go

+ 10 - 9
registry/search_endpoint_v1.go

@@ -140,6 +140,16 @@ func (e *v1Endpoint) ping() (v1PingResult, error) {
 
 	defer resp.Body.Close()
 
+	if v := resp.Header.Get("X-Docker-Registry-Standalone"); v != "" {
+		info := v1PingResult{}
+		// Accepted values are "1", and "true" (case-insensitive).
+		if v == "1" || strings.EqualFold(v, "true") {
+			info.Standalone = true
+		}
+		log.G(context.TODO()).Debugf("v1PingResult.Standalone (from X-Docker-Registry-Standalone header): %t", info.Standalone)
+		return info, nil
+	}
+
 	jsonString, err := io.ReadAll(resp.Body)
 	if err != nil {
 		return v1PingResult{}, invalidParamWrapf(err, "error while reading response from %s", pingURL)
@@ -155,15 +165,6 @@ func (e *v1Endpoint) ping() (v1PingResult, error) {
 		// don't stop here. Just assume sane defaults
 	}
 
-	standalone := resp.Header.Get("X-Docker-Registry-Standalone")
-
-	// Accepted values are "true" (case-insensitive) and "1".
-	if strings.EqualFold(standalone, "true") || standalone == "1" {
-		info.Standalone = true
-	} else if len(standalone) > 0 {
-		// there is a header set, and it is not "true" or "1", so assume fails
-		info.Standalone = false
-	}
 	log.G(context.TODO()).Debugf("v1PingResult.Standalone: %t", info.Standalone)
 	return info, nil
 }