|
| 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 | +funcSplitSubdomain(hostnamestring) (subdomainstring,reststring,errerror) { |
| 27 | +toks:=strings.SplitN(hostname,".",2) |
| 28 | +iflen(toks)<2 { |
| 29 | +return"","",xerrors.New("no subdomain") |
| 30 | +} |
| 31 | + |
| 32 | +returntoks[0],toks[1],nil |
| 33 | +} |
| 34 | + |
| 35 | +// ApplicationURL is a parsed application URL hostname. |
| 36 | +typeApplicationURLstruct { |
| 37 | +// Only one of AppName or Port will be set. |
| 38 | +AppNamestring |
| 39 | +Portuint16 |
| 40 | +AgentNamestring |
| 41 | +WorkspaceNamestring |
| 42 | +Usernamestring |
| 43 | +// BaseHostname is the rest of the hostname minus the application URL part |
| 44 | +// and the first dot. |
| 45 | +BaseHostnamestring |
| 46 | +} |
| 47 | + |
| 48 | +// String returns the application URL hostname without scheme. |
| 49 | +func (aApplicationURL)String()string { |
| 50 | +appNameOrPort:=a.AppName |
| 51 | +ifa.Port!=0 { |
| 52 | +appNameOrPort=strconv.Itoa(int(a.Port)) |
| 53 | +} |
| 54 | + |
| 55 | +returnfmt.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 | +funcParseSubdomainAppURL(hostnamestring) (ApplicationURL,error) { |
| 67 | +subdomain,rest,err:=SplitSubdomain(hostname) |
| 68 | +iferr!=nil { |
| 69 | +returnApplicationURL{},xerrors.Errorf("split host domain %q: %w",hostname,err) |
| 70 | +} |
| 71 | + |
| 72 | +matches:=appURL.FindAllStringSubmatch(subdomain,-1) |
| 73 | +iflen(matches)==0 { |
| 74 | +returnApplicationURL{},xerrors.Errorf("invalid application url format: %q",subdomain) |
| 75 | +} |
| 76 | +matchGroup:=matches[0] |
| 77 | + |
| 78 | +appName,port:=AppNameOrPort(matchGroup[appURL.SubexpIndex("AppName")]) |
| 79 | +returnApplicationURL{ |
| 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 | +funcAppNameOrPort(valstring) (string,uint16) { |
| 92 | +port,err:=strconv.ParseUint(val,10,16) |
| 93 | +iferr!=nil||port==0 { |
| 94 | +port=0 |
| 95 | +}else { |
| 96 | +val="" |
| 97 | +} |
| 98 | + |
| 99 | +returnval,uint16(port) |
| 100 | +} |