febe-sonda legge un HAR troncato senza perdere la parte buona
Chrome ha troncato due esportazioni del giro A quater (a 200 000 byte e a 57 MB, a meta di un corpo di risposta) e la sonda le rifiutava come file sbagliati. Ora da-har e richieste tengono le voci complete, scartano quella dentro cui il file finisce e lo dicono; un file rotto in qualunque altro modo resta rifiutato. Per richieste la voce scartata conta comunque come dati: si chiude dove si ferma e passa per le stesse regole di una voce intera, e ogni stringa JSON del testo interrotto conta come valore, cosi un nome che e un dato solo li non arriva nel file delle forme. In A quater: come esportare senza che il file si tronchi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d2f85693c
commit
78aa32bdb2
8 files changed
+831
-41
No files matched your search
@@ -12,7 +12,14 @@ namespace Febe.Sonda;
|
||||
/// <param name="SiteRequests">Requests the page made to the site (XHR, Fetch) that were described.</param>
|
||||
/// <param name="Shapes">Distinct shapes written: same method, path pattern, operation and query identifier.</param>
|
||||
/// <param name="RemovedNames">Names (parameters, fields, keys) replaced because they could be data.</param>
|
||||
public sealed record RequestShapesReport(int Entries, int SiteRequests, int Shapes, int RemovedNames);
|
||||
public sealed record RequestShapesReport(int Entries, int SiteRequests, int Shapes, int RemovedNames)
|
||||
{
|
||||
/// <summary>The file ends before its JSON does: only its complete entries are described (<see cref="HarFile"/>).</summary>
|
||||
public bool Truncated { get; init; }
|
||||
|
||||
/// <summary>The file ends inside an entry, which is not described (its values still count as data).</summary>
|
||||
public bool DroppedEntry { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Describes the SHAPE of the requests Instagram's own page makes to the site (decision of 12 November 2026, issue #34:
|
||||
@@ -126,31 +133,29 @@ public static partial class RequestShapes
|
||||
throw new InvalidOperationException($"Il file «{output}» esiste già: indica un file nuovo.");
|
||||
}
|
||||
|
||||
JsonDocument document;
|
||||
try
|
||||
{
|
||||
using var stream = File.OpenRead(har);
|
||||
document = JsonDocument.Parse(stream, DeepDocuments);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"{Path.GetFileName(har)}: non è un file HAR valido (riga {ex.LineNumber + 1}).", ex);
|
||||
}
|
||||
|
||||
var shapes = new Dictionary<string, Shape>(StringComparer.Ordinal);
|
||||
int count = 0, described = 0;
|
||||
Names names;
|
||||
using (document)
|
||||
bool truncated, droppedEntry;
|
||||
using (var file = HarFile.Open(har, DeepDocuments.MaxDepth))
|
||||
{
|
||||
if (document.RootElement.ValueKind != JsonValueKind.Object
|
||||
|| !document.RootElement.TryGetProperty("log", out var log) || log.ValueKind != JsonValueKind.Object
|
||||
|| !log.TryGetProperty("entries", out var entries) || entries.ValueKind != JsonValueKind.Array)
|
||||
var entries = file.Entries;
|
||||
truncated = file.Truncated;
|
||||
|
||||
// First pass: every value the HAR holds, wherever it is, the entry a cut file ends inside included. A name
|
||||
// equal to one of them is never written.
|
||||
var values = ValueCollector.Collect(entries);
|
||||
droppedEntry = file.DroppedAnEntry;
|
||||
if (file.ReadDroppedEntry() is { } dropped)
|
||||
{
|
||||
throw new InvalidDataException($"{Path.GetFileName(har)}: non è un file HAR (manca «log.entries»).");
|
||||
using (dropped)
|
||||
{
|
||||
values.UnionWith(ValueCollector.Collect(dropped.Document.RootElement));
|
||||
ValueCollector.AddCutText(values, dropped.CutText);
|
||||
}
|
||||
}
|
||||
|
||||
// First pass: every value the HAR holds, wherever it is. A name equal to one of them is never written.
|
||||
names = new Names(ValueCollector.Collect(entries));
|
||||
names = new Names(values);
|
||||
foreach (var entry in entries.EnumerateArray())
|
||||
{
|
||||
count++;
|
||||
@@ -191,7 +196,7 @@ public static partial class RequestShapes
|
||||
writer.Write('\n');
|
||||
}
|
||||
|
||||
return new RequestShapesReport(count, described, shapes.Count, names.Removed);
|
||||
return new RequestShapesReport(count, described, shapes.Count, names.Removed) { Truncated = truncated, DroppedEntry = droppedEntry };
|
||||
}
|
||||
|
||||
/// <summary>The path with each segment that is not a known word replaced by <c>{numero}</c> or <c>{testo}</c>.</summary>
|
||||
@@ -705,7 +710,7 @@ public static partial class RequestShapes
|
||||
/// Every value a HAR holds (addresses, cookies, header values, form values, strings of JSON requests and responses,
|
||||
/// data embedded in pages), to tell a name of the site's code from a piece of data used as a name.
|
||||
/// </summary>
|
||||
internal static class ValueCollector
|
||||
internal static partial class ValueCollector
|
||||
{
|
||||
private const int MaxBodyChars = 64 * 1024 * 1024;
|
||||
|
||||
@@ -780,6 +785,63 @@ internal static class ValueCollector
|
||||
return values;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The text a cut HAR ends inside (usually a response body, possibly base64 or a page): besides what
|
||||
/// <see cref="Collect"/> already took from the closed entry, every JSON string literal in it counts as a value, keys
|
||||
/// included, so that names inside a body or an embedded block cut in two are found too. More values only remove
|
||||
/// more names.
|
||||
/// </summary>
|
||||
public static void AddCutText(HashSet<string> values, string? text, bool decoded = false)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Body(values, text);
|
||||
foreach (Match literal in StringLiteral().Matches(text))
|
||||
{
|
||||
string? value;
|
||||
try
|
||||
{
|
||||
value = JsonSerializer.Deserialize<string>(literal.Value);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
value = literal.Groups[1].Value;
|
||||
}
|
||||
|
||||
Add(values, value);
|
||||
if (value is not null)
|
||||
{
|
||||
Address(values, value);
|
||||
}
|
||||
}
|
||||
|
||||
// A base64 body cut anywhere: its first whole groups of four characters still decode.
|
||||
if (!decoded && text.Length >= 8 && Base64Text().IsMatch(text))
|
||||
{
|
||||
var length = text.Length / 4 * 4;
|
||||
try
|
||||
{
|
||||
var bytes = Convert.FromBase64String(text[..length]);
|
||||
AddCutText(values, Encoding.UTF8.GetString(bytes), decoded: true);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Not base64 after all.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[GeneratedRegex("""
|
||||
"((?:[^"\\\r\n]|\\.){1,512})"
|
||||
""", RegexOptions.CultureInvariant)]
|
||||
private static partial Regex StringLiteral();
|
||||
|
||||
[GeneratedRegex("^[A-Za-z0-9+/]+=*$", RegexOptions.CultureInvariant)]
|
||||
private static partial Regex Base64Text();
|
||||
|
||||
private static void Add(HashSet<string> values, string? value)
|
||||
{
|
||||
var text = value?.Trim();
|
||||
|
||||
Reference in new issue
Block a user