...

Source file src/github.com/docker/docker-credential-helpers/registryurl/parse.go

Documentation: github.com/docker/docker-credential-helpers/registryurl

     1  package registryurl
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  	"strings"
     7  )
     8  
     9  // Parse parses and validates a given serverURL to an url.URL, and
    10  // returns an error if validation failed. Querystring parameters are
    11  // omitted in the resulting URL, because they are not used in the helper.
    12  //
    13  // If serverURL does not have a valid scheme, `//` is used as scheme
    14  // before parsing. This prevents the hostname being used as path,
    15  // and the credentials being stored without host.
    16  func Parse(registryURL string) (*url.URL, error) {
    17  	// Check if registryURL has a scheme, otherwise add `//` as scheme.
    18  	if !strings.Contains(registryURL, "://") && !strings.HasPrefix(registryURL, "//") {
    19  		registryURL = "//" + registryURL
    20  	}
    21  
    22  	u, err := url.Parse(registryURL)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	if u.Scheme != "" && u.Scheme != "https" && u.Scheme != "http" {
    28  		return nil, errors.New("unsupported scheme: " + u.Scheme)
    29  	}
    30  
    31  	if u.Hostname() == "" {
    32  		return nil, errors.New("no hostname in URL")
    33  	}
    34  
    35  	u.RawQuery = ""
    36  	return u, nil
    37  }
    38  
    39  // GetHostname returns the hostname of the URL
    40  //
    41  // Deprecated: use url.Hostname()
    42  func GetHostname(u *url.URL) string {
    43  	return u.Hostname()
    44  }
    45  
    46  // GetPort returns the port number of the URL
    47  //
    48  // Deprecated: use url.Port()
    49  func GetPort(u *url.URL) string {
    50  	return u.Port()
    51  }
    52  

View as plain text