Client del sito contro il sito di prova (#36, S2)
verifica / controlli (push) Successful in 17s
verifica / prove (push) Successful in 15m4s
verifica / installatore (push) Successful in 4m22s
verifica / immagine (push) Successful in 15m55s

HttpSiteRequester è l'unica classe che parla col sito: va solo a
https://www.instagram.com/ o a un sito di prova di loopback, non segue i
rinvii, una richiesta per volta col permesso del Governatore, solo le
intestazioni del catalogo con l'user agent della sessione, e registra solo
operazione, forma del percorso e stato. Cookie rinnovati e claim finiscono
nella sessione (ISiteSession, per ora in memoria; la copia cifrata è S4).
SiteAnswerSignals traduce le risposte nei segnali di §7.6: 429 rallenta;
rinvii, 401/403, HTML o campi mancanti valgono come Verifica (H16).
Il sito di prova risponde alle richieste dirette come dice
richieste.prova.json e simula i casi d'arresto; una visita senza sessione
viva apre una sessione nuova anche per il client. Nulla è ancora collegato
al motore (S3).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-16 18:34:12 +01:00
1 parent e10c25d12d
commit ce9691fe1c
19 files changed
+3563 -51

No files matched your search

+192
View File
@@ -24,11 +24,56 @@ public enum StopMode
UnknownFormat,
}
/// <summary>
/// What the site answers to the next request of the site client (<c>/api/…</c>, issue #36) instead of the data: the stop
/// cases of spec §7.6 and the renewals the client must keep. The shapes not known from samples are generic (H16).
/// </summary>
public enum DirectReply
{
/// <summary>The data, after the checks of a real site.</summary>
Normal,
/// <summary>429 with a short JSON and <c>Retry-After: 60</c> (Rallentamento).</summary>
TooManyRequests,
/// <summary>302 to the login page <c>/accesso/</c> (H16-generic).</summary>
RedirectToLogin,
/// <summary>302 to the challenge page <c>/verifica</c> (H16-generic).</summary>
RedirectToChallenge,
/// <summary>200 with an HTML document where JSON is expected (H16-generic).</summary>
HtmlInsteadOfJson,
/// <summary>200 with a JSON that has none of the expected root fields (H16-generic).</summary>
MissingRootField,
/// <summary>The data, with a <c>Set-Cookie</c> that renews the session cookie (new value, the old one stops working).</summary>
RenewSessionCookie,
/// <summary>The data, with a <c>Set-Cookie</c> that deletes the session cookie (the session ends).</summary>
DeleteSessionCookie,
/// <summary>The data, with a new <c>x-ig-set-www-claim</c>.</summary>
SetClaim,
}
/// <summary>Switches and counters of the test site. Thread-safe.</summary>
public sealed class SiteControl
{
private readonly Lock _gate = new();
private readonly ConcurrentQueue<string> _dataRequests = new();
private readonly ConcurrentQueue<string> _directRequests = new();
private readonly ConcurrentQueue<DirectReply> _directReplies = new();
private readonly HashSet<string> _sessions = new(StringComparer.Ordinal) { InitialSession };
private DirectReply _directAlways = DirectReply.Normal;
private int _directInFlight;
private int _maxDirectInFlight;
private int _loginVisits;
private int _challengeVisits;
private int _renewals;
private int _logins;
private int _claims;
private StopMode _mode = StopMode.Normal;
private int _armAfter;
@@ -76,6 +121,140 @@ public sealed class SiteControl
/// </summary>
public int RecentSearchWrites => _recentSearchWrites;
/// <summary>
/// A session already open when the site starts and after <see cref="Reset"/>, as one opened before the test: the
/// tests that build a saved session without the browser use it. The pages never hand it out: a visit without a live
/// session gets a new value (<see cref="OpenSession"/>).
/// </summary>
public const string InitialSession = "attiva";
/// <summary>Method, path and query of every request of the site client (<c>/api/…</c>), in arrival order.</summary>
public IReadOnlyList<string> DirectRequests => [.. _directRequests];
/// <summary>The most requests of the site client the site was answering at the same time.</summary>
public int MaxDirectInFlight => Volatile.Read(ref _maxDirectInFlight);
/// <summary>Extra wait before every answer to the site client (slow answers).</summary>
public TimeSpan DirectDelay { get; set; }
/// <summary>Requests of the login page <c>/accesso…</c> (a redirect followed would land there).</summary>
public int LoginPageVisits => Volatile.Read(ref _loginVisits);
/// <summary>Requests of the challenge page <c>/verifica…</c>.</summary>
public int ChallengePageVisits => Volatile.Read(ref _challengeVisits);
/// <summary>The site client answers to the next request (or, with <paramref name="always"/>, to every one) with <paramref name="reply"/>.</summary>
public void ReplyToDirect(DirectReply reply, bool always = false)
{
if (always)
{
lock (_gate)
{
_directAlways = reply;
}
_directReplies.Clear();
return;
}
_directReplies.Enqueue(reply);
}
/// <summary>True when <paramref name="value"/> is a session cookie the site handed out and did not end.</summary>
public bool IsSession(string? value)
{
lock (_gate)
{
return value is { Length: > 0 } && _sessions.Contains(value);
}
}
/// <summary>A new session, as a new login opens: its value works for the pages and for the site client.</summary>
internal string OpenSession()
{
var opened = "aperta-" + Interlocked.Increment(ref _logins).ToString(System.Globalization.CultureInfo.InvariantCulture);
lock (_gate)
{
_sessions.Add(opened);
}
return opened;
}
/// <summary>A new value of the session cookie in place of <paramref name="old"/>, which stops working.</summary>
internal string RenewSession(string old)
{
var renewed = "rinnovata-" + Interlocked.Increment(ref _renewals).ToString(System.Globalization.CultureInfo.InvariantCulture);
lock (_gate)
{
_sessions.Remove(old);
_sessions.Add(renewed);
}
return renewed;
}
/// <summary>Ends the session <paramref name="value"/>.</summary>
internal void EndSession(string value)
{
lock (_gate)
{
_sessions.Remove(value);
}
}
/// <summary>A new claim value.</summary>
internal string NextClaim() => "claim-prova-" + Interlocked.Increment(ref _claims).ToString(System.Globalization.CultureInfo.InvariantCulture);
/// <summary>
/// Records a request of the site client and returns the reply it gets and the mode of the site that applies to it:
/// a request of the site client counts as a data request for <see cref="Set"/> with <c>afterDataRequests</c>, as a
/// page's <c>/dati/</c> does. Pair with <see cref="OnDirectFinished"/>.
/// </summary>
internal (DirectReply Reply, StopMode Mode) OnDirectRequest(string methodPathAndQuery)
{
_directRequests.Enqueue(methodPathAndQuery);
var now = Interlocked.Increment(ref _directInFlight);
int max;
while (now > (max = Volatile.Read(ref _maxDirectInFlight)) && Interlocked.CompareExchange(ref _maxDirectInFlight, now, max) != max)
{
}
StopMode mode;
DirectReply always;
lock (_gate)
{
if (_armAfter > 0)
{
_armAfter--;
mode = StopMode.Normal;
}
else
{
mode = _mode;
}
always = _directAlways;
}
return (_directReplies.TryDequeue(out var next) ? next : always, mode);
}
internal void OnDirectFinished() => Interlocked.Decrement(ref _directInFlight);
/// <summary>Counts the visits of the login and challenge pages.</summary>
internal void OnPageVisit(string path)
{
if (path.StartsWith("/accesso", StringComparison.Ordinal))
{
Interlocked.Increment(ref _loginVisits);
}
else if (path.StartsWith("/verifica", StringComparison.Ordinal))
{
Interlocked.Increment(ref _challengeVisits);
}
}
public StopMode Mode
{
get
@@ -107,6 +286,19 @@ public sealed class SiteControl
}
_dataRequests.Clear();
_directRequests.Clear();
_directReplies.Clear();
lock (_gate)
{
_directAlways = DirectReply.Normal;
_sessions.Clear();
_sessions.Add(InitialSession);
}
DirectDelay = TimeSpan.Zero;
Interlocked.Exchange(ref _maxDirectInFlight, 0);
Interlocked.Exchange(ref _loginVisits, 0);
Interlocked.Exchange(ref _challengeVisits, 0);
StoryDuration = TimeSpan.FromSeconds(5);
AddressLifetime = TimeSpan.FromHours(6);
CdnTooManyRequests = false;