Files
Febe/src/Febe.Core/Collection/Requests/ISiteRequester.cs
T
Alby96andClaude Opus 5.5 76dd1b6032
verifica / controlli (push) Successful in 17s
verifica / prove (push) Successful in 18m22s
verifica / installatore (push) Successful in 4m43s
verifica / immagine (push) Successful in 17m20s
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>
2026-11-16 12:59:32 +01:00

127 lines
5.9 KiB
C#

using System.Runtime.CompilerServices;
using Febe.Core.Abstractions;
namespace Febe.Core.Collection.Requests;
/// <summary>
/// Asks the site for one operation of the catalog (issue #36, spec §6.0) and returns its answer. The only way the
/// engine talks to the site without the browser: the HTTP implementation (tappa S2) is the one class of Febe that
/// contacts <c>www.instagram.com</c> (rule 3), the test double answers from the cleaned samples.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item>Every call needs a <see cref="Permit"/> the Governor granted for that very request (rule 8, §7.4): a permit per
/// request, never one for several. An implementation refuses, with <see cref="SiteRequestRefusedException"/> and before
/// anything leaves, a permit that is missing, not granted or already presented once (<see cref="SpentPermits"/>), a
/// permit of another category than the operation's (<see cref="SiteOperation.Category"/>), an operation that is not in
/// the catalog, not configured or with a header or field still <see cref="ValueSourceKind.Pending"/>, and values that
/// do not match the operation's placeholders or would change its path. <see cref="SiteRequestGuard"/> does all of
/// these checks.</item>
/// <item>One request at a time: an implementation never sends two together and makes a second call wait (or refuse)
/// until the first has its answer; the minimum spacing of §7.7 is the Governor's.</item>
/// <item>Only reads: the catalog refuses writes when it loads (<see cref="RequestCatalog"/>).</item>
/// <item>Redirects are never followed: a 3xx comes back as a <see cref="SiteAnswer"/> with <see cref="SiteAnswer.IsRedirect"/>.</item>
/// <item>Logs name only the operation or the shape of the path and the status (rule 10).</item>
/// </list>
/// Cancellation throws <see cref="OperationCanceledException"/>. The answer goes to the readers through
/// <see cref="ResponseDelivery"/>, like the captured ones.
/// </remarks>
public interface ISiteRequester
{
Task<SiteAnswer> SendAsync(SiteRequest request, Permit permit, CancellationToken ct);
}
/// <summary>
/// A request that must not leave: no granted permit, a permit already used, another category, an operation outside
/// the catalog or not configured, values that do not match. The message is in Italian and names only the operation and
/// value names.
/// </summary>
public sealed class SiteRequestRefusedException : InvalidOperationException
{
public SiteRequestRefusedException()
{
}
public SiteRequestRefusedException(string message)
: base(message)
{
}
public SiteRequestRefusedException(string message, Exception innerException)
: base(message, innerException)
{
}
}
/// <summary>
/// The permits a site client was already shown (rule 8, §7.4: a permit per request). By reference, not by value: two
/// permits the Governor granted one after the other are two, even when their fields are equal. Holds them weakly, so a
/// long run does not keep them alive.
/// </summary>
public sealed class SpentPermits
{
private static readonly object Marker = new();
private readonly ConditionalWeakTable<Permit, object> _spent = new();
/// <summary>Marks <paramref name="permit"/> as used; false when it was used before.</summary>
public bool TrySpend(Permit permit)
{
ArgumentNullException.ThrowIfNull(permit);
return _spent.TryAdd(permit, Marker);
}
}
/// <summary>The checks every <see cref="ISiteRequester"/> makes before a request leaves.</summary>
public static class SiteRequestGuard
{
/// <summary>
/// The catalog operation <paramref name="request"/> asks for, when it may leave with <paramref name="permit"/>. A
/// granted permit is spent as soon as it is shown, also when the request is then refused: the Governor has already
/// counted it.
/// </summary>
/// <exception cref="SiteRequestRefusedException">It must not leave (see <see cref="ISiteRequester"/>).</exception>
public static SiteOperation Check(RequestCatalog catalog, SiteRequest request, Permit? permit, SpentPermits spent)
{
ArgumentNullException.ThrowIfNull(catalog);
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(spent);
if (permit is null || !permit.Granted)
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: il Governatore non ha dato il permesso.");
}
if (!spent.TrySpend(permit))
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: il permesso è già stato usato (un permesso per richiesta).");
}
if (!catalog.TryGet(request.Operation, out var operation))
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: l'operazione non è in {catalog.Source}.");
}
if (!operation.IsConfigured)
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: l'operazione non è configurata ({operation.NotConfiguredReason}).");
}
// The catalog never loads a configured operation with pending sources; checked again so no path relies on it.
if (operation.PendingValues.Count > 0)
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: intestazioni o campi senza una fonte stabilita.");
}
if (permit.Kind != operation.Category)
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: il permesso è per {permit.Kind}, l'operazione conta come {operation.Category}.");
}
if (operation.CheckValues(request) is { } problem)
{
throw new SiteRequestRefusedException($"Richiesta «{request.Operation}» non inviata: {problem}");
}
return operation;
}
}