mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 14:04:27 +01:00
This commit introduces the UriUtility class to the codebase. This utility class contains a static method, isURL, used for validating URLs. The method checks for common URL validation criteria, ensuring that the input string adheres to the standard URL format. It verifies that the URL is non-empty, does not contain invalid characters, is not a mailto link, can be parsed as a URI, and has both a scheme and a host. Additional validations include checks on user info and port numbers. Changes made: - Created UriUtility class with isURL static method for URL validation. - Included validations for empty strings, invalid characters, mailto links, scheme, host, user info, and port numbers. - The utility will be utilized in forms and other areas where URL validation is necessary. This enhancement improves the robustness of URL validation across the application, ensuring that only valid URLs are processed and stored.
41 lines
1.0 KiB
Dart
41 lines
1.0 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)) {
|
|
return false;
|
|
}
|
|
|
|
// Check for 'mailto:' at the start of the URL
|
|
if (url.startsWith('mailto:')) {
|
|
return false;
|
|
}
|
|
|
|
// Try to parse the URL, return false if parsing fails
|
|
Uri? uri;
|
|
try {
|
|
uri = Uri.parse(url);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
|
|
// Validate the URL has a scheme (protocol) and host
|
|
if (uri.scheme.isEmpty || uri.host.isEmpty) {
|
|
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.isEmpty ||
|
|
uri.userInfo.contains(':') && uri.userInfo.split(':').length > 2)) {
|
|
return false;
|
|
}
|
|
|
|
// Validate the port number if exists
|
|
if (uri.hasPort && (uri.port <= 0 || uri.port > 65535)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|