ParseURL parses a URL and returns the scheme, URL without port, and port If the URL doesn't have an explicit port, returns the default port for the scheme
(rawURL string)
| 176 | // ParseURL parses a URL and returns the scheme, URL without port, and port |
| 177 | // If the URL doesn't have an explicit port, returns the default port for the scheme |
| 178 | func ParseURL(rawURL string) (scheme string, urlWithoutPort string, port string) { |
| 179 | if rawURL == "" { |
| 180 | return "", "", "" |
| 181 | } |
| 182 | |
| 183 | parsedURL, err := url.Parse(rawURL) |
| 184 | if err != nil { |
| 185 | return "", "", "" |
| 186 | } |
| 187 | |
| 188 | // Check if the URL has a valid scheme |
| 189 | scheme = parsedURL.Scheme |
| 190 | if scheme != "http" && scheme != "https" { |
| 191 | return "", "", "" |
| 192 | } |
| 193 | |
| 194 | // Get URL without port |
| 195 | urlWithoutPort = fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Hostname()) |
| 196 | |
| 197 | // Get port |
| 198 | if parsedURL.Port() != "" { |
| 199 | port = parsedURL.Port() |
| 200 | } else if parsedURL.Scheme == "https" { |
| 201 | port = "443" |
| 202 | } else { |
| 203 | port = "80" |
| 204 | } |
| 205 | |
| 206 | return scheme, urlWithoutPort, port |
| 207 | } |
| 208 | |
| 209 | // IsEnvTrue returns true if the given environment variable |
| 210 | // has a value accepted by strconv.ParseBool. |
no outgoing calls