Skip to content

Commit

Permalink
truncate length to file size in bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Dec 1, 2014
1 parent 5f1a222 commit 0f3a6d0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/ecstatic.js
Expand Up @@ -180,16 +180,21 @@ var ecstatic = module.exports = function (dir, options) {
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total-1;
var end = Math.min(total-1, partialend ? parseInt(partialend, 10) : total-1);
var chunksize = (end-start)+1;
if (start > end) {
if (start > end || isNaN(start) || isNaN(end)) {
return status['416'](res, next);
}
var fstream = fs.createReadStream(file, {start: start, end: end});
fstream.on('error', function (err) {
status['500'](res, next, { error: err });
});
res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': contentType || 'application/octet-stream' });
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': contentType || 'application/octet-stream'
});
fstream.pipe(res);
return;
}
Expand Down

0 comments on commit 0f3a6d0

Please sign in to comment.