trimURL returns the host[:port] if the input is a URL, otherwise returns an empty string (and 'isURL:false'). If the URL is valid and no port is specified, the default port determined by the URL prefix is used. Examples: trimURL("https://smallstep.com/onboarding") -> "smallstep.com:443", true, nil
(ref string)
| 77 | // trimURL("hTtPs://sMaLlStEp.cOm") -> "sMaLlStEp.cOm:443", true, nil |
| 78 | // trimURL("hTtPs://sMaLlStEp.cOm hello") -> "", false, err{"invalid url"} |
| 79 | func trimURL(ref string) (string, bool, error) { |
| 80 | tmp := strings.ToLower(ref) |
| 81 | for prefix := range urlPrefixes { |
| 82 | if strings.HasPrefix(tmp, prefix) { |
| 83 | u, err := url.Parse(ref) |
| 84 | if err != nil { |
| 85 | return "", false, errors.Wrapf(err, "error parsing URL '%s'", ref) |
| 86 | } |
| 87 | if _, _, err := net.SplitHostPort(u.Host); err != nil { |
| 88 | port := strconv.FormatUint(uint64(urlPrefixes[prefix]), 10) |
| 89 | u.Host = net.JoinHostPort(u.Host, port) |
| 90 | } |
| 91 | return u.Host, true, nil |
| 92 | } |
| 93 | } |
| 94 | return "", false, nil |
| 95 | } |
no outgoing calls
searching dependent graphs…