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>
This commit is contained in:
Ciro Spaciari
2023-09-13 21:48:30 -07:00
committed by GitHub
parent 9976e3f528
commit fa2ef0972b
4 changed files with 35 additions and 3 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);
});
});