Skip to content

Lightpanda: the headless browser designed for AI and automation

`Request`: add `Request#formData`

Halil Durak50d942f807c5
diff --git a/src/browser/webapi/net/Request.zig b/src/browser/webapi/net/Request.zig
index b9b28c31e..e3b46c70f 100644
--- a/src/browser/webapi/net/Request.zig
+++ b/src/browser/webapi/net/Request.zig
@@ -298,6 +298,21 @@ pub fn bytes(self: *Request, exec: *const Execution) !js.Promise {
return local.resolvePromise(js.TypedArray(u8){ .values = self._body orelse "" });
}
+pub fn formData(self: *Request, exec: *const Execution) !js.Promise {
+ const local = exec.js.local.?;
+ if (self.consume(local)) |rejected| {
+ return rejected;
+ }
+
+ const headers = try self.getHeaders(exec);
+ const content_type = try headers.get("content-type", exec) orelse "";
+ const form_data = body_init.parseFormData(self._body orelse "", content_type, exec) catch |err| switch (err) {
+ error.OutOfMemory => return err,
+ else => return local.rejectPromise(.{ .type_error = "Failed to parse body as FormData" }),
+ };
+ return local.resolvePromise(form_data);
+}
+
pub fn clone(self: *const Request, exec: *const Execution) !*Request {
const arena = try exec.getArena(if (self._body) |b| b.len else 512, "Request.clone");
errdefer exec.releaseArena(arena);
@@ -340,6 +355,7 @@ pub const JsApi = struct {
pub const json = bridge.function(Request.json, .{});
pub const arrayBuffer = bridge.function(Request.arrayBuffer, .{});
pub const bytes = bridge.function(Request.bytes, .{});
+ pub const formData = bridge.function(Request.formData, .{});
pub const clone = bridge.function(Request.clone, .{});
};
diff --git a/src/browser/webapi/net/body_init.zig b/src/browser/webapi/net/body_init.zig
index d47ecad31..551a03613 100644
--- a/src/browser/webapi/net/body_init.zig
+++ b/src/browser/webapi/net/body_init.zig
@@ -125,6 +125,43 @@ pub const Extracted = struct {
content_type: ?[]const u8,
};
+pub fn parseFormData(bytes: []const u8, content_type: []const u8, exec: *const js.Execution) !*FormData {
+ const end = std.mem.indexOfScalar(u8, content_type, ';') orelse content_type.len;
+ const essence = std.mem.trim(u8, content_type[0..end], " \t");
+
+ if (std.ascii.eqlIgnoreCase(essence, "multipart/form-data")) {
+ const boundary = multipartBoundary(content_type) orelse return error.InvalidFormData;
+ return FormData.initFromMultipart(bytes, boundary, exec);
+ }
+ if (std.ascii.eqlIgnoreCase(essence, "application/x-www-form-urlencoded")) {
+ return FormData.initFromUrlEncoded(bytes, exec);
+ }
+ return error.InvalidFormData;
+}
+
+// Extracts the boundary param from a multipart/form-data Content-Type.
+// Splitting on ';' is safe: RFC 2046 §5.1.1 bchars never include ';', even
+// in the quoted form.
+fn multipartBoundary(content_type: []const u8) ?[]const u8 {
+ var it = std.mem.splitScalar(u8, content_type, ';');
+ _ = it.next(); // skip the type/subtype
+ while (it.next()) |param_| {
+ const param = std.mem.trim(u8, param_, " \t");
+ if (!std.ascii.startsWithIgnoreCase(param, "boundary=")) {
+ continue;
+ }
+ var value = param["boundary=".len..];
+ if (value.len >= 2 and value[0] == '"' and value[value.len - 1] == '"') {
+ value = value[1 .. value.len - 1];
+ }
+ if (value.len == 0) {
+ return null;
+ }
+ return value;
+ }
+ return null;
+}
+
// "UTF-8 decode" (Encoding §4.2) strips a leading BOM; consuming a body as
// text/json must use it, while arrayBuffer/blob/bytes keep the raw bytes.
pub fn stripUtf8Bom(bytes: []const u8) []const u8 {
@@ -188,6 +225,15 @@ test "BodyInit: buffer source has no default Content-Type" {
try testing.expectEqual(true, r.content_type == null);
}
+test "multipartBoundary" {
+ try testing.expectString("----abc", multipartBoundary("multipart/form-data; boundary=----abc").?);
+ try testing.expectString("a b", multipartBoundary("multipart/form-data; boundary=\"a b\"").?);
+ try testing.expectString("x", multipartBoundary("multipart/form-data; charset=UTF-8; Boundary=x").?);
+ try testing.expectEqual(null, multipartBoundary("multipart/form-data"));
+ try testing.expectEqual(null, multipartBoundary("multipart/form-data; boundary="));
+ try testing.expectEqual(null, multipartBoundary("multipart/form-data; charset=UTF-8"));
+}
+
test "stripUtf8Bom" {
try testing.expectString("abc", stripUtf8Bom("\xef\xbb\xbfabc"));
try testing.expectString("abc", stripUtf8Bom("abc"));