github.com/pdecat/terraform@v0.11.9-beta1/svchost/disco/host.go (about)

     1  package disco
     2  
     3  import (
     4  	"net/url"
     5  )
     6  
     7  type Host struct {
     8  	discoURL *url.URL
     9  	services map[string]interface{}
    10  }
    11  
    12  // ServiceURL returns the URL associated with the given service identifier,
    13  // which should be of the form "servicename.vN".
    14  //
    15  // A non-nil result is always an absolute URL with a scheme of either https
    16  // or http.
    17  //
    18  // If the requested service is not supported by the host, this method returns
    19  // a nil URL.
    20  //
    21  // If the discovery document entry for the given service is invalid (not a URL),
    22  // it is treated as absent, also returning a nil URL.
    23  func (h Host) ServiceURL(id string) *url.URL {
    24  	if h.services == nil {
    25  		return nil // no services supported for an empty Host
    26  	}
    27  
    28  	urlStr, ok := h.services[id].(string)
    29  	if !ok {
    30  		return nil
    31  	}
    32  
    33  	ret, err := url.Parse(urlStr)
    34  	if err != nil {
    35  		return nil
    36  	}
    37  	if !ret.IsAbs() {
    38  		ret = h.discoURL.ResolveReference(ret) // make absolute using our discovery doc URL
    39  	}
    40  	if ret.Scheme != "https" && ret.Scheme != "http" {
    41  		return nil
    42  	}
    43  	if ret.User != nil {
    44  		// embedded username/password information is not permitted; credentials
    45  		// are handled out of band.
    46  		return nil
    47  	}
    48  	ret.Fragment = "" // fragment part is irrelevant, since we're not a browser
    49  
    50  	return h.discoURL.ResolveReference(ret)
    51  }