|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "golang.org/x/xerrors" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + // Remove the "starts with" and "ends with" regex components. |
| 14 | + nameRegex = strings.Trim(UsernameValidRegex.String(), "^$") |
| 15 | + appURL = regexp.MustCompile(fmt.Sprintf( |
| 16 | + // {PORT/APP_NAME}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME} |
| 17 | + `^(?P<AppName>%[1]s)--(?P<AgentName>%[1]s)--(?P<WorkspaceName>%[1]s)--(?P<Username>%[1]s)$`, |
| 18 | + nameRegex)) |
| 19 | +) |
| 20 | + |
| 21 | +// SplitSubdomain splits a subdomain from the rest of the hostname. E.g.: |
| 22 | +// - "foo.bar.com" becomes "foo", "bar.com" |
| 23 | +// - "foo.bar.baz.com" becomes "foo", "bar.baz.com" |
| 24 | +// |
| 25 | +// An error is returned if the string doesn't contain a period. |
| 26 | +func SplitSubdomain(hostname string) (subdomain string, rest string, err error) { |
| 27 | + toks := strings.SplitN(hostname, ".", 2) |
| 28 | + if len(toks) < 2 { |
| 29 | + return "", "", xerrors.New("no subdomain") |
| 30 | + } |
| 31 | + |
| 32 | + return toks[0], toks[1], nil |
| 33 | +} |
| 34 | + |
| 35 | +// ApplicationURL is a parsed application URL hostname. |
| 36 | +type ApplicationURL struct { |
| 37 | + // Only one of AppName or Port will be set. |
| 38 | + AppName string |
| 39 | + Port uint16 |
| 40 | + AgentName string |
| 41 | + WorkspaceName string |
| 42 | + Username string |
| 43 | + // BaseHostname is the rest of the hostname minus the application URL part |
| 44 | + // and the first dot. |
| 45 | + BaseHostname string |
| 46 | +} |
| 47 | + |
| 48 | +// String returns the application URL hostname without scheme. |
| 49 | +func (a ApplicationURL) String() string { |
| 50 | + appNameOrPort := a.AppName |
| 51 | + if a.Port != 0 { |
| 52 | + appNameOrPort = strconv.Itoa(int(a.Port)) |
| 53 | + } |
| 54 | + |
| 55 | + return fmt.Sprintf("%s--%s--%s--%s.%s", appNameOrPort, a.AgentName, a.WorkspaceName, a.Username, a.BaseHostname) |
| 56 | +} |
| 57 | + |
| 58 | +// ParseSubdomainAppURL parses an ApplicationURL from the given hostname. If |
| 59 | +// the subdomain is not a valid application URL hostname, returns a non-nil |
| 60 | +// error. |
| 61 | +// |
| 62 | +// Subdomains should be in the form: |
| 63 | +// |
| 64 | +// {PORT/APP_NAME}--{AGENT_NAME}--{WORKSPACE_NAME}--{USERNAME} |
| 65 | +// (eg. http://8080--main--dev--dean.hi.c8s.io) |
| 66 | +func ParseSubdomainAppURL(hostname string) (ApplicationURL, error) { |
| 67 | + subdomain, rest, err := SplitSubdomain(hostname) |
| 68 | + if err != nil { |
| 69 | + return ApplicationURL{}, xerrors.Errorf("split host domain %q: %w", hostname, err) |
| 70 | + } |
| 71 | + |
| 72 | + matches := appURL.FindAllStringSubmatch(subdomain, -1) |
| 73 | + if len(matches) == 0 { |
| 74 | + return ApplicationURL{}, xerrors.Errorf("invalid application url format: %q", subdomain) |
| 75 | + } |
| 76 | + matchGroup := matches[0] |
| 77 | + |
| 78 | + appName, port := AppNameOrPort(matchGroup[appURL.SubexpIndex("AppName")]) |
| 79 | + return ApplicationURL{ |
| 80 | + AppName: appName, |
| 81 | + Port: port, |
| 82 | + AgentName: matchGroup[appURL.SubexpIndex("AgentName")], |
| 83 | + WorkspaceName: matchGroup[appURL.SubexpIndex("WorkspaceName")], |
| 84 | + Username: matchGroup[appURL.SubexpIndex("Username")], |
| 85 | + BaseHostname: rest, |
| 86 | + }, nil |
| 87 | +} |
| 88 | + |
| 89 | +// AppNameOrPort takes a string and returns either the input string or a port |
| 90 | +// number. |
| 91 | +func AppNameOrPort(val string) (string, uint16) { |
| 92 | + port, err := strconv.ParseUint(val, 10, 16) |
| 93 | + if err != nil || port == 0 { |
| 94 | + port = 0 |
| 95 | + } else { |
| 96 | + val = "" |
| 97 | + } |
| 98 | + |
| 99 | + return val, uint16(port) |
| 100 | +} |
0 commit comments