isURL function
check if the string is a URL
options
is a Map
which defaults to
{ 'protocols': ['http','https','ftp'], 'require_tld': true, 'require_protocol': false, 'allow_underscores': false }
.
Implementation
bool isURL(String? input, [Map<String, Object>? options]) {
var str = input;
if (str == null ||
str.isEmpty ||
str.length > 2083 ||
str.indexOf('mailto:') == 0) {
return false;
}
final defaultUrlOptions = {
'protocols': ['http', 'https', 'ftp'],
'require_tld': true,
'require_protocol': false,
'allow_underscores': false,
};
options = merge(options, defaultUrlOptions);
// String? protocol;
// String user;
// String pass;
// // String auth;
// String host;
// String hostname;
// String port;
// String portStr;
// String path;
// String query;
// String hash;
// List<String> split;
// check protocol
var split = str.split('://');
if (split.length > 1) {
final protocol = shift(split);
final protocols = options['protocols'] as List<String>;
if (!protocols.contains(protocol)) {
return false;
}
} else if (options['require_protocol'] == true) {
return false;
}
str = split.join('://');
// check hash
split = str.split('#');
str = shift(split);
final hash = split.join('#');
if (hash.isNotEmpty && RegExp(r'\s').hasMatch(hash)) {
return false;
}
// check query params
split = str?.split('?') ?? [];
str = shift(split);
final query = split.join('?');
if (query != "" && RegExp(r'\s').hasMatch(query)) {
return false;
}
// check path
split = str?.split('/') ?? [];
str = shift(split);
final path = split.join('/');
if (path != "" && RegExp(r'\s').hasMatch(path)) {
return false;
}
// check auth type urls
split = str?.split('@') ?? [];
if (split.length > 1) {
final auth = shift(split);
if (auth != null && auth.contains(':')) {
// final auth = auth.split(':');
final parts = auth.split(':');
final user = shift(parts);
if (user == null || !RegExp(r'^\S+$').hasMatch(user)) {
return false;
}
final pass = parts.join(':');
if (!RegExp(r'^\S*$').hasMatch(pass)) {
return false;
}
}
}
// check hostname
final hostname = split.join('@');
split = hostname.split(':');
final host = shift(split);
if (split.isNotEmpty) {
final portStr = split.join(':');
final port = int.tryParse(portStr, radix: 10);
if (!RegExp(r'^[0-9]+$').hasMatch(portStr) ||
port == null ||
port <= 0 ||
port > 65535) {
return false;
}
}
if (host == null ||
!isIP(host) && !isFQDN(host, options) && host != 'localhost') {
return false;
}
return true;
}