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

Content-Range for 416 Range Not Satisfiable #839

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ module.exports = function createMiddleware(_dir, _options) {
let fstream = null;

if (start > end || isNaN(start) || isNaN(end)) {
status['416'](res, next);
status['416'](res, next, { size: total });
return;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/core/status-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ exports['404'] = (res, next) => {
}
};

exports['416'] = (res, next) => {
exports['416'] = (res, next, opts) => {
res.statusCode = 416;
res.setHeader('content-range', 'bytes */' + opts.size)
if (typeof next === 'function') {
next();
} else if (res.writable) {
Expand Down
26 changes: 24 additions & 2 deletions test/range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,28 @@ test('range past the end', (t) => {
});
});

test('range starts beyond the end', (t) => {
t.plan(4);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });

server.listen(0, () => {
const port = server.address().port;
const opts = {
uri: `http://localhost:${port}/e.html`,
headers: { range: '500-' },
};
request.get(opts, (err, res, body) => {
t.ifError(err);
t.equal(res.statusCode, 416, 'range error status code');
t.equal(res.headers['content-range'], 'bytes */11');
t.equal(body, 'Requested range not satisfiable');
});
});
});

test('NaN range', (t) => {
t.plan(3);
t.plan(4);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });

Expand All @@ -60,13 +80,14 @@ test('NaN range', (t) => {
request.get(opts, (err, res, body) => {
t.ifError(err);
t.equal(res.statusCode, 416, 'range error status code');
t.equal(res.headers['content-range'], 'bytes */11');
t.equal(body, 'Requested range not satisfiable');
});
});
});

test('flipped range', (t) => {
t.plan(3);
t.plan(4);
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
t.on('end', () => { server.close(); });

Expand All @@ -79,6 +100,7 @@ test('flipped range', (t) => {
request.get(opts, (err, res, body) => {
t.ifError(err);
t.equal(res.statusCode, 416, 'range error status code');
t.equal(res.headers['content-range'], 'bytes */11');
t.equal(body, 'Requested range not satisfiable');
});
});
Expand Down