isURL function

bool isURL(
  1. String? str, {
  2. List<String?> protocols = const ['http', 'https', 'ftp', 'ftps', 'file', 'git'],
  3. bool requireTld = true,
  4. bool requireProtocol = false,
  5. bool allowUnderscore = false,
  6. List<String> hostWhitelist = const [],
  7. List<String> hostBlacklist = const [],
})

check if the string str is a URL

  • protocols sets the list of allowed protocols
  • requireTld sets if TLD is required
  • requireProtocol is a bool that sets if protocol is required for validation
  • allowUnderscore sets if underscores are allowed
  • hostWhitelist sets the list of allowed hosts
  • hostBlacklist sets the list of disallowed hosts

Implementation

bool isURL(String? str,
    /// sets the list of allowed protocols, currently only `http`, `https`, `ftp`, `ftps`, `file` and `git` are supported
    {List<String?> protocols = const ['http', 'https', 'ftp', 'ftps', 'file', 'git'],
    /// sets if TLD is required
    bool requireTld = true,
    /// is a `bool` that sets if protocol is required for validation
    bool requireProtocol = false,
    /// sets if underscores are allowed
    bool allowUnderscore = false,
    /// sets the list of allowed hosts
    List<String> hostWhitelist = const [],
    /// sets the list of disallowed hosts
    List<String> hostBlacklist = const []}) {
  if (str == null || str.length == 0 || str.length > 2083 || str.startsWith('mailto:')) {
    return false;
  }

  var protocol, user, auth, host, hostname, port, port_str, path, query, hash, split;

  // check protocol
  split = str.split('://');
  if (split.length > 1 == true) {
    protocol = shift<String>(split as List<String>);
    if (protocols.indexOf(protocol as String) == -1) {
      return false;
    }
  } else if (requireProtocol == true) {
    return false;
  }
  str = split.join('://') as String?;

  // check hash
  split = str!.split('#');
  str = shift<String>(split);
  hash = split.join('#');
  if (hash != null && hash != '' && RegExp(r'\s').hasMatch(hash as String)) {
    return false;
  }

  // check query params
  split = str!.split('?');
  str = shift<String>(split);
  query = split.join('?');
  if (query != null && query != "" && RegExp(r'\s').hasMatch(query as String)) {
    return false;
  }

  // check path
  split = str!.split('/');
  str = shift(split);
  path = split.join('/');
  if (path != null && path != "" && RegExp(r'\s').hasMatch(path as String)) {
    return false;
  }

  // check auth type urls
  split = str!.split('@');
  if (split.length > 1 == true) {
    auth = shift(split);
    if (auth.indexOf(':') >= 0 == true) {
      auth = auth.split(':');
      user = shift(auth as List);
      if (!RegExp(r'^\S+$').hasMatch(user as String)) {
        return false;
      }
      if (!RegExp(r'^\S*$').hasMatch(user)) {
        return false;
      }
    }
  }

  // check hostname
  hostname = split.join('@');
  split = hostname.split(':');
  host = shift(split as List);
  if (split.length > 0 == true) {
    port_str = split.join(':');
    try {
      port = int.parse(port_str as String, radix: 10);
    } catch (e) {
      return false;
    }
    if (!RegExp(r'^[0-9]+$').hasMatch(port_str) || port as int <= 0 || port > 65535) {
      return false;
    }
  }

  if (!isIP(host as String?) && !isFQDN(host as String, requireTld: requireTld, allowUnderscores: allowUnderscore) && host != 'localhost') {
    return false;
  }

  if (hostWhitelist.isNotEmpty && !hostWhitelist.contains(host)) {
    return false;
  }

  if (hostBlacklist.isNotEmpty && hostBlacklist.contains(host)) {
    return false;
  }

  return true;
}