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>
389 lines
15 KiB
C#
389 lines
15 KiB
C#
using System.Buffers;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
namespace Febe.Sonda;
|
|
|
|
/// <summary>
|
|
/// A HAR exported from the browser's DevTools, read whole into one <see cref="JsonDocument"/>, with its
|
|
/// <c>log.entries</c>. An export cut short (Chrome may stop writing a large HAR in the middle of a response body) is
|
|
/// still read: the entries that are complete are kept, the one the file ends inside is left out of
|
|
/// <see cref="Entries"/>, and <see cref="Truncated"/> says so. A file broken in any other way is refused, as before.
|
|
/// </summary>
|
|
internal sealed class HarFile : IDisposable
|
|
{
|
|
private static readonly byte[] Utf8Bom = [0xEF, 0xBB, 0xBF];
|
|
|
|
private static readonly byte[] Prefix = Encoding.UTF8.GetBytes("{\"log\":{\"entries\":[");
|
|
|
|
private static readonly byte[] Suffix = Encoding.UTF8.GetBytes("]}}");
|
|
|
|
private readonly ReadOnlyMemory<byte> _dropped;
|
|
|
|
private readonly int _maxDepth;
|
|
|
|
private HarFile(JsonDocument document, JsonElement entries, bool truncated, ReadOnlyMemory<byte> dropped, int maxDepth)
|
|
{
|
|
Document = document;
|
|
Entries = entries;
|
|
Truncated = truncated;
|
|
_dropped = dropped;
|
|
_maxDepth = maxDepth;
|
|
}
|
|
|
|
public JsonDocument Document { get; }
|
|
|
|
/// <summary>The array <c>log.entries</c>: in a cut file, its complete entries only.</summary>
|
|
public JsonElement Entries { get; }
|
|
|
|
/// <summary>The file ends before its JSON does.</summary>
|
|
public bool Truncated { get; }
|
|
|
|
/// <summary>The file ends inside an entry, which is not in <see cref="Entries"/> (false if it ends between entries or after them).</summary>
|
|
public bool DroppedAnEntry => !_dropped.IsEmpty;
|
|
|
|
/// <exception cref="InvalidDataException">The file is not JSON, or not a HAR, or is broken other than by being cut short.</exception>
|
|
public static HarFile Open(string path, int maxDepth)
|
|
{
|
|
ReadOnlyMemory<byte> json = File.ReadAllBytes(path);
|
|
if (json.Span.StartsWith(Utf8Bom))
|
|
{
|
|
json = json[Utf8Bom.Length..];
|
|
}
|
|
|
|
var name = Path.GetFileName(path);
|
|
JsonDocument document;
|
|
var truncated = false;
|
|
var dropped = ReadOnlyMemory<byte>.Empty;
|
|
try
|
|
{
|
|
document = JsonDocument.Parse(json, new JsonDocumentOptions { MaxDepth = maxDepth });
|
|
}
|
|
catch (JsonException ex)
|
|
{
|
|
if (Recover(json, maxDepth) is not { } recovered)
|
|
{
|
|
throw new InvalidDataException($"{name}: non è un file HAR valido (riga {ex.LineNumber + 1}).", ex);
|
|
}
|
|
|
|
(document, dropped) = recovered;
|
|
truncated = true;
|
|
}
|
|
|
|
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)
|
|
{
|
|
document.Dispose();
|
|
throw new InvalidDataException($"{name}: non è un file HAR (manca «log.entries»).");
|
|
}
|
|
|
|
return new HarFile(document, entries, truncated, dropped, maxDepth);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The entry the file ends inside, closed where it stops (<see cref="CloseCut"/>), so that <c>richieste</c> can
|
|
/// collect its values with the same rules as a whole entry; null if no entry was cut. Never written anywhere.
|
|
/// </summary>
|
|
public DroppedEntry? ReadDroppedEntry() => _dropped.IsEmpty ? null : CloseCut(_dropped.Span, _maxDepth);
|
|
|
|
public void Dispose() => Document.Dispose();
|
|
|
|
/// <summary>
|
|
/// The complete entries of a HAR whose file ends early, as a new document <c>{"log":{"entries":[…]}}</c>, and the
|
|
/// bytes of the entry the file ends inside (empty if it ends between entries or after them); null when the file is
|
|
/// broken in any other way (a syntax error, a shape that is not a HAR, no complete entry).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The reader is told the data is NOT final: where the file simply stops, <see cref="Utf8JsonReader.Read"/> and
|
|
/// <see cref="Utf8JsonReader.TrySkip"/> return false instead of throwing, while a real syntax error still throws.
|
|
/// </remarks>
|
|
internal static (JsonDocument Document, ReadOnlyMemory<byte> Dropped)? Recover(ReadOnlyMemory<byte> json, int maxDepth)
|
|
{
|
|
var options = new JsonReaderOptions { MaxDepth = maxDepth };
|
|
var ranges = new List<(int Start, int End)>();
|
|
var droppedFrom = -1;
|
|
try
|
|
{
|
|
var reader = new Utf8JsonReader(json.Span, isFinalBlock: false, new JsonReaderState(options));
|
|
if (!reader.Read() || reader.TokenType != JsonTokenType.StartObject
|
|
|| !MoveToProperty(ref reader, "log") || !reader.Read() || reader.TokenType != JsonTokenType.StartObject
|
|
|| !MoveToProperty(ref reader, "entries") || !reader.Read() || reader.TokenType != JsonTokenType.StartArray)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
if (!reader.Read())
|
|
{
|
|
break; // the file ends between two entries
|
|
}
|
|
|
|
if (reader.TokenType == JsonTokenType.EndArray)
|
|
{
|
|
// Every entry is whole: the file must end later, after the array, and not because of a syntax error.
|
|
while (reader.Read())
|
|
{
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if (reader.TokenType != JsonTokenType.StartObject)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var start = (int)reader.TokenStartIndex;
|
|
if (!reader.TrySkip())
|
|
{
|
|
droppedFrom = start; // the file ends inside this entry
|
|
break;
|
|
}
|
|
|
|
ranges.Add((start, (int)reader.BytesConsumed));
|
|
}
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (ranges.Count == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var length = Prefix.Length + Suffix.Length + ranges.Sum(r => r.End - r.Start + 1);
|
|
var buffer = new byte[length];
|
|
var at = 0;
|
|
Prefix.CopyTo(buffer, at);
|
|
at += Prefix.Length;
|
|
for (var i = 0; i < ranges.Count; i++)
|
|
{
|
|
if (i > 0)
|
|
{
|
|
buffer[at++] = (byte)',';
|
|
}
|
|
|
|
var (start, end) = ranges[i];
|
|
json.Span[start..end].CopyTo(buffer.AsSpan(at));
|
|
at += end - start;
|
|
}
|
|
|
|
Suffix.CopyTo(buffer, at);
|
|
at += Suffix.Length;
|
|
var document = JsonDocument.Parse(buffer.AsMemory(0, at), new JsonDocumentOptions { MaxDepth = maxDepth });
|
|
return (document, droppedFrom < 0 ? ReadOnlyMemory<byte>.Empty : json[droppedFrom..]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Closes a JSON object that stops early: every token read whole is kept; a string value the data ends inside is
|
|
/// kept up to the cut (it is <see cref="DroppedEntry.CutText"/> too, usually the response body Chrome stopped
|
|
/// writing); a name left without its value gets null; then the open objects and arrays are closed.
|
|
/// </summary>
|
|
internal static DroppedEntry CloseCut(ReadOnlySpan<byte> json, int maxDepth)
|
|
{
|
|
var buffer = new ArrayBufferWriter<byte>();
|
|
var open = new Stack<JsonTokenType>();
|
|
var pendingName = false;
|
|
string? cut = null;
|
|
using (var writer = new Utf8JsonWriter(buffer, new JsonWriterOptions { MaxDepth = maxDepth + 1 }))
|
|
{
|
|
writer.WriteStartArray();
|
|
var reader = new Utf8JsonReader(json, isFinalBlock: false, new JsonReaderState(new JsonReaderOptions { MaxDepth = maxDepth }));
|
|
while (reader.Read())
|
|
{
|
|
switch (reader.TokenType)
|
|
{
|
|
case JsonTokenType.StartObject:
|
|
writer.WriteStartObject();
|
|
open.Push(JsonTokenType.StartObject);
|
|
pendingName = false;
|
|
break;
|
|
case JsonTokenType.StartArray:
|
|
writer.WriteStartArray();
|
|
open.Push(JsonTokenType.StartArray);
|
|
pendingName = false;
|
|
break;
|
|
case JsonTokenType.EndObject:
|
|
writer.WriteEndObject();
|
|
open.Pop();
|
|
break;
|
|
case JsonTokenType.EndArray:
|
|
writer.WriteEndArray();
|
|
open.Pop();
|
|
break;
|
|
case JsonTokenType.PropertyName:
|
|
writer.WritePropertyName(Text(ref reader));
|
|
pendingName = true;
|
|
break;
|
|
case JsonTokenType.String:
|
|
writer.WriteStringValue(Text(ref reader));
|
|
pendingName = false;
|
|
break;
|
|
case JsonTokenType.Number:
|
|
writer.WriteRawValue(reader.ValueSpan, skipInputValidation: true);
|
|
pendingName = false;
|
|
break;
|
|
case JsonTokenType.True:
|
|
case JsonTokenType.False:
|
|
writer.WriteBooleanValue(reader.TokenType == JsonTokenType.True);
|
|
pendingName = false;
|
|
break;
|
|
default:
|
|
writer.WriteNullValue();
|
|
pendingName = false;
|
|
break;
|
|
}
|
|
|
|
if (open.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
// What the reader could not finish: separators, then perhaps the start of a string.
|
|
var rest = json[(int)reader.BytesConsumed..];
|
|
var skip = 0;
|
|
while (skip < rest.Length && rest[skip] is (byte)' ' or (byte)'\t' or (byte)'\r' or (byte)'\n' or (byte)',' or (byte)':')
|
|
{
|
|
skip++;
|
|
}
|
|
|
|
rest = rest[skip..];
|
|
var valueExpected = pendingName || (open.Count > 0 && open.Peek() == JsonTokenType.StartArray);
|
|
if (valueExpected && rest.Length > 0 && rest[0] == (byte)'"')
|
|
{
|
|
cut = PartialString(rest[1..]);
|
|
writer.WriteStringValue(cut);
|
|
pendingName = false;
|
|
}
|
|
|
|
if (pendingName)
|
|
{
|
|
writer.WriteNullValue();
|
|
}
|
|
|
|
while (open.Count > 0)
|
|
{
|
|
if (open.Pop() == JsonTokenType.StartObject)
|
|
{
|
|
writer.WriteEndObject();
|
|
}
|
|
else
|
|
{
|
|
writer.WriteEndArray();
|
|
}
|
|
}
|
|
|
|
writer.WriteEndArray();
|
|
}
|
|
|
|
return new DroppedEntry(JsonDocument.Parse(buffer.WrittenMemory, new JsonDocumentOptions { MaxDepth = maxDepth + 1 }), cut);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves the reader to the value of the property <paramref name="name"/> of the object it is in, skipping the others;
|
|
/// false if the object ends, or the file does, first.
|
|
/// </summary>
|
|
private static bool MoveToProperty(ref Utf8JsonReader reader, string name)
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
if (reader.TokenType != JsonTokenType.PropertyName)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (reader.ValueTextEquals(name))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (!reader.Read() || !reader.TrySkip())
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>A string token as text; one that is not valid UTF-8 or UTF-16 is decoded leniently, never refused.</summary>
|
|
private static string Text(ref Utf8JsonReader reader)
|
|
{
|
|
try
|
|
{
|
|
return reader.GetString() ?? string.Empty;
|
|
}
|
|
catch (InvalidOperationException)
|
|
{
|
|
return Encoding.UTF8.GetString(reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The text of a JSON string whose closing quote never came: the bytes after the opening quote, without an escape
|
|
/// or a UTF-8 character the cut left half written, unescaped; decoded leniently if it still does not read.
|
|
/// </summary>
|
|
internal static string PartialString(ReadOnlySpan<byte> raw)
|
|
{
|
|
// A closing quote inside the rest would mean the reader stopped for another reason: keep only up to it.
|
|
var end = 0;
|
|
while (end < raw.Length && raw[end] != (byte)'"')
|
|
{
|
|
end += raw[end] == (byte)'\\' ? 2 : 1;
|
|
}
|
|
|
|
var text = raw[..Math.Min(end, raw.Length)];
|
|
|
|
// An escape cut in two: a lone backslash, or «\u» with fewer than four hexadecimal digits.
|
|
var backslash = text.LastIndexOf((byte)'\\');
|
|
if (backslash >= 0 && text.Length - backslash < 6)
|
|
{
|
|
var run = 0;
|
|
while (backslash - run >= 0 && text[backslash - run] == (byte)'\\')
|
|
{
|
|
run++;
|
|
}
|
|
|
|
var escape = text[backslash..];
|
|
var whole = run % 2 == 0 || (escape.Length >= 2 && escape[1] != (byte)'u') || escape.Length == 6;
|
|
if (!whole)
|
|
{
|
|
text = text[..backslash];
|
|
}
|
|
}
|
|
|
|
for (var trim = 0; trim <= 3 && trim <= text.Length; trim++)
|
|
{
|
|
var candidate = new byte[text.Length - trim + 2];
|
|
candidate[0] = (byte)'"';
|
|
text[..^trim].CopyTo(candidate.AsSpan(1));
|
|
candidate[^1] = (byte)'"';
|
|
try
|
|
{
|
|
var reader = new Utf8JsonReader(candidate);
|
|
if (reader.Read() && reader.TokenType == JsonTokenType.String)
|
|
{
|
|
return reader.GetString() ?? string.Empty;
|
|
}
|
|
}
|
|
catch (Exception ex) when (ex is JsonException or InvalidOperationException)
|
|
{
|
|
// A UTF-8 character cut in two, or bytes that are not UTF-8: one byte less, then lenient decoding.
|
|
}
|
|
}
|
|
|
|
return Encoding.UTF8.GetString(text);
|
|
}
|
|
}
|
|
|
|
/// <summary>The entry a cut HAR ends inside, closed where it stops, as a one-element array; and the text cut short.</summary>
|
|
internal sealed record DroppedEntry(JsonDocument Document, string? CutText) : IDisposable
|
|
{
|
|
public void Dispose() => Document.Dispose();
|
|
}
|