Skip to content

Lightpanda: the headless browser designed for AI and automation

`Parser`: count the rule lines it drops

Halil Durak48282a930749
diff --git a/src/network/adblock/AdBlocker.zig b/src/network/adblock/AdBlocker.zig
index 960993020..c19946c60 100644
--- a/src/network/adblock/AdBlocker.zig
+++ b/src/network/adblock/AdBlocker.zig
@@ -42,6 +42,8 @@ allowed: u32,
suppressed: u32,
/// Rules that reached a trie, across every list parsed so far.
rules_loaded: usize,
+/// Rules the lists carry and we do not apply, across those same lists: the
+/// ones the parser could not read, plus the ones no trie can express.
rules_skipped: usize,
/// Read buffer for filter lists, and therefore the longest line we can see.
@@ -117,6 +119,10 @@ pub fn parse(self: *AdBlocker, reader: *Io.Reader) !void {
const scratch = scratch_instance.allocator();
var parser: Parser = .init(reader);
+ // Lines the parser dropped are rules we did not load just as much as the
+ // ones we drop below, and on a real list they are the bulk of them.
+ defer self.rules_skipped += parser.skipped;
+
while (try parser.next(scratch)) |filter| {
// Nothing survives the iteration but the trie entry, so the
// scratch arena is recycled rather than grown per filter.
@@ -222,10 +228,11 @@ test "adblock.AdBlocker: parse accumulates across lists" {
// its hostname instead of allowing it.
try testing.expectEqual(.none, blocker.matchHostname("cdn.example.com"));
- // The block and the exception. The cosmetic line never parsed as a
- // filter, so it is not a rule we dropped.
+ // The block and the exception loaded; the cosmetic line is a rule the
+ // list carries and we do not apply, so it counts as skipped even though
+ // it never reached a trie.
try testing.expectEqual(2, blocker.rules_loaded);
- try testing.expectEqual(0, blocker.rules_skipped);
+ try testing.expectEqual(1, blocker.rules_skipped);
var second: Io.Reader = .fixed(
\\||tracker.net^$third-party,domain=news.com|~sports.news.com
@@ -235,7 +242,7 @@ test "adblock.AdBlocker: parse accumulates across lists" {
try testing.expectEqual(.none, blocker.matchHostname("tracker.net"));
// The counts carry across lists, like the entries do.
try testing.expectEqual(2, blocker.rules_loaded);
- try testing.expectEqual(1, blocker.rules_skipped);
+ try testing.expectEqual(2, blocker.rules_skipped);
// The first list's entries survived the second parse.
try testing.expectEqual(.blocked, blocker.matchHostname("ads.example.com"));
}
diff --git a/src/network/adblock/Parser.zig b/src/network/adblock/Parser.zig
index 9cb2b6042..8a1291bba 100644
--- a/src/network/adblock/Parser.zig
+++ b/src/network/adblock/Parser.zig
@@ -29,6 +29,11 @@ const Parser = @This();
reader: *std.Io.Reader,
first_line: bool = true,
+/// Rule-shaped lines that never became a filter: syntax outside our subset,
+/// malformed rules, and lines too long to read. Blank lines and comments are
+/// not rules, so they never count. The caller adds this to whatever it drops
+/// afterwards, so that "skipped" means every rule the list has and we do not.
+skipped: usize = 0,
pub const Error = error{ OutOfMemory, ReadFailed };
@@ -51,7 +56,8 @@ pub fn next(self: *Parser, arena: Allocator) Error!?NetworkFilter {
const line = std.mem.trim(u8, stripped, &std.ascii.whitespace);
switch (LineClass.fromLine(line)) {
- .empty, .comment, .unsupported => {},
+ .empty, .comment => {},
+ .unsupported => self.skipped += 1,
.network => {
if (NetworkFilter.parse(arena, line)) |filter| {
return filter;
@@ -59,7 +65,7 @@ pub fn next(self: *Parser, arena: Allocator) Error!?NetworkFilter {
error.OutOfMemory => return error.OutOfMemory,
// Anything else is a line outside the supported subset
// or malformed; either way it is not ours to enforce.
- else => {},
+ else => self.skipped += 1,
}
},
}
@@ -78,6 +84,9 @@ fn takeLine(self: *Parser) error{ReadFailed}!?[]u8 {
} else |err| switch (err) {
error.ReadFailed => return error.ReadFailed,
error.StreamTooLong => {
+ // Counted without knowing what it was: a line we cannot read
+ // is a line we cannot rule out being a filter.
+ self.skipped += 1;
// takeDelimiter leaves the stream untouched on StreamTooLong,
// so the oversized line still has to be stepped over.
_ = self.reader.discardDelimiterInclusive('\n') catch |e| switch (e) {
@@ -169,6 +178,31 @@ test "adblock.Parser: yields one filter per next() call" {
try testing.expect(try parser.next(arena) == null);
}
+test "adblock.Parser: only rule-shaped lines count as skipped" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ var reader: std.Io.Reader = .fixed(
+ \\[Adblock Plus 2.0]
+ \\! Title: Counted
+ \\
+ \\||ads.example.com^
+ \\example.com##.ad-banner
+ \\||bogus.example.com^$notarealoption
+ \\example.com$$script[data-x]
+ );
+ var parser: Parser = .init(&reader);
+
+ var count: usize = 0;
+ while (try parser.next(arena)) |_| count += 1;
+
+ // The header, the comment and the blank line are not rules.
+ try testing.expectEqual(1, count);
+ // The cosmetic filter, the unknown option and the AdGuard line are.
+ try testing.expectEqual(3, parser.skipped);
+}
+
test "adblock.Parser: a line too long for the buffer is skipped" {
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
defer arena_state.deinit();
@@ -194,6 +228,8 @@ test "adblock.Parser: a line too long for the buffer is skipped" {
try testing.expectString("tracker.net", second.hostname);
try testing.expect(try parser.next(arena) == null);
+ // It is still a line we could not load, and it says so.
+ try testing.expectEqual(1, parser.skipped);
}
test "adblock.Parser: full list" {