Giunto per le richieste dirette, senza cambiare comportamento (#36, S1)
Le risposte catturate dal browser e quelle che il client del sito chiederà devono arrivare ai lettori e ai segnali nello stesso modo: la consegna passa in ResponseDelivery (Core), che ResponseTap usa con gli stessi conteggi e messaggi. Arrivano i contratti SiteRequest, SiteAnswer e ISiteRequester con SiteRequestGuard (un permesso concesso, della categoria giusta, per una sola richiesta) e RequestCatalog, che legge richieste.<sito>.json e tiene nel codice i divieti: nomi con Mutation, chat, telemetria, /ajax/, /ws/, percorsi con segmenti strani. Nessun doc_id nel repository; nessuna operazione di Instagram è ancora configurata perché le forme della sonda non dicono da dove vengano alcuni campi (tappa S4). ScriptedRequester risponde dai campioni ripuliti per le prove. Nulla di nuovo è collegato al motore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d4675a5e21
commit
76dd1b6032
15 files changed
+3488
-68
No files matched your search
@@ -0,0 +1,195 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Febe.Core.Collection.Privacy;
|
||||
|
||||
namespace Febe.Core.Collection.Requests;
|
||||
|
||||
/// <summary>
|
||||
/// What the site answered to one <see cref="SiteRequest"/> (issue #36, spec §6.0), with what the stop signals of §7.6
|
||||
/// need: the status, the declared type, whether it was a redirect and where to (the shape of the path only, never
|
||||
/// followed), and the names of the cookies the site set or deleted. The body lives in memory only, for the time of the
|
||||
/// reading (§6.2).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// It never holds a value of a header or of a cookie: the site client keeps the renewed cookies and the
|
||||
/// <c>x-ig-www-claim</c> in the saved session itself (decision of 16 November 2026) and reports here only that they
|
||||
/// came. Its text (<see cref="ToString"/>) shows the operation, the shape of the path, the status, the type and the
|
||||
/// sizes, never the body, the cookies, a query string or the variables (rule 10).
|
||||
/// </remarks>
|
||||
public sealed class SiteAnswer
|
||||
{
|
||||
/// <param name="operation">
|
||||
/// The catalog operation that was asked: its name and its <see cref="SiteOperation.PathShape"/> are what the answer
|
||||
/// shows, so the path with its values (an identifier, a user name) can never be passed in its place.
|
||||
/// </param>
|
||||
/// <param name="status">HTTP status.</param>
|
||||
/// <param name="mimeType">Declared type (parameters such as the charset are dropped).</param>
|
||||
/// <param name="body">The body, decoded (memory only).</param>
|
||||
/// <param name="redirectTo">For a redirect, the <c>Location</c> as the site sent it: only the shape of its path is kept.</param>
|
||||
/// <param name="cookiesSet">Names of the cookies the site set (<c>Set-Cookie</c> with a value).</param>
|
||||
/// <param name="cookiesDeleted">Names of the cookies the site deleted (<c>Set-Cookie</c> expired or empty).</param>
|
||||
/// <param name="setsClaim">True when the site sent a new <c>x-ig-www-claim</c> (<c>x-ig-set-www-claim</c>).</param>
|
||||
/// <param name="retryAfter">What a <c>Retry-After</c> asked to wait, when the site sent one.</param>
|
||||
/// <exception cref="ArgumentException">A cookie name that is not a plain name (a value must never slip in).</exception>
|
||||
public SiteAnswer(
|
||||
SiteOperation operation,
|
||||
int status,
|
||||
string? mimeType,
|
||||
ReadOnlyMemory<byte> body,
|
||||
string? redirectTo = null,
|
||||
IEnumerable<string>? cookiesSet = null,
|
||||
IEnumerable<string>? cookiesDeleted = null,
|
||||
bool setsClaim = false,
|
||||
TimeSpan? retryAfter = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(operation);
|
||||
Operation = operation.Name;
|
||||
PathShape = operation.PathShape;
|
||||
Status = status;
|
||||
MimeType = (mimeType ?? string.Empty).Split(';')[0].Trim().ToLowerInvariant();
|
||||
Body = body;
|
||||
IsRedirect = status is >= 300 and < 400;
|
||||
RedirectPathShape = IsRedirect || redirectTo is not null ? ShapeOfLocation(redirectTo) : null;
|
||||
CookiesSet = Names(cookiesSet, nameof(cookiesSet));
|
||||
CookiesDeleted = Names(cookiesDeleted, nameof(cookiesDeleted));
|
||||
SetsClaim = setsClaim;
|
||||
RetryAfter = retryAfter;
|
||||
}
|
||||
|
||||
/// <summary>Name of the catalog operation that was asked.</summary>
|
||||
public string Operation { get; }
|
||||
|
||||
/// <summary>Shape of the path asked (<c>/api/v1/media/{numero}/info/</c>): what the log may show.</summary>
|
||||
public string PathShape { get; }
|
||||
|
||||
public int Status { get; }
|
||||
|
||||
/// <summary>Declared type, lower case, without parameters.</summary>
|
||||
public string MimeType { get; }
|
||||
|
||||
/// <summary>The body (memory only, never logged).</summary>
|
||||
public ReadOnlyMemory<byte> Body { get; }
|
||||
|
||||
/// <summary>True for a 3xx: a signal (§7.6), never an address to visit.</summary>
|
||||
public bool IsRedirect { get; }
|
||||
|
||||
/// <summary>
|
||||
/// For a redirect, the shape of the path it pointed to (<c>/accounts/login/</c>, <c>/challenge/{testo}/</c>):
|
||||
/// words of the site kept, everything else <c>{numero}</c> or <c>{testo}</c>, no host, no query string. Empty when
|
||||
/// the site sent no <c>Location</c>.
|
||||
/// </summary>
|
||||
public string? RedirectPathShape { get; }
|
||||
|
||||
/// <summary>Names of the cookies the site set.</summary>
|
||||
public IReadOnlyList<string> CookiesSet { get; }
|
||||
|
||||
/// <summary>Names of the cookies the site deleted (§7.6: the session cookie deleted is a Disconnessione).</summary>
|
||||
public IReadOnlyList<string> CookiesDeleted { get; }
|
||||
|
||||
/// <summary>True when the site sent a new <c>x-ig-www-claim</c> (kept by the site client in the saved session, never here).</summary>
|
||||
public bool SetsClaim { get; }
|
||||
|
||||
/// <summary>What a <c>Retry-After</c> asked to wait (§7.6, Rallentamento).</summary>
|
||||
public TimeSpan? RetryAfter { get; }
|
||||
|
||||
/// <summary>True when the declared type is JSON, or JavaScript as Instagram serves its GraphQL data (<see cref="DataResponses.IsData"/>).</summary>
|
||||
public bool IsDeclaredData => DataResponses.IsData(MimeType, "Fetch");
|
||||
|
||||
/// <summary>True for an HTML document (§7.6: an HTML where the catalog expects JSON stops the session).</summary>
|
||||
public bool IsHtml => MimeType is "text/html" or "application/xhtml+xml";
|
||||
|
||||
/// <summary>True when the site deleted the cookie <paramref name="name"/>.</summary>
|
||||
public bool Deletes(string name) => CookiesDeleted.Contains(name, StringComparer.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// The shape of the path of a <c>Location</c> (absolute or relative): no scheme, no host, no query string, values
|
||||
/// replaced by <see cref="PathPatterns"/>. Empty for a missing one.
|
||||
/// </summary>
|
||||
public static string ShapeOfLocation(string? location)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(location))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var text = location.Trim();
|
||||
var cut = text.IndexOfAny(['?', '#']);
|
||||
if (cut >= 0)
|
||||
{
|
||||
text = text[..cut];
|
||||
}
|
||||
|
||||
var scheme = text.IndexOf("://", StringComparison.Ordinal);
|
||||
if (scheme >= 0)
|
||||
{
|
||||
var slash = text.IndexOf('/', scheme + 3);
|
||||
text = slash >= 0 ? text[slash..] : "/";
|
||||
}
|
||||
else if (text.StartsWith("//", StringComparison.Ordinal))
|
||||
{
|
||||
var slash = text.IndexOf('/', 2);
|
||||
text = slash >= 0 ? text[slash..] : "/";
|
||||
}
|
||||
|
||||
if (!text.StartsWith('/'))
|
||||
{
|
||||
text = "/" + text;
|
||||
}
|
||||
|
||||
return PathPatterns.Of(text);
|
||||
}
|
||||
|
||||
/// <summary>Operation, path shape, status, type and sizes: never the body, the cookies or a value.</summary>
|
||||
public override string ToString()
|
||||
{
|
||||
var text = new StringBuilder();
|
||||
text.Append(CultureInfo.InvariantCulture, $"Risposta a «{Operation}» ({PathShape}): stato {Status}");
|
||||
text.Append(MimeType.Length == 0 ? ", senza tipo" : $", {MimeType}");
|
||||
text.Append(CultureInfo.InvariantCulture, $", {Body.Length} byte");
|
||||
if (IsRedirect)
|
||||
{
|
||||
text.Append(RedirectPathShape is { Length: > 0 } shape ? $", rinvio a {shape} (non seguito)" : ", rinvio senza indirizzo (non seguito)");
|
||||
}
|
||||
|
||||
if (CookiesSet.Count > 0 || CookiesDeleted.Count > 0)
|
||||
{
|
||||
text.Append(CultureInfo.InvariantCulture, $", cookie impostati {CookiesSet.Count}, cancellati {CookiesDeleted.Count}");
|
||||
}
|
||||
|
||||
if (SetsClaim)
|
||||
{
|
||||
text.Append(", nuovo claim");
|
||||
}
|
||||
|
||||
if (RetryAfter is { } wait)
|
||||
{
|
||||
text.Append(CultureInfo.InvariantCulture, $", attendere {(long)wait.TotalSeconds} s");
|
||||
}
|
||||
|
||||
return text.Append('.').ToString();
|
||||
}
|
||||
|
||||
private static string[] Names(IEnumerable<string>? names, string parameter)
|
||||
{
|
||||
if (names is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var list = new List<string>();
|
||||
foreach (var name in names)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name) || name.Any(c => c is '=' or ';' or ',' or '"' || char.IsWhiteSpace(c) || char.IsControl(c)))
|
||||
{
|
||||
throw new ArgumentException("Un nome di cookie non è un nome semplice: qui vanno solo i nomi, mai i valori.", parameter);
|
||||
}
|
||||
|
||||
if (!list.Contains(name, StringComparer.Ordinal))
|
||||
{
|
||||
list.Add(name);
|
||||
}
|
||||
}
|
||||
|
||||
return [.. list];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user