Skip to content
Myr

Remove res.sendfile

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

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent ccdbe4e · Browse files · 3 files+2 added−450 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+2 added−450 removed

  • tests:+0 added−377 removed
  • other:+2 added−73 removed

3 files+2 added−450 removed

modified:lib/response.js+1 added-73 removed
386 unmodified lines
387 });388};389390/**391 * Transfer the file at the given `path`.392 *393 * Automatically sets the _Content-Type_ response header field.394 * The callback `fn(err)` is invoked when the transfer is complete395 * or when an error occurs. Be sure to check `res.sentHeader`396 * if you wish to attempt responding, as the header and some data397 * may have already been transferred.398 *399 * Options:400 *401 * - `maxAge` defaulting to 0 (can be string converted by `ms`)402 * - `root` root directory for relative filenames403 * - `headers` object of headers to serve with file404 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them405 *406 * Other options are passed along to `send`.407 *408 * Examples:409 *410 * The following example illustrates how `res.sendfile()` may411 * be used as an alternative for the `static()` middleware for412 * dynamic situations. The code backing `res.sendfile()` is actually413 * the same code, so HTTP cache support etc is identical.414 *415 * app.get('/user/:uid/photos/:file', function(req, res){416 * var uid = req.params.uid417 * , file = req.params.file;418 *419 * req.user.mayViewFilesFrom(uid, function(yes){420 * if (yes) {421 * res.sendfile('/uploads/' + uid + '/' + file);422 * } else {423 * res.send(403, 'Sorry! you cant see that.');424 * }425 * });426 * });427 *428 * @api public429 */430431res.sendfile = function(path, options, fn){432 var req = this.req;433 var res = this;434 var next = req.next;435436 // support function as second arg437 if (typeof options === 'function') {438 fn = options;439 options = {};440 }441442 options = options || {};443444 // create file stream445 var file = send(req, path, options);446447 // transfer448 sendfile(res, file, options, function (err) {449 if (fn) return fn(err);450 if (err && err.code === 'EISDIR') return next();451452 // next() all but aborted errors453 if (err && err.code !== 'ECONNABORT') {454 next(err);455 }456 });457};458459res.sendfile = deprecate.function(res.sendfile,460 'res.sendfile: Use res.sendFile instead');461462/**391 * Transfer the file at the given `path` as an attachment.392 *393 * Optionally providing an alternate attachment `filename`,394 * and optional callback `fn(err)`. The callback is invoked395 * when the data transfer is complete, or when an error has396 * ocurred. Be sure to check `res.headersSent` if you plan to respond.397 *470 * This method uses `res.sendfile()`.398 * This method uses `res.sendFile()`.399 *400 * @api public401 */402
479 unmodified lines
modified:test/res.sendFile.js-377 removed
205 unmodified lines
206 .get('/')207 .expect(200, 'got it', done);208 })209 })210211 describe('.sendfile(path, fn)', function(){212 it('should invoke the callback when complete', function(done){213 var app = express();214 var cb = after(2, done);215216 app.use(function(req, res){217 res.sendfile('test/fixtures/user.html', cb)218 });219220 request(app)221 .get('/')222 .expect(200, cb);223 })224225 it('should utilize the same options as express.static()', function(done){226 var app = express();227228 app.use(function(req, res){229 res.sendfile('test/fixtures/user.html', { maxAge: 60000 });230 });231232 request(app)233 .get('/')234 .expect('Cache-Control', 'public, max-age=60')235 .end(done);236 })237238 it('should invoke the callback when client aborts', function (done) {239 var cb = after(1, done);240 var app = express();241242 app.use(function (req, res) {243 setImmediate(function () {244 res.sendfile('test/fixtures/name.txt', function (err) {245 should(err).be.ok;246 err.code.should.equal('ECONNABORT');247 cb();248 });249 });250 test.abort();251 });252253 var test = request(app).get('/');254 test.expect(200, cb);255 })256257 it('should invoke the callback when client already aborted', function (done) {258 var cb = after(1, done);259 var app = express();260261 app.use(function (req, res) {262 onFinished(res, function () {263 res.sendfile('test/fixtures/name.txt', function (err) {264 should(err).be.ok;265 err.code.should.equal('ECONNABORT');266 cb();267 });268 });269 test.abort();270 });271272 var test = request(app).get('/');273 test.expect(200, cb);274 })275276 it('should invoke the callback on 404', function(done){277 var app = express()278 , calls = 0;279280 app.use(function(req, res){281 res.sendfile('test/fixtures/nope.html', function(err){282 ++calls;283 assert(!res.headersSent);284 res.send(err.message);285 });286 });287288 request(app)289 .get('/')290 .end(function(err, res){291 assert(1 == calls, 'called too many times');292 res.text.should.startWith("ENOENT, stat");293 res.statusCode.should.equal(200);294 done();295 });296 })297298 it('should not override manual content-types', function(done){299 var app = express();300301 app.use(function(req, res){302 res.contentType('txt');303 res.sendfile('test/fixtures/user.html');304 });305306 request(app)307 .get('/')308 .expect('Content-Type', 'text/plain; charset=utf-8')309 .end(done);310 })311312 it('should invoke the callback on 403', function(done){313 var app = express()314 , calls = 0;315316 app.use(function(req, res){317 res.sendfile('test/fixtures/foo/../user.html', function(err){318 assert(!res.headersSent);319 ++calls;320 res.send(err.message);321 });322 });323324 request(app)325 .get('/')326 .expect('Forbidden')327 .expect(200, done);328 })329330 it('should invoke the callback on socket error', function(done){331 var app = express()332 , calls = 0;333334 app.use(function(req, res){335 res.sendfile('test/fixtures/user.html', function(err){336 assert(!res.headersSent);337 req.socket.listeners('error').should.have.length(1); // node's original handler338 done();339 });340341 req.socket.emit('error', new Error('broken!'));342 });343344 request(app)345 .get('/')346 .end(function(){});347 })348 })349350 describe('.sendfile(path)', function(){351 it('should not serve dotfiles', function(done){352 var app = express();353354 app.use(function(req, res){355 res.sendfile('test/fixtures/.name');356 });357358 request(app)359 .get('/')360 .expect(404, done);361 })362363 it('should accept dotfiles option', function(done){364 var app = express();365366 app.use(function(req, res){367 res.sendfile('test/fixtures/.name', { dotfiles: 'allow' });368 });369370 request(app)371 .get('/')372 .expect(200, 'tobi', done);373 })374375 it('should accept headers option', function(done){376 var app = express();377 var headers = {378 'x-success': 'sent',379 'x-other': 'done'380 };381382 app.use(function(req, res){383 res.sendfile('test/fixtures/user.html', { headers: headers });384 });385386 request(app)387 .get('/')388 .expect('x-success', 'sent')389 .expect('x-other', 'done')390 .expect(200, done);391 })392393 it('should ignore headers option on 404', function(done){394 var app = express();395 var headers = { 'x-success': 'sent' };396397 app.use(function(req, res){398 res.sendfile('test/fixtures/user.nothing', { headers: headers });399 });400401 request(app)402 .get('/')403 .expect(404, function (err, res) {404 if (err) return done(err);405 res.headers.should.not.have.property('x-success');406 done();407 });408 })409410 it('should transfer a file', function (done) {411 var app = express();412413 app.use(function (req, res) {414 res.sendfile('test/fixtures/name.txt');415 });416417 request(app)418 .get('/')419 .expect(200, 'tobi', done);420 });421422 it('should transfer a directory index file', function (done) {423 var app = express();424425 app.use(function (req, res) {426 res.sendfile('test/fixtures/blog/');427 });428429 request(app)430 .get('/')431 .expect(200, '<b>index</b>', done);432 });433434 it('should 404 for directory without trailing slash', function (done) {435 var app = express();436437 app.use(function (req, res) {438 res.sendfile('test/fixtures/blog');439 });440441 request(app)442 .get('/')443 .expect(404, done);444 });445446 it('should transfer a file with urlencoded name', function (done) {447 var app = express();448449 app.use(function (req, res) {450 res.sendfile('test/fixtures/%25%20of%20dogs.txt');451 });452453 request(app)454 .get('/')455 .expect(200, '20%', done);456 });457458 describe('with an absolute path', function(){459 it('should transfer the file', function(done){460 var app = express();461462 app.use(function(req, res){463 res.sendfile(__dirname + '/fixtures/user.html');464 });465466 request(app)467 .get('/')468 .end(function(err, res){469 res.text.should.equal('<p>{{user.name}}</p>');470 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');471 done();472 });473 })474 })475476 describe('with a relative path', function(){477 it('should transfer the file', function(done){478 var app = express();479480 app.use(function(req, res){481 res.sendfile('test/fixtures/user.html');482 });483484 request(app)485 .get('/')486 .end(function(err, res){487 res.text.should.equal('<p>{{user.name}}</p>');488 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');489 done();490 });491 })492493 it('should serve relative to "root"', function(done){494 var app = express();495496 app.use(function(req, res){497 res.sendfile('user.html', { root: 'test/fixtures/' });498 });499500 request(app)501 .get('/')502 .end(function(err, res){503 res.text.should.equal('<p>{{user.name}}</p>');504 res.headers.should.have.property('content-type', 'text/html; charset=UTF-8');505 done();506 });507 })508509 it('should consider ../ malicious when "root" is not set', function(done){510 var app = express();511512 app.use(function(req, res){513 res.sendfile('test/fixtures/foo/../user.html');514 });515516 request(app)517 .get('/')518 .expect(403, done);519 })520521 it('should allow ../ when "root" is set', function(done){522 var app = express();523524 app.use(function(req, res){525 res.sendfile('foo/../user.html', { root: 'test/fixtures' });526 });527528 request(app)529 .get('/')530 .expect(200, done);531 })532533 it('should disallow requesting out of "root"', function(done){534 var app = express();535536 app.use(function(req, res){537 res.sendfile('foo/../../user.html', { root: 'test/fixtures' });538 });539540 request(app)541 .get('/')542 .expect(403, done);543 })544545 it('should next(404) when not found', function(done){546 var app = express()547 , calls = 0;548549 app.use(function(req, res){550 res.sendfile('user.html');551 });552553 app.use(function(req, res){554 assert(0, 'this should not be called');555 });556557 app.use(function(err, req, res, next){558 ++calls;559 next(err);560 });561562 request(app)563 .get('/')564 .end(function(err, res){565 res.statusCode.should.equal(404);566 calls.should.equal(1);567 done();568 });569 })570571 describe('with non-GET', function(){572 it('should still serve', function(done){573 var app = express()574 , calls = 0;575576 app.use(function(req, res){577 res.sendfile(__dirname + '/fixtures/name.txt');578 });579580 request(app)581 .get('/')582 .expect('tobi', done);583 })584 })585 })586 })210})211212function createApp(path, options, fn) {213 var app = express();
7 unmodified lines
modified:History.md+1 added
7 unmodified lines
8 - `req.acceptsLanguage` - use `req.acceptsLanguages`9 - `res.json(obj, status)` signature - use `res.json(status, obj)`10 - `res.jsonp(obj, status)` signature - use `res.jsonp(status, obj)`11 - `res.send(body, status)` signature - use `res.send(status, body)`12 - `res.sendfile` - use `res.sendFile` instead13 - `express.query` middleware14 * change:15 - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname16 - `req.query` is now a getter instead of a plain property
2453 unmodified lines