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>
208 lines
8.8 KiB
C#
208 lines
8.8 KiB
C#
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>
|
|
/// <param name="rootFieldsPresent">
|
|
/// Whether the body is JSON with every field of <see cref="SiteOperation.RootFields"/>: true or false when the site
|
|
/// client looked (it does for every answer), null when nobody did (<see cref="SiteAnswerSignals"/> then looks itself).
|
|
/// </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,
|
|
bool? rootFieldsPresent = 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;
|
|
RootFieldsPresent = rootFieldsPresent;
|
|
}
|
|
|
|
/// <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 body is JSON with every expected field of the operation, false when it is not JSON or misses one
|
|
/// (§6.0: stop and warn), null when it was not looked at.
|
|
/// </summary>
|
|
public bool? RootFieldsPresent { 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];
|
|
}
|
|
}
|