Skip to content

Commit 4942eab

Browse files
Simplify parsing logic
1 parent 1872755 commit 4942eab

1 file changed

Lines changed: 12 additions & 30 deletions

File tree

  • Plugins/Flow.Launcher.Plugin.Url

Plugins/Flow.Launcher.Plugin.Url/Main.cs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,15 @@ public bool IsURL(string raw)
8383
if (decimal.TryParse(input, out _))
8484
return false;
8585

86-
// Check if it's a bare IP address (without protocol)
87-
var inputHost = Uri.TryCreate(input, UriKind.Absolute, out var tempUri) ? tempUri.Host : input.Split(['/', ':'])[0].Trim('[', ']');
88-
if (IPAddress.TryParse(inputHost, out var ip))
89-
{
90-
// Exclude invalid address 0.0.0.0
91-
if (ip.Equals(IPAddress.Any))
92-
return false;
93-
86+
// Check if it's a bare IP address with optional port and path
87+
var ipPart = input.Split('/')[0]; // Remove path
88+
if (IPEndPoint.TryParse(ipPart, out var endpoint) && !endpoint.Address.Equals(IPAddress.Any))
9489
return true;
95-
}
9690

97-
// Check if it's a bare IPv6 address (contains multiple colons but no protocol)
98-
if (input.Count(c => c == ':') > 1 && !input.Contains("://"))
99-
{
100-
var ipv6Part = input.Split('/')[0].Trim('[', ']');
101-
if (IPAddress.TryParse(ipv6Part, out _))
102-
return true;
103-
}
104-
105-
// Validate using Uri after adding protocol
106-
var urlToValidate = input;
107-
if (!UrlSchemes.Any(s => input.StartsWith(s, StringComparison.OrdinalIgnoreCase)))
108-
{
109-
urlToValidate = GetHttpPreference() + "://" + input;
110-
}
91+
// Add protocol if missing for Uri validation
92+
var urlToValidate = UrlSchemes.Any(s => input.StartsWith(s, StringComparison.OrdinalIgnoreCase))
93+
? input
94+
: GetHttpPreference() + "://" + input;
11195

11296
if (!Uri.TryCreate(urlToValidate, UriKind.Absolute, out var uri))
11397
return false;
@@ -116,24 +100,22 @@ public bool IsURL(string raw)
116100
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps && uri.Scheme != Uri.UriSchemeFtp)
117101
return false;
118102

119-
// Validate host: must contain a dot (domain), be localhost, or be a valid IP
120103
var host = uri.Host;
104+
105+
// localhost is valid
121106
if (host.Equals("localhost", StringComparison.OrdinalIgnoreCase))
122107
return true;
123108

109+
// Valid IP address (excluding 0.0.0.0)
124110
if (IPAddress.TryParse(host, out var hostIp))
125111
return !hostIp.Equals(IPAddress.Any);
126112

127-
// Domain must contain at least one dot, and dot cannot be at the start or end
128-
if (!host.Contains('.'))
129-
return false;
130-
131-
// Ensure valid domain format (not starting or ending with dot, TLD at least 2 characters)
113+
// Domain must have valid format with TLD
132114
var parts = host.Split('.');
133115
if (parts.Length < 2 || parts.Any(string.IsNullOrEmpty))
134116
return false;
135117

136-
// TLD must be at least 2 characters
118+
// TLD must be at least 2 letters
137119
var tld = parts[^1];
138120
return tld.Length >= 2 && tld.All(char.IsLetter);
139121
}

0 commit comments

Comments
 (0)