Skip to content

Commit bf08db7

Browse files
cirospaciariJarred-Sumner
authored andcommitted
fix(Bun.serve) fix buffering edge case (oven-sh#5152)
* fix buffering clean * fix resolveMaybeNeedsTrailingSlash and try to fix ci/cd error * fix resolveMaybeNeedsTrailingSlash and try to fix ci/cd error * oops --------- Co-authored-by: Jarred Sumner <[email protected]>
1 parent 8c25aab commit bf08db7

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/bun.js/api/server.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
27232723
}
27242724

27252725
pub fn doRender(this: *RequestContext) void {
2726-
ctxLog("render", .{});
2726+
ctxLog("doRender", .{});
27272727

27282728
if (this.flags.aborted) {
27292729
this.finalizeForAbort();
@@ -3039,7 +3039,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
30393039

30403040
if (last) {
30413041
var bytes = this.request_body_buf;
3042-
defer this.request_body_buf = .{};
3042+
30433043
var old = body.value;
30443044

30453045
const total = bytes.items.len + chunk.len;
@@ -3070,6 +3070,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
30703070
};
30713071
// }
30723072
}
3073+
this.request_body_buf = .{};
30733074

30743075
if (old == .Locked) {
30753076
var vm = this.server.vm;

src/bun.js/javascript.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ pub const VirtualMachine = struct {
17161716
printed,
17171717
),
17181718
};
1719-
res.* = ErrorableString.err(error.NameTooLong, ResolveMessage.create(global, VirtualMachine.get().allocator, msg, source.utf8()).asVoid());
1719+
res.* = ErrorableString.err(error.NameTooLong, ResolveMessage.create(global, VirtualMachine.get().allocator, msg, source_utf8.slice()).asVoid());
17201720
return;
17211721
}
17221722

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const content = "Bun".repeat(15360000);
2+
3+
const server = Bun.serve({
4+
port: 0,
5+
fetch: async req => {
6+
const data = await req.formData();
7+
return new Response(data.get("name") === content ? "OK" : "NO");
8+
},
9+
});
10+
11+
const formData = new FormData();
12+
formData.append("name", content);
13+
const result = await fetch(`http://${server.hostname}:${server.port}`, {
14+
method: "POST",
15+
body: formData,
16+
}).then(res => res.text());
17+
18+
server.stop();
19+
20+
process.exit(result === "OK" ? 0 : 1);

test/js/bun/http/bun-server.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, test } from "bun:test";
2+
import { bunExe, bunEnv } from "harness";
23

34
describe("Server", () => {
45
test("normlizes incoming request URLs", async () => {
@@ -357,4 +358,14 @@ describe("Server", () => {
357358
server.stop(true);
358359
}
359360
});
361+
362+
test("should not crash with big formData", async () => {
363+
const proc = Bun.spawn({
364+
cmd: [bunExe(), "big-form-data.fixture.js"],
365+
cwd: import.meta.dir,
366+
env: bunEnv,
367+
});
368+
await proc.exited;
369+
expect(proc.exitCode).toBe(0);
370+
});
360371
});

0 commit comments

Comments
 (0)