Skip to content
Myr

Fix incorrect "Request aborted" for res.sendFile when HEAD or 304

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

fixes #2481

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

  • tests:+93 added−0 removed
  • other:+22 added−2 removed

3 files+115 added−2 removed

modified:lib/response.js+21 added-2 removed
905 unmodified lines
906907// pipe the send file stream908function sendfile(res, file, options, callback) {909 var done = false;910 var streaming = false;911912 // directory913 function ondirectory() {914 if (done) return;
5 unmodified lines
920 }921922 // errors923 function onerror(err) {924 if (!err) return;925 if (done) return;926 done = true;927 callback(err);928 }
4 unmodified lines
933 done = true;934 callback();935 }936937 // file938 function onfile() {939 onFinished(res, onfinish);940 }941942 // finished943 function onfinish(err) {944 if (err) return onerror(err);945 if (done) return;
1 unmodified line
947 setImmediate(function () {948 if (done) return;949 done = true;950951 if (!streaming) {952 callback();953 return;954 }955956 // response finished before end of file957 var err = new Error('Request aborted');958 err.code = 'ECONNABORT';959 callback(err);960 });961 }962963 // streaming964 function onstream() {965 streaming = true;966 }967968 file.on('directory', ondirectory);969 file.on('end', onend);970 file.on('error', onerror);953 file.on('directory', ondirectory);954 onFinished(res, onfinish);971 file.on('file', onfile);972 file.on('stream', onstream);973 onFinished(res, onerror);974975 if (options.headers) {976 // set headers on successful transfer977 file.on('headers', function headers(res) {
13 unmodified lines
modified:test/res.sendFile.js+93 added
32 unmodified lines
33 .get('/')34 .expect(200, '20%', done);35 });3637 it('should include ETag', function (done) {38 var app = createApp(path.resolve(fixtures, 'name.txt'));3940 request(app)41 .get('/')42 .expect('ETag', /^(?:W\/)?"[^"]+"$/)43 .expect(200, 'tobi', done);44 });4546 it('should 304 when ETag matches', function (done) {47 var app = createApp(path.resolve(fixtures, 'name.txt'));4849 request(app)50 .get('/')51 .expect('ETag', /^(?:W\/)?"[^"]+"$/)52 .expect(200, 'tobi', function (err, res) {53 if (err) return done(err);54 var etag = res.headers.etag;55 request(app)56 .get('/')57 .set('If-None-Match', etag)58 .expect(304, done);59 });60 });6162 it('should 404 for directory', function (done) {63 var app = createApp(path.resolve(fixtures, 'blog'));6465 request(app)
170 unmodified lines
236 var test = request(app).get('/');237 test.expect(200, cb);238 })239240 it('should invoke the callback without error when HEAD', function (done) {241 var app = express();242 var cb = after(2, done);243244 app.use(function (req, res) {245 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);246 });247248 request(app)249 .head('/')250 .expect(200, cb);251 });252253 it('should invoke the callback without error when 304', function (done) {254 var app = express();255 var cb = after(3, done);256257 app.use(function (req, res) {258 res.sendFile(path.resolve(fixtures, 'name.txt'), cb);259 });260261 request(app)262 .get('/')263 .expect('ETag', /^(?:W\/)?"[^"]+"$/)264 .expect(200, 'tobi', function (err, res) {265 if (err) return cb(err);266 var etag = res.headers.etag;267 request(app)268 .get('/')269 .set('If-None-Match', etag)270 .expect(304, cb);271 });272 });273274 it('should invoke the callback on 404', function(done){275 var app = express();276277 app.use(function (req, res) {
74 unmodified lines
352 var test = request(app).get('/');353 test.expect(200, cb);354 })355356 it('should invoke the callback without error when HEAD', function (done) {357 var app = express();358 var cb = after(2, done);359360 app.use(function (req, res) {361 res.sendfile('test/fixtures/name.txt', cb);362 });363364 request(app)365 .head('/')366 .expect(200, cb);367 });368369 it('should invoke the callback without error when 304', function (done) {370 var app = express();371 var cb = after(3, done);372373 app.use(function (req, res) {374 res.sendfile('test/fixtures/name.txt', cb);375 });376377 request(app)378 .get('/')379 .expect('ETag', /^(?:W\/)?"[^"]+"$/)380 .expect(200, 'tobi', function (err, res) {381 if (err) return cb(err);382 var etag = res.headers.etag;383 request(app)384 .get('/')385 .set('If-None-Match', etag)386 .expect(304, cb);387 });388 });389390 it('should invoke the callback on 404', function(done){391 var app = express()392 , calls = 0;393
339 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Fix incorrect "Request aborted" for `res.sendFile` when `HEAD` or 3045 * deps: debug@~2.1.16 * deps: finalhandler@0.3.37 - deps: debug@~2.1.18 - deps: on-finished@~2.2.0
2533 unmodified lines