2015-09-08 23:00:48 +00:00
|
|
|
package reference
|
|
|
|
|
|
|
|
import "regexp"
|
|
|
|
|
|
|
|
var (
|
2015-12-04 22:40:09 +00:00
|
|
|
// alphaNumericRegexp defines the alpha numeric atom, typically a
|
|
|
|
// component of names. This only allows lower case characters and digits.
|
|
|
|
alphaNumericRegexp = match(`[a-z0-9]+`)
|
2015-10-09 23:01:01 +00:00
|
|
|
|
2015-12-04 22:40:09 +00:00
|
|
|
// separatorRegexp defines the separators allowed to be embedded in name
|
|
|
|
// components. This allow one period, one or two underscore and multiple
|
|
|
|
// dashes.
|
|
|
|
separatorRegexp = match(`(?:[._]|__|[-]*)`)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2015-12-04 22:40:09 +00:00
|
|
|
// nameComponentRegexp restricts registry path component names to start
|
|
|
|
// with at least one letter or number, with following parts able to be
|
|
|
|
// separated by one period, one or two underscore and multiple dashes.
|
|
|
|
nameComponentRegexp = expression(
|
|
|
|
alphaNumericRegexp,
|
|
|
|
optional(repeated(separatorRegexp, alphaNumericRegexp)))
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2016-06-09 18:32:23 +00:00
|
|
|
// domainComponentRegexp restricts the registry domain component of a
|
|
|
|
// repository name to start with a component as defined by domainRegexp
|
2015-12-04 22:40:09 +00:00
|
|
|
// and followed by an optional port.
|
2016-06-09 18:32:23 +00:00
|
|
|
domainComponentRegexp = match(`(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])`)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2016-06-09 18:32:23 +00:00
|
|
|
// domainRegexp defines the structure of potential domain components
|
2015-12-04 22:40:09 +00:00
|
|
|
// that may be part of image names. This is purposely a subset of what is
|
|
|
|
// allowed by DNS to ensure backwards compatibility with Docker image
|
|
|
|
// names.
|
2016-06-09 18:32:23 +00:00
|
|
|
domainRegexp = expression(
|
|
|
|
domainComponentRegexp,
|
|
|
|
optional(repeated(literal(`.`), domainComponentRegexp)),
|
2015-12-04 22:40:09 +00:00
|
|
|
optional(literal(`:`), match(`[0-9]+`)))
|
2015-09-08 23:00:48 +00:00
|
|
|
|
|
|
|
// TagRegexp matches valid tag names. From docker/docker:graph/tags.go.
|
2015-12-04 22:40:09 +00:00
|
|
|
TagRegexp = match(`[\w][\w.-]{0,127}`)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
|
|
|
// anchoredTagRegexp matches valid tag names, anchored at the start and
|
|
|
|
// end of the matched string.
|
2015-12-04 22:40:09 +00:00
|
|
|
anchoredTagRegexp = anchored(TagRegexp)
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2015-10-10 00:09:54 +00:00
|
|
|
// DigestRegexp matches valid digests.
|
2015-12-04 22:40:09 +00:00
|
|
|
DigestRegexp = match(`[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}`)
|
2015-10-10 00:09:54 +00:00
|
|
|
|
|
|
|
// anchoredDigestRegexp matches valid digests, anchored at the start and
|
|
|
|
// end of the matched string.
|
2015-12-04 22:40:09 +00:00
|
|
|
anchoredDigestRegexp = anchored(DigestRegexp)
|
2015-10-10 00:09:54 +00:00
|
|
|
|
2015-09-08 23:00:48 +00:00
|
|
|
// NameRegexp is the format for the name component of references. The
|
2016-06-09 18:32:23 +00:00
|
|
|
// regexp has capturing groups for the domain and name part omitting
|
2016-02-11 00:26:29 +00:00
|
|
|
// the separating forward slash from either.
|
2015-12-04 22:40:09 +00:00
|
|
|
NameRegexp = expression(
|
2016-06-09 18:32:23 +00:00
|
|
|
optional(domainRegexp, literal(`/`)),
|
2015-12-04 22:40:09 +00:00
|
|
|
nameComponentRegexp,
|
|
|
|
optional(repeated(literal(`/`), nameComponentRegexp)))
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2015-12-04 22:40:09 +00:00
|
|
|
// anchoredNameRegexp is used to parse a name value, capturing the
|
2016-06-09 18:32:23 +00:00
|
|
|
// domain and trailing components.
|
2015-12-04 22:40:09 +00:00
|
|
|
anchoredNameRegexp = anchored(
|
2016-06-09 18:32:23 +00:00
|
|
|
optional(capture(domainRegexp), literal(`/`)),
|
2015-12-04 22:40:09 +00:00
|
|
|
capture(nameComponentRegexp,
|
|
|
|
optional(repeated(literal(`/`), nameComponentRegexp))))
|
2015-09-08 23:00:48 +00:00
|
|
|
|
2015-12-04 22:40:09 +00:00
|
|
|
// ReferenceRegexp is the full supported format of a reference. The regexp
|
|
|
|
// is anchored and has capturing groups for name, tag, and digest
|
|
|
|
// components.
|
|
|
|
ReferenceRegexp = anchored(capture(NameRegexp),
|
|
|
|
optional(literal(":"), capture(TagRegexp)),
|
|
|
|
optional(literal("@"), capture(DigestRegexp)))
|
2015-09-08 23:00:48 +00:00
|
|
|
)
|
2015-12-04 22:40:09 +00:00
|
|
|
|
|
|
|
// match compiles the string to a regular expression.
|
|
|
|
var match = regexp.MustCompile
|
|
|
|
|
|
|
|
// literal compiles s into a literal regular expression, escaping any regexp
|
|
|
|
// reserved characters.
|
|
|
|
func literal(s string) *regexp.Regexp {
|
|
|
|
re := match(regexp.QuoteMeta(s))
|
|
|
|
|
|
|
|
if _, complete := re.LiteralPrefix(); !complete {
|
|
|
|
panic("must be a literal")
|
|
|
|
}
|
|
|
|
|
|
|
|
return re
|
|
|
|
}
|
|
|
|
|
|
|
|
// expression defines a full expression, where each regular expression must
|
|
|
|
// follow the previous.
|
|
|
|
func expression(res ...*regexp.Regexp) *regexp.Regexp {
|
|
|
|
var s string
|
|
|
|
for _, re := range res {
|
|
|
|
s += re.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return match(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// optional wraps the expression in a non-capturing group and makes the
|
|
|
|
// production optional.
|
|
|
|
func optional(res ...*regexp.Regexp) *regexp.Regexp {
|
|
|
|
return match(group(expression(res...)).String() + `?`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// repeated wraps the regexp in a non-capturing group to get one or more
|
|
|
|
// matches.
|
|
|
|
func repeated(res ...*regexp.Regexp) *regexp.Regexp {
|
|
|
|
return match(group(expression(res...)).String() + `+`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// group wraps the regexp in a non-capturing group.
|
|
|
|
func group(res ...*regexp.Regexp) *regexp.Regexp {
|
|
|
|
return match(`(?:` + expression(res...).String() + `)`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// capture wraps the expression in a capturing group.
|
|
|
|
func capture(res ...*regexp.Regexp) *regexp.Regexp {
|
|
|
|
return match(`(` + expression(res...).String() + `)`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// anchored anchors the regular expression by adding start and end delimiters.
|
|
|
|
func anchored(res ...*regexp.Regexp) *regexp.Regexp {
|
|
|
|
return match(`^` + expression(res...).String() + `$`)
|
|
|
|
}
|