Skip to content

Lightpanda: the headless browser designed for AI and automation

Remove dead EncodeSet

Karl Seguin4890e38b3a14
diff --git a/src/agent/auth/codex.zig b/src/agent/auth/codex.zig
index c69f4f3db..bea7836e1 100644
--- a/src/agent/auth/codex.zig
+++ b/src/agent/auth/codex.zig
@@ -109,15 +109,15 @@ fn parseTokenResponse(allocator: std.mem.Allocator, body: []const u8) !auth.Toke
fn refreshBody(arena: std.mem.Allocator, refresh_token: []const u8) ![]u8 {
return arena.print("grant_type=refresh_token&client_id=" ++ client_id ++ "&refresh_token={s}", .{
- try lp.URL.percentEncodeSegment(arena, refresh_token, .component),
+ try lp.URL.percentEncodeComponent(arena, refresh_token),
});
}
fn exchangeBody(arena: std.mem.Allocator, code: []const u8, code_verifier: []const u8) ![]u8 {
return arena.print("grant_type=authorization_code&client_id=" ++ client_id ++
"&redirect_uri=" ++ device_redirect_uri ++ "&code={s}&code_verifier={s}", .{
- try lp.URL.percentEncodeSegment(arena, code, .component),
- try lp.URL.percentEncodeSegment(arena, code_verifier, .component),
+ try lp.URL.percentEncodeComponent(arena, code),
+ try lp.URL.percentEncodeComponent(arena, code_verifier),
});
}
diff --git a/src/browser/URL.zig b/src/browser/URL.zig
index 182731a9e..c8ab26779 100644
--- a/src/browser/URL.zig
+++ b/src/browser/URL.zig
@@ -69,72 +69,18 @@ pub fn canParse(url: []const u8, maybe_base: ?[]const u8) bool {
return U.url_can_parse(url.ptr, url.len);
}
-const EncodeSet = enum { path, query, query_legacy, userinfo, fragment, component };
-
-pub fn percentEncodeSegment(allocator: Allocator, segment: []const u8, comptime encode_set: EncodeSet) ![]const u8 {
- // Check if encoding is needed
- var needs_encoding = false;
- for (segment) |c| {
- if (shouldPercentEncode(c, encode_set)) {
- needs_encoding = true;
- break;
- }
- }
- if (!needs_encoding) {
- // Always dupe — the signature returns owned bytes, so a caller doing
- // `defer allocator.free(out)` mustn't crash on the no-op path.
- return allocator.dupe(u8, segment);
- }
-
- var buf = try std.ArrayList(u8).initCapacity(allocator, segment.len + 10);
-
- var i: usize = 0;
- while (i < segment.len) : (i += 1) {
- const c = segment[i];
-
- // For URL-canonicalization sets, preserve existing %XX sequences so
- // already-encoded inputs round-trip cleanly. The `component` set treats
- // input as opaque and re-encodes `%` itself, since the caller is
- // embedding raw user data and a literal '%' must not be misread.
- if (encode_set != .component and c == '%' and i + 2 < segment.len) {
- const end = i + 2;
- const h1 = segment[i + 1];
- const h2 = segment[end];
- if (std.ascii.isHex(h1) and std.ascii.isHex(h2)) {
- try buf.appendSlice(allocator, segment[i .. end + 1]);
- i = end;
- continue;
- }
- }
-
- if (shouldPercentEncode(c, encode_set)) {
- try buf.print(allocator, "%{X:0>2}", .{c});
- } else {
- try buf.append(allocator, c);
- }
- }
-
- return buf.items;
+/// Percent-encodes everything but the RFC 3986 unreserved characters
+/// (A-Z a-z 0-9 - . _ ~), so raw data can be embedded as a query value.
+pub fn percentEncodeComponent(allocator: Allocator, value: []const u8) ![]const u8 {
+ var aw: std.Io.Writer.Allocating = try .initCapacity(allocator, value.len);
+ try std.Uri.Component.percentEncode(&aw.writer, value, isUnreserved);
+ return aw.written();
}
-fn shouldPercentEncode(c: u8, comptime encode_set: EncodeSet) bool {
+fn isUnreserved(c: u8) bool {
return switch (c) {
- // Unreserved characters (RFC 3986)
- 'A'...'Z', 'a'...'z', '0'...'9', '-', '.', '_', '~' => false,
- // sub-delims allowed in path/query but some must be encoded in userinfo/query_legacy/component
- '!', '$', '\'', '(', ')', '*', '+', ',' => encode_set == .component,
- // '&' and ';' must be encoded for legacy encoding (to preserve NCRs like &#nnnnn;)
- // and for component encoding (so a value can't break out into a new param)
- '&', ';' => encode_set == .userinfo or encode_set == .query_legacy or encode_set == .component,
- '=' => encode_set == .userinfo or encode_set == .component,
- // Separators: userinfo and component must encode these
- '/', ':', '@' => encode_set == .userinfo or encode_set == .component,
- // '?' is allowed in queries only
- '?' => encode_set != .query and encode_set != .query_legacy,
- // '#' is allowed in fragments only
- '#' => encode_set != .fragment,
- // Everything else needs encoding (including space)
- else => true,
+ 'A'...'Z', 'a'...'z', '0'...'9', '-', '.', '_', '~' => true,
+ else => false,
};
}