Skip to content

Lightpanda: the headless browser designed for AI and automation

fix(net): buffer closed ReadableStream bodies for fetch POST

arimu1f2ddaee8d194
diff --git a/src/browser/tests/net/fetch.html b/src/browser/tests/net/fetch.html
index f624d8ac8..c41dd8133 100644
--- a/src/browser/tests/net/fetch.html
+++ b/src/browser/tests/net/fetch.html
@@ -348,6 +348,33 @@
}
</script>
+<script id=fetch_readablestream_body type=module>
+ {
+ const state = await testing.async();
+ const payload = "name=Streamed&count=9";
+ const stream = new ReadableStream({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode(payload));
+ controller.close();
+ },
+ });
+
+ const response = await fetch("http://127.0.0.1:9582/xhr/echo_post_body", {
+ method: "POST",
+ body: stream,
+ duplex: "half",
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
+ });
+ const text = await response.text();
+ state.resolve();
+
+ await state.done(() => {
+ testing.expectEqual(200, response.status);
+ testing.expectEqual(payload, text);
+ });
+ }
+</script>
+
<script id=fetch_blob_url_in_worker type=module>
{
const state = await testing.async();
diff --git a/src/browser/webapi/net/body_init.zig b/src/browser/webapi/net/body_init.zig
index 025fff531..9a32e380c 100644
--- a/src/browser/webapi/net/body_init.zig
+++ b/src/browser/webapi/net/body_init.zig
@@ -104,14 +104,12 @@ pub const BodyInit = union(enum) {
.content_type = null,
};
},
- .stream => {
- // A ReadableStream body cannot be serialized synchronously.
- // Callers that support streaming bodies (Response) special-case
- // the `.stream` arm before calling extract; the Request/XHR
- // paths that reach here have no place to store a stream, so
- // they send an empty body. Like other non-string sources, a
- // stream carries no default Content-Type.
- return .{ .bytes = "", .content_type = null };
+ .stream => |stream| {
+ // Response special-cases `.stream` before extract. Request/XHR
+ // paths buffer a closed stream synchronously; open streams and
+ // async-only bodies reject rather than send Content-Length: 0.
+ const bytes = try stream.collectBodyBytes(arena);
+ return .{ .bytes = bytes, .content_type = null };
},
}
}
diff --git a/src/browser/webapi/streams/ReadableStream.zig b/src/browser/webapi/streams/ReadableStream.zig
index 1089d7502..88eb13ed7 100644
--- a/src/browser/webapi/streams/ReadableStream.zig
+++ b/src/browser/webapi/streams/ReadableStream.zig
@@ -131,6 +131,29 @@ pub fn getLocked(self: *const ReadableStream) bool {
return self._reader != null;
}
+/// Drain a closed stream's internal queue into bytes for outbound HTTP
+/// request bodies. Open, errored, or locked streams, and chunks that cannot
+/// be converted synchronously, return `error.TypeError`.
+pub fn collectBodyBytes(self: *ReadableStream, arena: std.mem.Allocator) ![]const u8 {
+ if (self.getLocked()) return error.TypeError;
+ switch (self._state) {
+ .errored, .readable => return error.TypeError,
+ .closed => {},
+ }
+
+ var buf = std.Io.Writer.Allocating.init(arena);
+ const controller = self._controller;
+ while (controller.dequeue()) |chunk| {
+ const bytes = switch (chunk) {
+ .string => |s| s,
+ .uint8array => |arr| arr.values,
+ .js_value => return error.TypeError,
+ };
+ try buf.writer.writeAll(bytes);
+ }
+ return buf.written();
+}
+
pub fn callPullIfNeeded(self: *ReadableStream) !void {
if (!self.shouldCallPull()) {
return;
diff --git a/src/testing.zig b/src/testing.zig
index 7cf26c4a7..4daac138a 100644
--- a/src/testing.zig
+++ b/src/testing.zig
@@ -970,6 +970,19 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
});
}
+ if (std.mem.eql(u8, path, "/xhr/echo_post_body")) {
+ var body_buf: [4096]u8 = undefined;
+ const body = if (req.head.method.requestHasBody())
+ req.readerExpectNone(&body_buf).allocRemaining(arena_allocator, .limited(body_buf.len)) catch ""
+ else
+ "";
+ return req.respond(body, .{
+ .extra_headers = &.{
+ .{ .name = "Content-Type", .value = "text/plain; charset=utf-8" },
+ },
+ });
+ }
+
if (std.mem.eql(u8, path, "/redirect_to_echo")) {
// 302 to /echo_method. Used by the Page.reload-after-redirect test to
// confirm a POST→302→GET chain doesn't replay POST on reload.