From 919bd8ab094dd05c78de6d21da8e73fd9caef425 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 10 Nov 2022 11:43:32 +0100 Subject: [PATCH] reference: add const for (optional) port, and rename "domain" variable The `domain` variable didn't make it clear that this could include port-numbers as well, so renaming it makes that more visible. Signed-off-by: Sebastiaan van Stijn --- reference/regexp.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/reference/regexp.go b/reference/regexp.go index 748623246..7fb9ee9f3 100644 --- a/reference/regexp.go +++ b/reference/regexp.go @@ -23,6 +23,10 @@ const ( // repository name to start with a component as defined by DomainRegexp. domainNameComponent = `(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])` + // optionalPort matches an optional port-number including the port separator + // (e.g. ":80"). + optionalPort = `(?::[0-9]+)?` + // tag matches valid tag names. From docker/docker:graph/tags.go. tag = `[\w][\w.-]{0,127}` @@ -66,7 +70,7 @@ var ( // allowed by the URI Host subcomponent on rfc3986 to ensure backwards // compatibility with Docker image names. - domain = host + optional(`:[0-9]+`) + domainAndPort = host + optionalPort // DomainRegexp matches hostname or IP-addresses, optionally including a port // number. It defines the structure of potential domain components that may be @@ -77,7 +81,7 @@ var ( // addresses such as IPv4-Mapped). // // [rfc6874]: https://www.rfc-editor.org/rfc/rfc6874. - DomainRegexp = regexp.MustCompile(domain) + DomainRegexp = regexp.MustCompile(domainAndPort) // TagRegexp matches valid tag names. From docker/docker:graph/tags.go. TagRegexp = regexp.MustCompile(tag) @@ -97,7 +101,7 @@ var ( // character, with following parts able to be separated by a separator // (one period, one or two underscore and multiple dashes). pathComponent = alphanumeric + optional(repeated(separator, alphanumeric)) - namePat = optional(domain+`/`) + pathComponent + optional(repeated(`/`+pathComponent)) + namePat = optional(domainAndPort+`/`) + pathComponent + optional(repeated(`/`+pathComponent)) // NameRegexp is the format for the name component of references, including // an optional domain and port, but without tag or digest suffix. @@ -105,7 +109,7 @@ var ( // anchoredNameRegexp is used to parse a name value, capturing the // domain and trailing components. - anchoredNameRegexp = regexp.MustCompile(anchored(optional(capture(domain), `/`), capture(pathComponent, optional(repeated(`/`+pathComponent))))) + anchoredNameRegexp = regexp.MustCompile(anchored(optional(capture(domainAndPort), `/`), capture(pathComponent, optional(repeated(`/`+pathComponent))))) referencePat = anchored(capture(namePat), optional(`:`, capture(tag)), optional(`@`, capture(digestPat)))