From c9daba7e8d5c93b994433f78d86e7fccbdbd8721 Mon Sep 17 00:00:00 2001 From: WingLim Date: Sat, 18 Nov 2023 17:14:30 +0800 Subject: [PATCH 1/3] fix: only write header if not exist If user set header by himself, then we not write duplicate header. --- src/bun.js/api/server.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 5e9abc282a80b0..cc8dd0bc50dafc 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -3008,9 +3008,11 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp }; var has_content_disposition = false; + var has_content_range = false; if (response.init.headers) |headers_| { has_content_disposition = headers_.fastHas(.ContentDisposition); - needs_content_range = needs_content_range and headers_.fastHas(.ContentRange); + has_content_range = headers_.fastHas(.ContentRange); + needs_content_range = needs_content_range and has_content_range; if (needs_content_range) { status = 206; } @@ -3057,7 +3059,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp this.flags.needs_content_length = false; } - if (needs_content_range) { + if (needs_content_range and !has_content_range) { var content_range_buf: [1024]u8 = undefined; resp.writeHeader( From df34295de17e2cccc2fbaa296a9fd36385260325 Mon Sep 17 00:00:00 2001 From: WingLim Date: Sat, 18 Nov 2023 17:16:31 +0800 Subject: [PATCH 2/3] feat: add test to check header --- test/js/bun/http/serve.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/js/bun/http/serve.test.ts b/test/js/bun/http/serve.test.ts index aef748c0d923df..b8b65a9ce92dd1 100644 --- a/test/js/bun/http/serve.test.ts +++ b/test/js/bun/http/serve.test.ts @@ -947,6 +947,18 @@ describe("should support Content-Range with Bun.file()", () => { }, }); + const getServerWithSize = runTest.bind(null, { + fetch(req) { + const { searchParams } = new URL(req.url); + const start = Number(searchParams.get("start")); + const end = Number(searchParams.get("end")); + const file = Bun.file(fixture); + return new Response(file.slice(start, end), { + headers: { 'Content-Range': 'bytes ' + start + '-' + end + '/' + file.size } + }); + }, + }); + const good = [ [0, 1], [1, 2], @@ -973,6 +985,19 @@ describe("should support Content-Range with Bun.file()", () => { }); } + for (const [start, end] of good) { + it(`good range with size: ${start} - ${end}`, async () => { + await getServerWithSize(async server => { + const response = await fetch(`http://${server.hostname}:${server.port}/?start=${start}&end=${end}`, { + verbose: true, + }); + expect(parseInt(response.headers.get('Content-Range')?.split("/")[1])).toEqual(full.byteLength) + expect(await response.arrayBuffer()).toEqual(full.buffer.slice(start, end)); + expect(response.status).toBe(start > 0 || end < full.byteLength ? 206 : 200); + }) + }) + } + const emptyRanges = [ [0, 0], [1, 1], From 739c3f7e34e252c50559303c94deb8d4f52df542 Mon Sep 17 00:00:00 2001 From: WingLim Date: Sat, 18 Nov 2023 17:18:51 +0800 Subject: [PATCH 3/3] chore: format code --- test/js/bun/http/serve.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/js/bun/http/serve.test.ts b/test/js/bun/http/serve.test.ts index b8b65a9ce92dd1..7974b06e374447 100644 --- a/test/js/bun/http/serve.test.ts +++ b/test/js/bun/http/serve.test.ts @@ -954,7 +954,7 @@ describe("should support Content-Range with Bun.file()", () => { const end = Number(searchParams.get("end")); const file = Bun.file(fixture); return new Response(file.slice(start, end), { - headers: { 'Content-Range': 'bytes ' + start + '-' + end + '/' + file.size } + headers: { "Content-Range": "bytes " + start + "-" + end + "/" + file.size }, }); }, }); @@ -991,11 +991,11 @@ describe("should support Content-Range with Bun.file()", () => { const response = await fetch(`http://${server.hostname}:${server.port}/?start=${start}&end=${end}`, { verbose: true, }); - expect(parseInt(response.headers.get('Content-Range')?.split("/")[1])).toEqual(full.byteLength) + expect(parseInt(response.headers.get("Content-Range")?.split("/")[1])).toEqual(full.byteLength); expect(await response.arrayBuffer()).toEqual(full.buffer.slice(start, end)); expect(response.status).toBe(start > 0 || end < full.byteLength ? 206 : 200); - }) - }) + }); + }); } const emptyRanges = [