url.go 796 B

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