using System.Buffers; using System.Text; using System.Text.Json; namespace Febe.Sonda; /// /// A HAR exported from the browser's DevTools, read whole into one , with its /// log.entries. 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 /// , and says so. A file broken in any other way is refused, as before. /// 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 _dropped; private readonly int _maxDepth; private HarFile(JsonDocument document, JsonElement entries, bool truncated, ReadOnlyMemory dropped, int maxDepth) { Document = document; Entries = entries; Truncated = truncated; _dropped = dropped; _maxDepth = maxDepth; } public JsonDocument Document { get; } /// The array log.entries: in a cut file, its complete entries only. public JsonElement Entries { get; } /// The file ends before its JSON does. public bool Truncated { get; } /// The file ends inside an entry, which is not in (false if it ends between entries or after them). public bool DroppedAnEntry => !_dropped.IsEmpty; /// The file is not JSON, or not a HAR, or is broken other than by being cut short. public static HarFile Open(string path, int maxDepth) { ReadOnlyMemory 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.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); } /// /// The entry the file ends inside, closed where it stops (), so that richieste can /// collect its values with the same rules as a whole entry; null if no entry was cut. Never written anywhere. /// public DroppedEntry? ReadDroppedEntry() => _dropped.IsEmpty ? null : CloseCut(_dropped.Span, _maxDepth); public void Dispose() => Document.Dispose(); /// /// The complete entries of a HAR whose file ends early, as a new document {"log":{"entries":[…]}}, 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). /// /// /// The reader is told the data is NOT final: where the file simply stops, and /// return false instead of throwing, while a real syntax error still throws. /// internal static (JsonDocument Document, ReadOnlyMemory Dropped)? Recover(ReadOnlyMemory 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.Empty : json[droppedFrom..]); } /// /// 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 too, usually the response body Chrome stopped /// writing); a name left without its value gets null; then the open objects and arrays are closed. /// internal static DroppedEntry CloseCut(ReadOnlySpan json, int maxDepth) { var buffer = new ArrayBufferWriter(); var open = new Stack(); 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); } /// /// Moves the reader to the value of the property of the object it is in, skipping the others; /// false if the object ends, or the file does, first. /// 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; } /// A string token as text; one that is not valid UTF-8 or UTF-16 is decoded leniently, never refused. 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); } } /// /// 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. /// internal static string PartialString(ReadOnlySpan 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); } } /// The entry a cut HAR ends inside, closed where it stops, as a one-element array; and the text cut short. internal sealed record DroppedEntry(JsonDocument Document, string? CutText) : IDisposable { public void Dispose() => Document.Dispose(); }