|
17 | 17 | // {PORT/APP_NAME}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME} |
18 | 18 | `^(?P<AppName>%[1]s)--(?P<AgentName>%[1]s)--(?P<WorkspaceName>%[1]s)--(?P<Username>%[1]s)$`, |
19 | 19 | nameRegex)) |
20 | | -) |
21 | | - |
22 | | -// SplitSubdomain splits a subdomain from the rest of the hostname. E.g.: |
23 | | -// - "foo.bar.com" becomes "foo", "bar.com" |
24 | | -// - "foo.bar.baz.com" becomes "foo", "bar.baz.com" |
25 | | -// - "foo" becomes "foo", "" |
26 | | -func SplitSubdomain(hostname string) (subdomain string, rest string) { |
27 | | - toks := strings.SplitN(hostname, ".", 2) |
28 | | - if len(toks) < 2 { |
29 | | - return toks[0], "" |
30 | | - } |
31 | 20 |
|
32 | | - return toks[0], toks[1] |
33 | | -} |
| 21 | + validHostnameLabelRegex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`) |
| 22 | +) |
34 | 23 |
|
35 | 24 | // ApplicationURL is a parsed application URL hostname. |
36 | 25 | type ApplicationURL struct { |
@@ -111,3 +100,81 @@ func HostnamesMatch(a, b string) bool { |
111 | 100 |
|
112 | 101 | return strings.EqualFold(aHost, bHost) |
113 | 102 | } |
| 103 | + |
| 104 | +// CompileHostnamePattern compiles a hostname pattern into a regular expression. |
| 105 | +// A hostname pattern is a string that may contain a single wildcard character |
| 106 | +// at the beginning. The wildcard character matches any number of hostname-safe |
| 107 | +// characters excluding periods. The pattern is case-insensitive. |
| 108 | +// |
| 109 | +// The supplied pattern: |
| 110 | +// - must not start or end with a period |
| 111 | +// - must contain exactly one asterisk at the beginning |
| 112 | +// - must not contain any other wildcard characters |
| 113 | +// - must not contain any other characters that are not hostname-safe (including |
| 114 | +// whitespace) |
| 115 | +// - must contain at least two hostname labels/segments (i.e. "foo" or "*" are |
| 116 | +// not valid patterns, but "foo.bar" and "*.bar" are). |
| 117 | +// |
| 118 | +// The returned regular expression will match an entire hostname with optional |
| 119 | +// trailing periods and whitespace. The first submatch will be the wildcard |
| 120 | +// match. |
| 121 | +func CompileHostnamePattern(pattern string) (*regexp.Regexp, error) { |
| 122 | + pattern = strings.ToLower(pattern) |
| 123 | + if strings.Contains(pattern, "http:") || strings.Contains(pattern, "https:") { |
| 124 | + return nil, xerrors.Errorf("hostname pattern must not contain a scheme: %q", pattern) |
| 125 | + } |
| 126 | + if strings.Contains(pattern, ":") { |
| 127 | + return nil, xerrors.Errorf("hostname pattern must not contain a port: %q", pattern) |
| 128 | + } |
| 129 | + if strings.HasPrefix(pattern, ".") || strings.HasSuffix(pattern, ".") { |
| 130 | + return nil, xerrors.Errorf("hostname pattern must not start or end with a period: %q", pattern) |
| 131 | + } |
| 132 | + if strings.Count(pattern, ".") < 1 { |
| 133 | + return nil, xerrors.Errorf("hostname pattern must contain at least two labels/segments: %q", pattern) |
| 134 | + } |
| 135 | + if strings.Count(pattern, "*") != 1 { |
| 136 | + return nil, xerrors.Errorf("hostname pattern must contain exactly one asterisk: %q", pattern) |
| 137 | + } |
| 138 | + if !strings.HasPrefix(pattern, "*") { |
| 139 | + return nil, xerrors.Errorf("hostname pattern must only contain an asterisk at the beginning: %q", pattern) |
| 140 | + } |
| 141 | + for i, label := range strings.Split(pattern, ".") { |
| 142 | + if i == 0 { |
| 143 | + // We have to allow the asterisk to be a valid hostname label. |
| 144 | + label = strings.TrimPrefix(label, "*") |
| 145 | + label = "a" + label |
| 146 | + } |
| 147 | + if !validHostnameLabelRegex.MatchString(label) { |
| 148 | + return nil, xerrors.Errorf("hostname pattern contains invalid label %q: %q", label, pattern) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Replace periods with escaped periods. |
| 153 | + regexPattern := strings.ReplaceAll(pattern, ".", "\\.") |
| 154 | + |
| 155 | + // Capture wildcard match. |
| 156 | + regexPattern = strings.Replace(regexPattern, "*", "([^.]+)", 1) |
| 157 | + |
| 158 | + // Allow trailing period. |
| 159 | + regexPattern = regexPattern + "\\.?" |
| 160 | + |
| 161 | + // Allow optional port number. |
| 162 | + regexPattern += "(:\\d+)?" |
| 163 | + |
| 164 | + // Allow leading and trailing whitespace. |
| 165 | + regexPattern = `^\s*` + regexPattern + `\s*$` |
| 166 | + |
| 167 | + return regexp.Compile(regexPattern) |
| 168 | +} |
| 169 | + |
| 170 | +// ExecuteHostnamePattern executes a pattern generated by CompileHostnamePattern |
| 171 | +// and returns the wildcard match. If the pattern does not match the hostname, |
| 172 | +// returns false. |
| 173 | +func ExecuteHostnamePattern(pattern *regexp.Regexp, hostname string) (string, bool) { |
| 174 | + matches := pattern.FindStringSubmatch(hostname) |
| 175 | + if len(matches) < 2 { |
| 176 | + return "", false |
| 177 | + } |
| 178 | + |
| 179 | + return matches[1], true |
| 180 | +} |
0 commit comments