Skip to content

fix: handling range headers correctly when full content requested #69

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

Merged
merged 3 commits into from
Mar 19, 2025
Merged
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
25 changes: 23 additions & 2 deletions src/routes/videoPlaybackProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,29 @@ videoPlaybackProxy.get("/", async (c) => {

let responseStatus = googlevideoResponse.status;
if (requestBytes && responseStatus == 200) {
responseStatus = 206;
headersForResponse["content-range"] = `bytes ${requestBytes}/*`;
// check for range headers in the forms:
// "bytes=0-" get full length from start
// "bytes=500-" get full length from 500 bytes in
// "bytes=500-1000" get 500 bytes starting from 500
const [firstByte, lastByte] = requestBytes.split("-");
if (lastByte) {
responseStatus = 206;
headersForResponse["content-range"] = `bytes ${requestBytes}/*`;
} else {
// i.e. "bytes=0-", "bytes=600-"
// full size of content is able to be calculated, so a full Content-Range header can be constructed
const bytesReceived = headersForResponse["content-length"];
// last byte should always be one less than the length
const totalContentLength = Number(firstByte) +
Number(bytesReceived);
const lastByte = totalContentLength - 1;
if (firstByte !== "0") {
// only part of the total content returned, 206
responseStatus = 206;
}
headersForResponse["content-range"] =
`bytes ${firstByte}-${lastByte}/${totalContentLength}`;
}
}

return new Response(googlevideoResponse.body, {
Expand Down