safeUrlCheck function

Future<bool> safeUrlCheck(
  1. Uri url, {
  2. int maxRedirects = 8,
  3. String userAgent = _defaultUserAgent,
  4. HttpClient? client,
  5. RetryOptions retryOptions = const RetryOptions(maxAttempts: 3),
  6. Duration timeout = const Duration(seconds: 90),
})

Check if url is available, without allowing access to private networks.

This will make a HEAD request to url with given userAgent, following redirects if the hostname of URLs do not resolve to a private IP address.

Each I/O is set to timeout after timeout duration. This coupled with the large number of retries allow means that this operation can be slow.

It is good practice to set a custom userAgent, this allows servers to see which bots are requesting their resources. This package defaults to the following user-agent:

User-Agent: package:safe_url_check/1.0.0 (+https://github.com/google/dart-neats/tree/master/safe_url_check)

Implementation

Future<bool> safeUrlCheck(
  Uri url, {
  int maxRedirects = 8,
  String userAgent = _defaultUserAgent,
  HttpClient? client,
  RetryOptions retryOptions = const RetryOptions(maxAttempts: 3),
  Duration timeout = const Duration(seconds: 90),
}) async {
  ArgumentError.checkNotNull(url, 'url');
  ArgumentError.checkNotNull(maxRedirects, 'maxRedirects');
  ArgumentError.checkNotNull(userAgent, 'userAgent');
  ArgumentError.checkNotNull(retryOptions, 'retryOptions');
  if (maxRedirects < 0) {
    throw ArgumentError.value(
      maxRedirects,
      'maxRedirects',
      'must be a positive integer',
    );
  }

  try {
    // Create client if one wasn't given.
    var c = client;
    c ??= HttpClient();
    try {
      return await _safeUrlCheck(
        url,
        maxRedirects,
        c,
        userAgent,
        retryOptions,
        timeout,
      );
    } finally {
      // Close client, if it was created here.
      if (client == null) {
        c.close(force: true);
      }
    }
  } on Exception {
    return false;
  }
}