Skip to content

Commit a4c13ae

Browse files
dstdKernelDeimos
authored andcommitted
"416 Range Not Satisfiable" should contain "Content-Range" as of RFC 9110 section 15.5.17
1 parent d2c7a5f commit a4c13ae

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ module.exports = function createMiddleware(_dir, _options) {
286286
let fstream = null;
287287

288288
if (start > end || isNaN(start) || isNaN(end)) {
289-
status['416'](res, next);
289+
status['416'](res, next, { size: total });
290290
return;
291291
}
292292

lib/core/status-handlers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ exports['404'] = (res, next) => {
4141
}
4242
};
4343

44-
exports['416'] = (res, next) => {
44+
exports['416'] = (res, next, opts) => {
4545
res.statusCode = 416;
46+
res.setHeader('content-range', 'bytes */' + opts.size)
4647
if (typeof next === 'function') {
4748
next();
4849
} else if (res.writable) {

test/range.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,28 @@ test('range past the end', (t) => {
4646
});
4747
});
4848

49+
test('range starts beyond the end', (t) => {
50+
t.plan(4);
51+
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
52+
t.on('end', () => { server.close(); });
53+
54+
server.listen(0, () => {
55+
const port = server.address().port;
56+
const opts = {
57+
uri: `http://localhost:${port}/e.html`,
58+
headers: { range: '500-' },
59+
};
60+
request.get(opts, (err, res, body) => {
61+
t.ifError(err);
62+
t.equal(res.statusCode, 416, 'range error status code');
63+
t.equal(res.headers['content-range'], 'bytes */11');
64+
t.equal(body, 'Requested range not satisfiable');
65+
});
66+
});
67+
});
68+
4969
test('NaN range', (t) => {
50-
t.plan(3);
70+
t.plan(4);
5171
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
5272
t.on('end', () => { server.close(); });
5373

@@ -60,13 +80,14 @@ test('NaN range', (t) => {
6080
request.get(opts, (err, res, body) => {
6181
t.error(err);
6282
t.equal(res.statusCode, 416, 'range error status code');
83+
t.equal(res.headers['content-range'], 'bytes */11');
6384
t.equal(body, 'Requested range not satisfiable');
6485
});
6586
});
6687
});
6788

6889
test('flipped range', (t) => {
69-
t.plan(3);
90+
t.plan(4);
7091
const server = http.createServer(ecstatic(`${__dirname}/public/subdir`));
7192
t.on('end', () => { server.close(); });
7293

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

0 commit comments

Comments
 (0)