diff --git a/v2/pkg/input/input.go b/v2/pkg/input/input.go index 772e550d7..14029f940 100644 --- a/v2/pkg/input/input.go +++ b/v2/pkg/input/input.go @@ -65,17 +65,22 @@ const ( // convertInputToType converts an input based on an inputType. // Various formats are supported for inputs and their transformation func (h *Helper) convertInputToType(input string, inputType inputType, defaultPort string) string { - notURL := !strings.Contains(input, "://") - parsed, _ := url.Parse(input) + isURL := strings.Contains(input, "://") + uri, _ := url.Parse(input) + var host, port string - if !notURL && parsed != nil { - host, port, _ = net.SplitHostPort(parsed.Host) + if isURL && uri != nil { + host, port, _ = net.SplitHostPort(uri.Host) } else { host, port, _ = net.SplitHostPort(input) } - hasPort := port != "" - if inputType == typeFilepath { + hasHost := host != "" + hasPort := port != "" + hasDefaultPort := defaultPort != "" + + switch inputType { + case typeFilepath: // if it has ports most likely it's not a file if hasPort { return "" @@ -86,20 +91,20 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo if absPath, _ := filepath.Abs(input); absPath != "" && fileOrFolderExists(absPath) { return input } - if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && notURL { + if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && !isURL { return input } - } else if inputType == typeHostOnly { - if host != "" { + case typeHostOnly: + if hasHost { return host } - if !notURL { - return parsed.Hostname() + if isURL { + return uri.Hostname() } else { return input } - } else if inputType == typeURL { - if parsed != nil && (parsed.Scheme == "http" || parsed.Scheme == "https") { + case typeURL: + if uri != nil && (uri.Scheme == "http" || uri.Scheme == "https") { return input } if h.InputsHTTP != nil { @@ -107,29 +112,18 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo return string(probed) } } - } else if inputType == typeHostWithPort { - if host != "" && port != "" { + case typeHostWithPort, typeHostWithOptionalPort: + if hasHost && hasPort { return net.JoinHostPort(host, port) } - if parsed != nil && port == "" && parsed.Scheme == "https" { - return net.JoinHostPort(parsed.Host, "443") + if uri != nil && !hasPort && uri.Scheme == "https" { + return net.JoinHostPort(uri.Host, "443") } - if defaultPort != "" { + if hasDefaultPort { return net.JoinHostPort(input, defaultPort) } - } else if inputType == typeHostWithOptionalPort { - if host != "" && port != "" { - return net.JoinHostPort(host, port) - } - if parsed != nil && port == "" && parsed.Scheme == "https" { - return net.JoinHostPort(parsed.Host, "443") - } - if defaultPort != "" { - return net.JoinHostPort(input, defaultPort) - } - return input - } else if inputType == typeWebsocket { - if parsed != nil && (parsed.Scheme == "ws" || parsed.Scheme == "wss") { + case typeWebsocket: + if uri != nil && (uri.Scheme == "ws" || uri.Scheme == "wss") { return input } } diff --git a/v2/pkg/input/input_test.go b/v2/pkg/input/input_test.go index 6a145326b..849c4cd85 100644 --- a/v2/pkg/input/input_test.go +++ b/v2/pkg/input/input_test.go @@ -50,6 +50,13 @@ func TestConvertInputToType(t *testing.T) { // host-port with default port {"google.com", typeHostWithPort, "google.com:443", "443"}, + // host with optional port + {"google.com", typeHostWithOptionalPort, "", ""}, + {"google.com:8443", typeHostWithOptionalPort, "google.com:8443", ""}, + {"https://google.com", typeHostWithOptionalPort, "google.com:443", ""}, + {"https://google.com:443", typeHostWithPort, "google.com:443", ""}, + {"https://google.com:8443", typeHostWithPort, "google.com:8443", ""}, + // websocket {"google.com", typeWebsocket, "", ""}, {"google.com:443", typeWebsocket, "", ""},