aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-09-13 21:48:30 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-13 21:48:30 -0700
commitfa2ef0972b11de2be5ecd5c92f13f56d6890c2ad (patch)
treeb1939d4dbd5e4ed1510e553215a46e3cc0c7223b
parent9976e3f528bae74667a2f4742c4b165b7f68c76d (diff)
downloadbun-fa2ef0972b11de2be5ecd5c92f13f56d6890c2ad.tar.gz
bun-fa2ef0972b11de2be5ecd5c92f13f56d6890c2ad.tar.zst
bun-fa2ef0972b11de2be5ecd5c92f13f56d6890c2ad.zip
fix(Bun.serve) fix buffering edge case (#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 <jarred@jarredsumner.com>
-rw-r--r--src/bun.js/api/server.zig5
-rw-r--r--src/bun.js/javascript.zig2
-rw-r--r--test/js/bun/http/big-form-data.fixture.js20
-rw-r--r--test/js/bun/http/bun-server.test.ts11
4 files changed, 35 insertions, 3 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig
index 390f8ef96..ac0f04f34 100644
--- a/src/bun.js/api/server.zig
+++ b/src/bun.js/api/server.zig
@@ -2723,7 +2723,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
}
pub fn doRender(this: *RequestContext) void {
- ctxLog("render", .{});
+ ctxLog("doRender", .{});
if (this.flags.aborted) {
this.finalizeForAbort();
@@ -3039,7 +3039,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
if (last) {
var bytes = this.request_body_buf;
- defer this.request_body_buf = .{};
+
var old = body.value;
const total = bytes.items.len + chunk.len;
@@ -3070,6 +3070,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
};
// }
}
+ this.request_body_buf = .{};
if (old == .Locked) {
var vm = this.server.vm;
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 60364706e..6ce145d82 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -1716,7 +1716,7 @@ pub const VirtualMachine = struct {
printed,
),
};
- res.* = ErrorableString.err(error.NameTooLong, ResolveMessage.create(global, VirtualMachine.get().allocator, msg, source.utf8()).asVoid());
+ res.* = ErrorableString.err(error.NameTooLong, ResolveMessage.create(global, VirtualMachine.get().allocator, msg, source_utf8.slice()).asVoid());
return;
}
diff --git a/test/js/bun/http/big-form-data.fixture.js b/test/js/bun/http/big-form-data.fixture.js
new file mode 100644
index 000000000..72036a222
--- /dev/null
+++ b/test/js/bun/http/big-form-data.fixture.js
@@ -0,0 +1,20 @@
+const content = "Bun".repeat(15360000);
+
+const server = Bun.serve({
+ port: 0,
+ fetch: async req => {
+ const data = await req.formData();
+ return new Response(data.get("name") === content ? "OK" : "NO");
+ },
+});
+
+const formData = new FormData();
+formData.append("name", content);
+const result = await fetch(`http://${server.hostname}:${server.port}`, {
+ method: "POST",
+ body: formData,
+}).then(res => res.text());
+
+server.stop();
+
+process.exit(result === "OK" ? 0 : 1);
diff --git a/test/js/bun/http/bun-server.test.ts b/test/js/bun/http/bun-server.test.ts
index 3e0e4e71e..398ef4bb7 100644
--- a/test/js/bun/http/bun-server.test.ts
+++ b/test/js/bun/http/bun-server.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, test } from "bun:test";
+import { bunExe, bunEnv } from "harness";
describe("Server", () => {
test("normlizes incoming request URLs", async () => {
@@ -357,4 +358,14 @@ describe("Server", () => {
server.stop(true);
}
});
+
+ test("should not crash with big formData", async () => {
+ const proc = Bun.spawn({
+ cmd: [bunExe(), "big-form-data.fixture.js"],
+ cwd: import.meta.dir,
+ env: bunEnv,
+ });
+ await proc.exited;
+ expect(proc.exitCode).toBe(0);
+ });
});