Handling invalid ranges
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent c3365aa · Browse files · 3 files·+45 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+45 added−2 removed
- tests:+38 added−0 removed
- other:+7 added−2 removed
Provenance
- Commit
4d01b4f- Tree
02217ec- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
c3365aa
3 files changed+45 added−2 removed
modified:lib/express/response.js+1 added-1 removed
148 unmodified lines149 150 fs.stat(path, function(err, stat){151 if (err) return error(err);152 if (stat.size >= streamThreshold) {153- if (ranges) ranges = parseRange(stat.size, ranges);153 var status = 200;154+ if (ranges) ranges = parseRange(stat.size, ranges);155 if (ranges) {156 var stream = fs.createReadStream(path, ranges[0]),157 start = ranges[0].start,158 end = ranges[0].end;202 unmodified linesmodified:lib/express/utils.js+6 added-1 removed
50 unmodified lines51525354555556575859605 unmodified lines666768697071727374757650 unmodified lines * @api private */
exports.parseRange = function(size, str){ var valid = true; return str.substr(6).split(',').map(function(range){ var arr = str.substr(6).split(',').map(function(range){ var range = range.split('-'), start = parseInt(range[0], 10), end = parseInt(range[1], 10);
5 unmodified lines } else if (isNaN(end)) { end = size - 1; }
// Invalid if (isNaN(start) || isNaN(end) || start > end) valid = false;
return { start: start, end: end }; }); return valid ? arr : undefined;};No newline at end of fileNo newline at end of filemodified:test/response.test.js+38 added
251 unmodified lines252 'Content-Length': 500,253 'Content-Range': 'bytes 0-499/2535'254 }, status: 206 });255 },256+257+ 'test #sendfile() Range invalid syntax': function(assert){258+ var app = express.createServer();259+ 260+ app.set('stream threshold', 1024);261+ 262+ app.get('/*', function(req, res, next){263+ var file = req.params[0],264+ path = __dirname + '/fixtures/' + file;265+ res.sendfile(path);266+ });267+268+ assert.response(app,269+ { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},270+ { headers: {271+ 'Content-Type': 'application/json',272+ 'Content-Length': 2535273+ }, status: 200 });274+ },275+ 276+ 'test #sendfile() Range invalid range': function(assert){277+ var app = express.createServer();278+ 279+ app.set('stream threshold', 1024);280+ 281+ app.get('/*', function(req, res, next){282+ var file = req.params[0],283+ path = __dirname + '/fixtures/' + file;284+ res.sendfile(path);285+ });286+287+ assert.response(app,288+ { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},289+ { headers: {290+ 'Content-Type': 'application/json',291+ 'Content-Length': 2535292+ }, status: 200 });293+ },294 295 'test #download()': function(assert){296 var app = express.createServer();297 83 unmodified lines