Browse code

Split API Version header when checking for v2

Since the Docker-Distribution-API-Version header value may contain multiple
space delimited versions as well as many instances of the header key, the
header value is now split on whitespace characters to iterate over all versions
that may be listed in one instance of the header.

Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)

Josh Hawn authored on 2015/01/22 05:11:53
Showing 2 changed files
... ...
@@ -231,10 +231,13 @@ func (e *Endpoint) pingV2() (RegistryInfo, error) {
231 231
 	// Ensure it supports the v2 Registry API.
232 232
 	var supportsV2 bool
233 233
 
234
-	for _, versionName := range resp.Header[http.CanonicalHeaderKey("Docker-Distribution-API-Version")] {
235
-		if versionName == "registry/2.0" {
236
-			supportsV2 = true
237
-			break
234
+HeaderLoop:
235
+	for _, supportedVersions := range resp.Header[http.CanonicalHeaderKey("Docker-Distribution-API-Version")] {
236
+		for _, versionName := range strings.Fields(supportedVersions) {
237
+			if versionName == "registry/2.0" {
238
+				supportsV2 = true
239
+				break HeaderLoop
240
+			}
238 241
 		}
239 242
 	}
240 243
 
... ...
@@ -42,7 +42,9 @@ func TestValidateEndpointAmbiguousAPIVersion(t *testing.T) {
42 42
 	})
43 43
 
44 44
 	requireBasicAuthHandlerV2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45
-		w.Header().Add("Docker-Distribution-API-Version", "registry/2.0")
45
+		// This mock server supports v2.0, v2.1, v42.0, and v100.0
46
+		w.Header().Add("Docker-Distribution-API-Version", "registry/100.0 registry/42.0")
47
+		w.Header().Add("Docker-Distribution-API-Version", "registry/2.0 registry/2.1")
46 48
 		requireBasicAuthHandler.ServeHTTP(w, r)
47 49
 	})
48 50