parseHostname will attempt to convert a user provided URL string into a string with some light error checking on certain expectations from the URL. Will convert all HTTP URLs to HTTPS
(input string)
| 46 | // certain expectations from the URL. |
| 47 | // Will convert all HTTP URLs to HTTPS |
| 48 | func parseURL(input string) (*url.URL, error) { |
| 49 | if input == "" { |
| 50 | return nil, errors.New("no input provided") |
| 51 | } |
| 52 | if !strings.HasPrefix(input, "https://") && !strings.HasPrefix(input, "http://") { |
| 53 | input = fmt.Sprintf("https://%s", input) |
| 54 | } |
| 55 | input = bracketBareIPv6(input) |
| 56 | url, err := url.ParseRequestURI(input) |
| 57 | if err != nil { |
| 58 | return nil, fmt.Errorf("failed to parse as URL: %w", err) |
| 59 | } |
| 60 | if url.Scheme != "https" { |
| 61 | url.Scheme = "https" |
| 62 | } |
| 63 | if url.Host == "" { |
| 64 | return nil, errors.New("failed to parse Host") |
| 65 | } |
| 66 | host, err := httpguts.PunycodeHostPort(url.Host) |
| 67 | if err != nil || host == "" { |
| 68 | return nil, err |
| 69 | } |
| 70 | if !httpguts.ValidHostHeader(host) { |
| 71 | return nil, errors.New("invalid Host provided") |
| 72 | } |
| 73 | url.Host = host |
| 74 | return url, nil |
| 75 | } |