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>
This commit is contained in:
Sebastiaan van Stijn 2023-09-08 12:03:19 +02:00
parent 0f7a65e59b
commit e9ad878df6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -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
}