Skip to content
Myr

Added 416 support to res.sendfile()

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 ee4c7d3 · Browse files · 2 files+24 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−27 removed

  • tests:+2 added−16 removed
  • other:+22 added−11 removed

2 files+24 added−27 removed

modified:lib/express/response.js+22 added-11 removed
151 unmodified lines
152153 fs.stat(path, function(err, stat){154 if (err) return error(err);155 var status = 200;156 if (ranges) ranges = parseRange(stat.size, ranges);156157 // We have a Range request158 if (ranges) {159 ranges = parseRange(stat.size, ranges);160 // Valid161 if (ranges) {158 var stream = fs.createReadStream(path, ranges[0])159 , start = ranges[0].start160 , end = ranges[0].end;161 status = 206;162 self.header('Content-Range', 'bytes '163 + start164 + '-'165 + end166 + '/'167 + stat.size);162 var stream = fs.createReadStream(path, ranges[0])163 , start = ranges[0].start164 , end = ranges[0].end;165 status = 206;166 self.header('Content-Range', 'bytes '167 + start168 + '-'169 + end170 + '/'171 + stat.size);172 // Invalid173 } else {174 return self.send(416);175 }176 // Stream the entire file177 } else {178 var stream = fs.createReadStream(path);179 self.header('Content-Length', stat.size);180 }181182 // Transfer183 self.contentType(path);184 self.header('Accept-Ranges', 'bytes');185 self.writeHead(status, self.headers);186 if (head) return self.end();
211 unmodified lines
modified:test/response.test.js+2 added-16 removed
225 unmodified lines
226 227 'test #sendfile()': function(){228 var app = express.createServer();229230 app.set('stream threshold', 2 * 1024)231230 app.get('/*', function(req, res, next){231 var file = req.params[0]232 , filePath = __dirname + '/fixtures/' + file;233 res.sendfile(filePath, function(err, path){
26 unmodified lines
260 261 'test #sendfile() Accept-Ranges': function(){262 var app = express.createServer();263 266 app.set('stream threshold', 1024);267 264 app.get('/*', function(req, res, next){265 var file = req.params[0]266 , path = __dirname + '/fixtures/' + file;267 res.sendfile(path);
6 unmodified lines
274 275 'test #sendfile() Range': function(){276 var app = express.createServer();277 282 app.set('stream threshold', 1024);283 278 app.get('/*', function(req, res, next){279 var file = req.params[0]280 , path = __dirname + '/fixtures/' + file;281 res.sendfile(path);
9 unmodified lines
291292 'test #sendfile() Range invalid syntax': function(){293 var app = express.createServer();294 301 app.set('stream threshold', 1024);302 295 app.get('/*', function(req, res, next){296 var file = req.params[0]297 , path = __dirname + '/fixtures/' + file;298 res.sendfile(path);299 });300301 assert.response(app,302 { url: '/large.json', headers: { 'Range': 'basdytes=asdf' }},311 { headers: {312 'Content-Type': 'application/json'313 }, status: 200 });303 { body: 'Requested Range Not Satisfiable', status: 416 });304 },305306 'test #sendfile() Range invalid range': function(){307 var app = express.createServer();308 319 app.set('stream threshold', 1024);320 309 app.get('/*', function(req, res, next){310 var file = req.params[0]311 , path = __dirname + '/fixtures/' + file;312 res.sendfile(path);313 });314315 assert.response(app,316 { url: '/large.json', headers: { 'Range': 'bytes=500-10' }},329 { headers: {330 'Content-Type': 'application/json'331 }, status: 200 });317 { body: 'Requested Range Not Satisfiable', status: 416 });318 },319 320 'test #download()': function(){321 var app = express.createServer();
82 unmodified lines