Files
Auto-GPT/frontend/lib/utils/uri_utility.dart
2023-09-27 15:46:05 -07:00

48 lines
1.3 KiB
Dart

class UriUtility {
static bool isURL(String url) {
// Validate if the URL string is empty, or contains spaces or invalid characters
if (url.isEmpty || RegExp(r'[\s<>]').hasMatch(url)) {
print('URL is either empty or contains spaces/invalid characters.');
return false;
}
// Check for 'mailto:' at the start of the URL
if (url.startsWith('mailto:')) {
print('URL starts with "mailto:".');
return false;
}
// Try to parse the URL, return false if parsing fails
Uri? uri;
try {
uri = Uri.parse(url);
} catch (e) {
print('URL parsing failed: $e');
return false;
}
// Validate the URL has a scheme (protocol) and host
if (uri.scheme.isEmpty || uri.host.isEmpty) {
print('URL is missing a scheme (protocol) or host.');
return false;
}
// Check if the URI has user info, which is not a common case for a valid HTTP/HTTPS URL
if (uri.hasAuthority &&
uri.userInfo.contains(':') &&
uri.userInfo.split(':').length > 2) {
print('URL contains invalid user info.');
return false;
}
// Validate the port number if exists
if (uri.hasPort && (uri.port <= 0 || uri.port > 65535)) {
print('URL contains an invalid port number.');
return false;
}
print('URL is valid.');
return true;
}
}