-
Notifications
You must be signed in to change notification settings - Fork 22
/
url.go
39 lines (32 loc) · 911 Bytes
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package uaa
import (
"fmt"
"net/url"
"path"
"strings"
)
// BuildTargetURL returns a URL. If the target does not include a scheme, https
/// will be used.
func BuildTargetURL(target string) (*url.URL, error) {
if !strings.Contains(target, "://") {
target = fmt.Sprintf("https://%s", target)
}
return url.Parse(target)
}
// BuildSubdomainURL returns a URL that optionally includes the zone ID as a host
// prefix. If the target does not include a scheme, https will be used.
func BuildSubdomainURL(target string, zoneID string) (*url.URL, error) {
url, err := BuildTargetURL(target)
if err != nil {
return nil, err
}
if !strings.HasPrefix(url.Hostname(), zoneID) {
url.Host = fmt.Sprintf("%s.%s", zoneID, url.Host)
}
return url, nil
}
// urlWithPath copies the URL and sets the path on the copy.
func urlWithPath(u url.URL, p string) url.URL {
u.Path = path.Join(u.Path, p)
return u
}