Skip to content

Commit ab3e12a

Browse files
committed
Fix some details in the ReferenceParser.
1 parent 48f9709 commit ab3e12a

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

client/src/main/groovy/de/gesellix/docker/client/distribution/ReferenceParser.groovy

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ReferenceParser {
3535

3636
String DomainRegexp = "${domainComponentRegexp}(?:(?:\\.${domainComponentRegexp})+)?(?::[0-9]+)?"
3737

38-
String NameRegexp = "(?:${DomainRegexp})?/${nameComponentRegexp}(?:(?:/)+)?${nameComponentRegexp}"
38+
String NameRegexp = "(?:${DomainRegexp}/)?${nameComponentRegexp}(?:(?:/${nameComponentRegexp})+)?"
3939

4040
String TagRegexp = "[\\w][\\w.-]{0,127}"
4141

@@ -82,10 +82,9 @@ class ReferenceParser {
8282
log.debug "anchoredNameMatcher.groupCount(): ${anchoredNameMatcher.groupCount()}"
8383

8484
if (anchoredNameMatcher.matches()
85-
&& anchoredNameMatcher.groupCount() == 3
86-
&& anchoredNameMatcher.group(1) != null
85+
&& anchoredNameMatcher.groupCount() >= 2
8786
&& anchoredNameMatcher.group(2) != null) {
88-
repo.domain = anchoredNameMatcher.group(1)
87+
repo.domain = anchoredNameMatcher.group(1) ?: ""
8988
repo.path = anchoredNameMatcher.group(2)
9089
}
9190
else {
@@ -158,13 +157,13 @@ class ReferenceParser {
158157
throw new IllegalArgumentException("invalid checksum digest format")
159158
}
160159

161-
def algorithm = s.substring(0, i)
160+
def algorithm = s.substring(0, i).toUpperCase()
162161
if (!knownAlgorithms.contains(algorithm)) {
163162
throw new IllegalArgumentException("unsupported digest algorithm")
164163
}
165164

166165
// Digests much always be hex-encoded, ensuring that their hex portion will always be size*2
167-
if (algorithmDigestSizes[algorithm] * 2 != s.substring(i + 1)) {
166+
if (algorithmDigestSizes[algorithm] * 2 != s.substring(i + 1).length()) {
168167
throw new IllegalArgumentException("invalid checksum digest length")
169168
}
170169
}
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
package de.gesellix.docker.client.distribution
22

3-
class ReferenceParserTest {
3+
import spock.lang.Specification
4+
5+
class ReferenceParserTest extends Specification {
6+
7+
ReferenceParser parser
8+
9+
def setup() {
10+
parser = new ReferenceParser()
11+
}
12+
13+
def "parse reference"() {
14+
when:
15+
def reference = parser.parse(s)
16+
then:
17+
reference == r
18+
where:
19+
s || r
20+
"docker.io/library/alpine:edge" || [repo: [domain: "docker.io", path: "library/alpine"], tag: "edge"]
21+
"test_com" || [domain: "", path: "test_com"]
22+
"test.com:tag" || [repo: [domain: "", path: "test.com"], tag: "tag"]
23+
"test.com:5000" || [repo: [domain: "", path: "test.com"], tag: "5000"]
24+
"test.com/repo:tag" || [repo: [domain: "test.com", path: "repo"], tag: "tag"]
25+
"test:5000/repo" || [domain: "test:5000", path: "repo"]
26+
"test:5000/repo:tag" || [repo: [domain: "test:5000", path: "repo"], tag: "tag"]
27+
"lowercase:Uppercase" || [repo: [domain: "", path: "lowercase"], tag: "Uppercase"]
28+
"foo_bar.com:8080" || [repo: [domain: "", path: "foo_bar.com"], tag: "8080"]
29+
"foo/foo_bar.com:8080" || [repo: [domain: "foo", path: "foo_bar.com"], tag: "8080"]
30+
"sub-dom1.foo.com/bar/baz/quux" || [domain: "sub-dom1.foo.com", path: "bar/baz/quux"]
31+
"sub-dom1.foo.com/bar/baz/quux:some-long-tag" || [repo: [domain: "sub-dom1.foo.com", path: "bar/baz/quux"], tag: "some-long-tag"]
32+
"b.gcr.io/test.example.com/my-app:test.example.com" || [repo: [domain: "b.gcr.io", path: "test.example.com/my-app"], tag: "test.example.com"]
33+
// ☃.com in punycode
34+
"xn--n3h.com/myimage:xn--n3h.com" || [repo: [domain: "xn--n3h.com", path: "myimage"], tag: "xn--n3h.com"]
35+
// 🐳.com in punycode
36+
"xn--7o8h.com/myimage:xn--7o8h.com@sha512:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" || [repo: [domain: "xn--7o8h.com", path: "myimage"], tag: "xn--7o8h.com", digest: "sha512:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"]
37+
"test:5000/repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" || [repo: [domain: "test:5000", path: "repo"], digest: "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"]
38+
"test:5000/repo:tag@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" || [repo: [domain: "test:5000", path: "repo"], tag: "tag", digest: "sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"]
39+
}
440
}

0 commit comments

Comments
 (0)