github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/url.go (about)

     1  package internal
     2  
     3  import "net/url"
     4  
     5  // SafeURL removes sensitive information from a URL.
     6  func SafeURL(u *url.URL) string {
     7  	if nil == u {
     8  		return ""
     9  	}
    10  	if "" != u.Opaque {
    11  		// If the URL is opaque, we cannot be sure if it contains
    12  		// sensitive information.
    13  		return ""
    14  	}
    15  
    16  	// Omit user, query, and fragment information for security.
    17  	ur := url.URL{
    18  		Scheme: u.Scheme,
    19  		Host:   u.Host,
    20  		Path:   u.Path,
    21  	}
    22  	return ur.String()
    23  }
    24  
    25  // SafeURLFromString removes sensitive information from a URL.
    26  func SafeURLFromString(rawurl string) string {
    27  	u, err := url.Parse(rawurl)
    28  	if nil != err {
    29  		return ""
    30  	}
    31  	return SafeURL(u)
    32  }
    33  
    34  // HostFromURL returns the URL's host.
    35  func HostFromURL(u *url.URL) string {
    36  	if nil == u {
    37  		return ""
    38  	}
    39  	if "" != u.Opaque {
    40  		return "opaque"
    41  	}
    42  	return u.Host
    43  }