forked from hashicorp/terraform
/
host.go
51 lines (45 loc) · 1.23 KB
/
host.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
40
41
42
43
44
45
46
47
48
49
50
51
package disco
import (
"net/url"
)
type Host struct {
discoURL *url.URL
services map[string]interface{}
}
// ServiceURL returns the URL associated with the given service identifier,
// which should be of the form "servicename.vN".
//
// A non-nil result is always an absolute URL with a scheme of either https
// or http.
//
// If the requested service is not supported by the host, this method returns
// a nil URL.
//
// If the discovery document entry for the given service is invalid (not a URL),
// it is treated as absent, also returning a nil URL.
func (h Host) ServiceURL(id string) *url.URL {
if h.services == nil {
return nil // no services supported for an empty Host
}
urlStr, ok := h.services[id].(string)
if !ok {
return nil
}
ret, err := url.Parse(urlStr)
if err != nil {
return nil
}
if !ret.IsAbs() {
ret = h.discoURL.ResolveReference(ret) // make absolute using our discovery doc URL
}
if ret.Scheme != "https" && ret.Scheme != "http" {
return nil
}
if ret.User != nil {
// embedded username/password information is not permitted; credentials
// are handled out of band.
return nil
}
ret.Fragment = "" // fragment part is irrelevant, since we're not a browser
return h.discoURL.ResolveReference(ret)
}