fix: improve support for unbounded range headers

This commit is contained in:
ProgrammerIn-wonderland
2025-08-27 18:01:19 -04:00
parent 9863f92aaa
commit e48179a1f9
2 changed files with 44 additions and 8 deletions
+22 -4
View File
@@ -94,10 +94,28 @@ module.exports = eggspress('/read', {
const rangeInfo = parseRangeHeader(req.headers["range"]);
if (rangeInfo) {
const { start, end, isMultipart } = rangeInfo;
const contentRange = end !== null
? `bytes ${start}-${end}/*`
: `bytes ${start}-*/*`;
res.set("Content-Range", contentRange);
// For open-ended ranges, we need to calculate the actual end byte
let actualEnd = end;
let fileSize = null;
try {
fileSize = await req.values.fsNode.get('size');
if (end === null) {
actualEnd = fileSize - 1; // File size is 1-based, end byte is 0-based
}
} catch (e) {
// If we can't get file size, we'll let the storage layer handle it
// and not set Content-Range header
actualEnd = null;
fileSize = null;
}
if (actualEnd !== null) {
const totalSize = fileSize !== null ? fileSize : '*';
const contentRange = `bytes ${start}-${actualEnd}/${totalSize}`;
res.set("Content-Range", contentRange);
}
// If this was a multipart request, modify the range header to only include the first range
if (isMultipart) {
@@ -113,10 +113,28 @@ module.exports = eggspress('/token-read', {
const rangeInfo = parseRangeHeader(req.headers["range"]);
if (rangeInfo) {
const { start, end, isMultipart } = rangeInfo;
const contentRange = end !== null
? `bytes ${start}-${end}/*`
: `bytes ${start}-*/*`;
res.set("Content-Range", contentRange);
// For open-ended ranges, we need to calculate the actual end byte
let actualEnd = end;
let fileSize = null;
try {
fileSize = await req.values.fsNode.get('size');
if (end === null) {
actualEnd = fileSize - 1; // File size is 1-based, end byte is 0-based
}
} catch (e) {
// If we can't get file size, we'll let the storage layer handle it
// and not set Content-Range header
actualEnd = null;
fileSize = null;
}
if (actualEnd !== null) {
const totalSize = fileSize !== null ? fileSize : '*';
const contentRange = `bytes ${start}-${actualEnd}/${totalSize}`;
res.set("Content-Range", contentRange);
}
// If this was a multipart request, modify the range header to only include the first range
if (isMultipart) {