Strato browser (M0/M2): client CDP con elenco dei comandi ammessi, ChromiumProcessHost e Xvfb sorvegliati, gesti umani, mappa dei bersagli, cattura delle risposte, segnali di arresto, scaricamento dal browser, vista remota, distribuzioni del ritmo; prove unitarie e d'insieme con Chromium su Xvfb; esito locale di S5 e S6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
44 files changed
+8396
No files matched your search
@@ -0,0 +1,136 @@
|
||||
namespace Febe.Core.Collection;
|
||||
|
||||
/// <summary>An address reduced to what may be kept: scheme, host and path. Never query string or fragment.</summary>
|
||||
/// <param name="Url">The sanitized address, e.g. «https://www.example.org/a/b/».</param>
|
||||
/// <param name="Scheme">Scheme in lower case («https», «about», «data»...).</param>
|
||||
/// <param name="Host">Host in lower case, with the port when it is not the default one; empty when there is none.</param>
|
||||
/// <param name="Path">Path without query and fragment; empty for opaque addresses (data:, blob:).</param>
|
||||
public sealed record SanitizedUrl(string Url, string Scheme, string Host, string Path)
|
||||
{
|
||||
public override string ToString() => Url;
|
||||
}
|
||||
|
||||
/// <summary>Strips query strings, fragments and credentials from addresses (spec §7.6, §13.2).</summary>
|
||||
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}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
public sealed class HostClassifier
|
||||
{
|
||||
private readonly string[] _site;
|
||||
private readonly string[] _cdn;
|
||||
|
||||
public HostClassifier(IEnumerable<string> siteHosts, IEnumerable<string> cdnHosts)
|
||||
{
|
||||
_site = Normalize(siteHosts);
|
||||
_cdn = Normalize(cdnHosts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instagram's public host names: the site and the two CDN families its pages load files from.
|
||||
/// Public knowledge, not response formats; still to be confirmed by the M0 probe.
|
||||
/// </summary>
|
||||
public static HostClassifier Instagram { get; } = new(
|
||||
["instagram.com"],
|
||||
["cdninstagram.com", "fbcdn.net"]);
|
||||
|
||||
public IReadOnlyList<string> SiteHosts => _site;
|
||||
|
||||
public IReadOnlyList<string> 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<string> hosts)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(hosts);
|
||||
return [.. hosts.Select(h => h.Trim().TrimStart('.').ToLowerInvariant()).Where(h => h.Length > 0).Distinct()];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user