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>
121 lines
6.0 KiB
C#
121 lines
6.0 KiB
C#
using System.Collections.Frozen;
|
|
using Febe.Core.Collection.Privacy;
|
|
|
|
namespace Febe.Core.Collection.Requests;
|
|
|
|
/// <summary>
|
|
/// Names of the values of the case that a <see cref="SiteRequest"/> may carry (spec §6.0: «variabili con i soli valori
|
|
/// del caso»): who or what is asked for, and where a list goes on. A closed list: everything else a request carries
|
|
/// comes from the catalog (<see cref="RequestCatalog"/>) or from the saved session, never from the caller.
|
|
/// </summary>
|
|
public static class CaseValues
|
|
{
|
|
/// <summary>Numeric identifier of an account on the site.</summary>
|
|
public const string AccountId = "account_id";
|
|
|
|
/// <summary>User name of an account.</summary>
|
|
public const string Username = "username";
|
|
|
|
/// <summary>Cursor of the next page, as the previous answer gave it.</summary>
|
|
public const string Cursor = "cursor";
|
|
|
|
/// <summary>Text typed in the search box.</summary>
|
|
public const string SearchText = "search_text";
|
|
|
|
/// <summary>Numeric identifier of a content (post, reel, story).</summary>
|
|
public const string MediaId = "media_id";
|
|
|
|
/// <summary>Short code of a post, as in its address.</summary>
|
|
public const string Code = "code";
|
|
|
|
/// <summary>Identifier of a highlight (collection of stories).</summary>
|
|
public const string HighlightId = "highlight_id";
|
|
|
|
/// <summary>Every name of the list.</summary>
|
|
public static IReadOnlySet<string> All { get; } = new[] { AccountId, Username, Cursor, SearchText, MediaId, Code, HighlightId }
|
|
.ToFrozenSet(StringComparer.Ordinal);
|
|
|
|
/// <summary>The shape a value takes in a logged path (<see cref="PathPatterns"/>): never the value itself.</summary>
|
|
public static string ShapeOf(string name) => name is AccountId or MediaId ? PathPatterns.Number : PathPatterns.Text;
|
|
|
|
/// <summary>
|
|
/// True when <paramref name="value"/> has the form of the value <paramref name="name"/>: digits for identifiers,
|
|
/// the characters of a user name, a short code or a highlight; for a cursor or a searched text, no control
|
|
/// character. So a value can never carry a path, a query or a step up into the request it fills.
|
|
/// </summary>
|
|
public static bool HasForm(string name, string value)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(name);
|
|
ArgumentNullException.ThrowIfNull(value);
|
|
return name switch
|
|
{
|
|
AccountId or MediaId => value.Length <= 32 && value.Length > 0 && value.All(char.IsAsciiDigit),
|
|
Username => value.Length <= 64 && value.Trim('.').Length > 0 && value.All(c => char.IsAsciiLetterOrDigit(c) || c is '.' or '_'),
|
|
Code => value.Length is > 0 and <= 64 && value.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-'),
|
|
HighlightId => value.Length is > 0 and <= 64 && value.All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-' or ':'),
|
|
Cursor or SearchText => value.Length is > 0 and <= 4096 && !value.Any(char.IsControl),
|
|
_ => false,
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// One request to the site (issue #36, spec §6.0): which operation of the catalog, and the values of the case by name.
|
|
/// Nothing else: method, path, headers, body and constants come from <c>richieste.<sito>.json</c>, the session
|
|
/// values from the saved session. Callers never pass raw headers, bodies or addresses.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Its text (<see cref="ToString"/>) names the operation and the names of the values, never the values: user names,
|
|
/// identifiers, cursors and searched texts stay out of logs (rule 10).
|
|
/// </remarks>
|
|
public sealed class SiteRequest
|
|
{
|
|
/// <param name="operation">Name of the catalog operation (<see cref="SiteOperation.Name"/>).</param>
|
|
/// <param name="values">Values of the case, by a name of <see cref="CaseValues"/>.</param>
|
|
/// <exception cref="ArgumentException">An unknown value name, an empty value, or one without its form (<see cref="CaseValues.HasForm"/>).</exception>
|
|
public SiteRequest(string operation, IReadOnlyDictionary<string, string>? values = null)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(operation);
|
|
var copy = new Dictionary<string, string>(StringComparer.Ordinal);
|
|
foreach (var (name, value) in values ?? new Dictionary<string, string>())
|
|
{
|
|
if (!CaseValues.All.Contains(name))
|
|
{
|
|
throw new ArgumentException($"Valore «{name}» sconosciuto: una richiesta porta solo {string.Join(", ", CaseValues.All.Order(StringComparer.Ordinal))}.", nameof(values));
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
throw new ArgumentException($"Il valore «{name}» è vuoto.", nameof(values));
|
|
}
|
|
|
|
if (!CaseValues.HasForm(name, value))
|
|
{
|
|
// Never the value in the message: it may be a user name or a searched text (rule 10).
|
|
throw new ArgumentException($"Il valore «{name}» non ha la forma attesa (cifre per gli identificativi; mai un percorso o una query).", nameof(values));
|
|
}
|
|
|
|
copy[name] = value;
|
|
}
|
|
|
|
Operation = operation.Trim();
|
|
Values = copy.ToFrozenDictionary(StringComparer.Ordinal);
|
|
}
|
|
|
|
/// <summary>Name of the catalog operation.</summary>
|
|
public string Operation { get; }
|
|
|
|
/// <summary>Values of the case, by name (never logged).</summary>
|
|
public IReadOnlyDictionary<string, string> Values { get; }
|
|
|
|
/// <summary>A request for <paramref name="operation"/> with the given values.</summary>
|
|
public static SiteRequest For(string operation, params (string Name, string Value)[] values) =>
|
|
new(operation, values.ToDictionary(v => v.Name, v => v.Value, StringComparer.Ordinal));
|
|
|
|
/// <summary>The operation and the names of the values, never the values.</summary>
|
|
public override string ToString() =>
|
|
Values.Count == 0
|
|
? $"Richiesta «{Operation}»"
|
|
: $"Richiesta «{Operation}» (valori: {string.Join(", ", Values.Keys.Order(StringComparer.Ordinal))})";
|
|
}
|