Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Response): single Content-Range in http response #7199

Merged
merged 3 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions test/js/bun/http/serve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
return new Response(
new ReadableStream({
pull(controller) {
throw new Error("TestPassed");

Check failure on line 229 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: TestPassed

at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:229:24) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 229 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: TestPassed

at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:229:24) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 229 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-aarch64

error: TestPassed

at pull (/Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:229:24) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)
},
cancel(reason) {},
}),
Expand Down Expand Up @@ -263,7 +263,7 @@
async pull(controller) {
controller.enqueue("PASS");
controller.close();
throw new Error("FAIL");

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64-baseline

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests linux-x64

error: FAIL

at /home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/home/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-aarch64

error: FAIL

at /Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-aarch64

error: FAIL

at /Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-aarch64

error: FAIL

at /Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / Tests bun-darwin-aarch64

error: FAIL

at /Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/jarred/actions-2/_work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / macOS Test (bun-darwin-x64-baseline, macos-12)

error: FAIL

at /Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / macOS Test (bun-darwin-x64-baseline, macos-12)

error: FAIL

at /Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / macOS Test (bun-darwin-x64-baseline, macos-12)

error: FAIL

at /Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)

Check failure on line 266 in test/js/bun/http/serve.test.ts

View workflow job for this annotation

GitHub Actions / macOS Test (bun-darwin-x64-baseline, macos-12)

error: FAIL

at /Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:266:24 at pull (/Users/runner/work/bun/bun/test/js/bun/http/serve.test.ts:263:39) at promiseInvokeOrNoopMethodNoCatch (:1:20) at promiseInvokeOrNoopMethod (:1:20) at readableStreamDefaultControllerCallPullIfNeeded (:1:20)
},
});
return new Response(stream, options);
Expand Down Expand Up @@ -947,6 +947,18 @@
},
});

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],
Expand All @@ -973,6 +985,19 @@
});
}

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],
Expand Down
Loading