Significant refactor around the input to type conversion for clarity and dedup.

This commit is contained in:
Víctor Zamanillo 2022-12-08 21:00:55 +01:00
parent f4c2212a88
commit 5f02282468
2 changed files with 32 additions and 31 deletions

View File

@ -65,17 +65,22 @@ const (
// convertInputToType converts an input based on an inputType. // convertInputToType converts an input based on an inputType.
// Various formats are supported for inputs and their transformation // Various formats are supported for inputs and their transformation
func (h *Helper) convertInputToType(input string, inputType inputType, defaultPort string) string { func (h *Helper) convertInputToType(input string, inputType inputType, defaultPort string) string {
notURL := !strings.Contains(input, "://") isURL := strings.Contains(input, "://")
parsed, _ := url.Parse(input) uri, _ := url.Parse(input)
var host, port string var host, port string
if !notURL && parsed != nil { if isURL && uri != nil {
host, port, _ = net.SplitHostPort(parsed.Host) host, port, _ = net.SplitHostPort(uri.Host)
} else { } else {
host, port, _ = net.SplitHostPort(input) 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 it has ports most likely it's not a file
if hasPort { if hasPort {
return "" return ""
@ -86,20 +91,20 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
if absPath, _ := filepath.Abs(input); absPath != "" && fileOrFolderExists(absPath) { if absPath, _ := filepath.Abs(input); absPath != "" && fileOrFolderExists(absPath) {
return input return input
} }
if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && notURL { if _, err := filepath.Match(input, ""); err != filepath.ErrBadPattern && !isURL {
return input return input
} }
} else if inputType == typeHostOnly { case typeHostOnly:
if host != "" { if hasHost {
return host return host
} }
if !notURL { if isURL {
return parsed.Hostname() return uri.Hostname()
} else { } else {
return input return input
} }
} else if inputType == typeURL { case typeURL:
if parsed != nil && (parsed.Scheme == "http" || parsed.Scheme == "https") { if uri != nil && (uri.Scheme == "http" || uri.Scheme == "https") {
return input return input
} }
if h.InputsHTTP != nil { if h.InputsHTTP != nil {
@ -107,29 +112,18 @@ func (h *Helper) convertInputToType(input string, inputType inputType, defaultPo
return string(probed) return string(probed)
} }
} }
} else if inputType == typeHostWithPort { case typeHostWithPort, typeHostWithOptionalPort:
if host != "" && port != "" { if hasHost && hasPort {
return net.JoinHostPort(host, port) return net.JoinHostPort(host, port)
} }
if parsed != nil && port == "" && parsed.Scheme == "https" { if uri != nil && !hasPort && uri.Scheme == "https" {
return net.JoinHostPort(parsed.Host, "443") return net.JoinHostPort(uri.Host, "443")
} }
if defaultPort != "" { if hasDefaultPort {
return net.JoinHostPort(input, defaultPort) return net.JoinHostPort(input, defaultPort)
} }
} else if inputType == typeHostWithOptionalPort { case typeWebsocket:
if host != "" && port != "" { if uri != nil && (uri.Scheme == "ws" || uri.Scheme == "wss") {
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") {
return input return input
} }
} }

View File

@ -50,6 +50,13 @@ func TestConvertInputToType(t *testing.T) {
// host-port with default port // host-port with default port
{"google.com", typeHostWithPort, "google.com:443", "443"}, {"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 // websocket
{"google.com", typeWebsocket, "", ""}, {"google.com", typeWebsocket, "", ""},
{"google.com:443", typeWebsocket, "", ""}, {"google.com:443", typeWebsocket, "", ""},