sanitizeUrl static method

UrlDetails? sanitizeUrl(
  1. String? url
)

Parse and sanitize url data for sentry.io

Implementation

static UrlDetails? sanitizeUrl(String? url) {
  if (url == null) {
    return null;
  }

  final queryIndex = url.indexOf('?');
  final fragmentIndex = url.indexOf('#');

  if (queryIndex > -1 && fragmentIndex > -1 && fragmentIndex < queryIndex) {
    // url considered malformed because of fragment position
    return UrlDetails();
  } else {
    try {
      final uri = Uri.parse(url);
      final urlWithRedactedAuth = uri._urlWithRedactedAuth();
      return UrlDetails(
          url: urlWithRedactedAuth.isEmpty ? null : urlWithRedactedAuth,
          query: uri.query.isEmpty ? null : uri.query,
          fragment: uri.fragment.isEmpty ? null : uri.fragment);
    } catch (_) {
      return null;
    }
  }
}