using System.Runtime.CompilerServices; using Febe.Core.Abstractions; namespace Febe.Core.Collection.Requests; /// /// 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 www.instagram.com (rule 3), the test double answers from the cleaned samples. /// /// /// /// Every call needs a the Governor granted for that very request (rule 8, §7.4): a permit per /// request, never one for several. An implementation refuses, with and before /// anything leaves, a permit that is missing, not granted or already presented once (), a /// permit of another category than the operation's (), an operation that is not in /// the catalog, not configured or with a header or field still , and values that /// do not match the operation's placeholders or would change its path. does all of /// these checks. /// 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. /// Only reads: the catalog refuses writes when it loads (). /// Redirects are never followed: a 3xx comes back as a with . /// Logs name only the operation or the shape of the path and the status (rule 10). /// /// Cancellation throws . The answer goes to the readers through /// , like the captured ones. /// public interface ISiteRequester { Task SendAsync(SiteRequest request, Permit permit, CancellationToken ct); } /// /// 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. /// public sealed class SiteRequestRefusedException : InvalidOperationException { public SiteRequestRefusedException() { } public SiteRequestRefusedException(string message) : base(message) { } public SiteRequestRefusedException(string message, Exception innerException) : base(message, innerException) { } } /// /// 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. /// public sealed class SpentPermits { private static readonly object Marker = new(); private readonly ConditionalWeakTable _spent = new(); /// Marks as used; false when it was used before. public bool TrySpend(Permit permit) { ArgumentNullException.ThrowIfNull(permit); return _spent.TryAdd(permit, Marker); } } /// The checks every makes before a request leaves. public static class SiteRequestGuard { /// /// The catalog operation asks for, when it may leave with . A /// granted permit is spent as soon as it is shown, also when the request is then refused: the Governor has already /// counted it. /// /// It must not leave (see ). 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; } }