shouldBypassProxy function
Check if a URL should bypass the proxy based on NO_PROXY.
Supports:
- Exact hostname matches (e.g., "localhost")
- Domain suffix matches with leading dot (e.g., ".example.com")
- Wildcard "*" to bypass all
- Port-specific matches (e.g., "example.com:8080")
- IP addresses (e.g., "127.0.0.1")
Implementation
bool shouldBypassProxy(String urlString, [String? noProxy]) {
noProxy ??= getNoProxy();
if (noProxy == null || noProxy.isEmpty) return false;
// Handle wildcard
if (noProxy == '*') return true;
try {
final uri = Uri.parse(urlString);
final hostname = uri.host.toLowerCase();
final port = uri.port != 0
? '${uri.port}'
: (uri.scheme == 'https' || uri.scheme == 'wss' ? '443' : '80');
final hostWithPort = '$hostname:$port';
// Split by comma or space and trim
final noProxyList = noProxy
.split(RegExp(r'[,\s]+'))
.where((s) => s.isNotEmpty)
.toList();
return noProxyList.any((pattern) {
final p = pattern.toLowerCase().trim();
// Port-specific match
if (p.contains(':')) {
return hostWithPort == p;
}
// Domain suffix match
if (p.startsWith('.')) {
return hostname == p.substring(1) || hostname.endsWith(p);
}
// Exact hostname match
return hostname == p;
});
} catch (_) {
return false;
}
}