namespace Febe.Core.Collection; /// An address reduced to what may be kept: scheme, host and path. Never query string or fragment. /// The sanitized address, e.g. «https://www.example.org/a/b/». /// Scheme in lower case («https», «about», «data»...). /// Host in lower case, with the port when it is not the default one; empty when there is none. /// Path without query and fragment; empty for opaque addresses (data:, blob:). public sealed record SanitizedUrl(string Url, string Scheme, string Host, string Path) { public override string ToString() => Url; } /// Strips query strings, fragments and credentials from addresses (spec §7.6, §13.2). public static class UrlSanitizer { public static SanitizedUrl Sanitize(string? url) { if (string.IsNullOrWhiteSpace(url)) { return new SanitizedUrl(string.Empty, string.Empty, string.Empty, string.Empty); } if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) { // Not an absolute address: keep nothing that could carry data. return new SanitizedUrl("(indirizzo non valido)", string.Empty, string.Empty, string.Empty); } var scheme = uri.Scheme.ToLowerInvariant(); if (scheme is "http" or "https" or "ws" or "wss") { var host = HostWithPort(uri); var path = string.IsNullOrEmpty(uri.AbsolutePath) ? "/" : uri.AbsolutePath; return new SanitizedUrl($"{scheme}://{host}{path}", scheme, host, path); } if (scheme == "about") { // about:blank, about:srcdoc: no data inside. var opaque = uri.AbsolutePath.ToLowerInvariant(); return new SanitizedUrl($"about:{opaque}", scheme, string.Empty, opaque); } // data:, blob:, chrome:, file: ... only the scheme survives. return new SanitizedUrl($"{scheme}:", scheme, string.Empty, string.Empty); } internal static string HostWithPort(Uri uri) { var host = uri.IdnHost.ToLowerInvariant(); if (uri.HostNameType == UriHostNameType.IPv6 && !host.StartsWith('[')) { host = $"[{host}]"; } return uri.IsDefaultPort ? host : $"{host}:{uri.Port}"; } } /// /// Tells site, CDN and other hosts apart. Entries are host names that also match their sub-domains /// («instagram.com» matches «www.instagram.com»), or exact «host:port» pairs (used by the local test site). /// public sealed class HostClassifier { private readonly string[] _site; private readonly string[] _cdn; public HostClassifier(IEnumerable siteHosts, IEnumerable cdnHosts) { _site = Normalize(siteHosts); _cdn = Normalize(cdnHosts); } /// /// Instagram's public host names: the site and the two CDN families its pages load files from. In the HAR of /// 21 October 2026 the data came from www.instagram.com (logging from graph.instagram.com), the video files from /// scontent-*.cdninstagram.com and the scripts from static.cdninstagram.com; fbcdn.net is public knowledge. /// public static HostClassifier Instagram { get; } = new( ["instagram.com"], ["cdninstagram.com", "fbcdn.net"]); public IReadOnlyList SiteHosts => _site; public IReadOnlyList CdnHosts => _cdn; public HostKind Classify(string? url) => Uri.TryCreate(url, UriKind.Absolute, out var uri) ? Classify(uri) : HostKind.Other; public HostKind Classify(Uri uri) { ArgumentNullException.ThrowIfNull(uri); if (uri.Scheme is not ("http" or "https")) { return HostKind.Other; } var host = uri.IdnHost.ToLowerInvariant(); var hostWithPort = UrlSanitizer.HostWithPort(uri); var explicitPort = $"{host}:{uri.Port}"; // CDN first: a CDN entry is more specific than a site entry when both could match. if (Matches(_cdn, host, hostWithPort, explicitPort)) { return HostKind.Cdn; } return Matches(_site, host, hostWithPort, explicitPort) ? HostKind.Site : HostKind.Other; } private static bool Matches(string[] entries, string host, string hostWithPort, string explicitPort) { foreach (var entry in entries) { if (entry.Contains(':', StringComparison.Ordinal)) { if (entry == hostWithPort || entry == explicitPort) { return true; } } else if (host == entry || host.EndsWith("." + entry, StringComparison.Ordinal)) { return true; } } return false; } private static string[] Normalize(IEnumerable hosts) { ArgumentNullException.ThrowIfNull(hosts); return [.. hosts.Select(h => h.Trim().TrimStart('.').ToLowerInvariant()).Where(h => h.Length > 0).Distinct()]; } }