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(sirv): avoid false-positive on too big range end #147

Merged
merged 3 commits into from
Apr 24, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/sirv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ function send(req, res, file, stats, headers) {
let end = opts.end = parseInt(y, 10) || stats.size - 1;
let start = opts.start = parseInt(x, 10) || 0;

if (start >= stats.size || end >= stats.size) {
if (end >= stats.size) {
end = stats.size - 1;
}

if (start >= stats.size) {
res.setHeader('Content-Range', `bytes */${stats.size}`);
res.statusCode = 416;
return res.end();
Expand Down
18 changes: 16 additions & 2 deletions tests/sirv.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,11 +1017,11 @@ ranges('should assume the end-value is final byte when not included :: full', as
}
});

ranges('should throw `416` when range cannot be met (overflow)', async () => {
ranges('should throw `416` when range start cannot be met (overflow)', async () => {
let server = utils.http();

try {
let headers = { Range: 'bytes=0-123456' };
let headers = { Range: 'bytes=123456-234567' };
let file = await utils.lookup('bundle.67329.js', 'utf8');
await server.send('GET', '/bundle.67329.js', { headers }).catch(err => {
assert.is(err.headers['content-range'], `bytes */${file.size}`);
Expand All @@ -1032,6 +1032,20 @@ ranges('should throw `416` when range cannot be met (overflow)', async () => {
}
});

ranges('should shrink range end if it cannot be met (overflow)', async () => {
let server = utils.http();

try {
let headers = { Range: 'bytes=10-123456' };
let file = await utils.lookup('bundle.67329.js', 'utf8');
let res = await server.send('GET', '/bundle.67329.js', { headers });
assert.is(res.headers['content-range'], `bytes 10-${file.size - 1}/${file.size}`);
assert.is(res.statusCode, 206);
} finally {
server.close();
}
});

ranges('should not mutate response headers on subsequent non-Range requests :: dev', async () => {
let server = utils.http({ dev: true });

Expand Down