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
Provenance
- Commit
3387916- Tree
cba011a- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
f76403b
3 files changed+115 added−2 removed
modified:lib/response.js+21 added-2 removed
905 unmodified lines906 907 // pipe the send file stream908 function sendfile(res, file, options, callback) {909 var done = false;910+ var streaming = false;911 912 // directory913 function ondirectory() {914 if (done) return;5 unmodified lines920 }921 922 // errors923 function onerror(err) {924+ if (!err) return;925 if (done) return;926 done = true;927 callback(err);928 }4 unmodified lines933 done = true;934 callback();935 }936 937+ // file938+ function onfile() {939+ onFinished(res, onfinish);940+ }941+942 // finished943 function onfinish(err) {944 if (err) return onerror(err);945 if (done) return;1 unmodified line947 setImmediate(function () {948 if (done) return;949 done = true;950 951+ if (!streaming) {952+ callback();953+ return;954+ }955+956 // response finished before end of file957 var err = new Error('Request aborted');958 err.code = 'ECONNABORT';959 callback(err);960 });961 }962 963+ // streaming964+ function onstream() {965+ streaming = true;966+ }967+968+ 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);974 975 if (options.headers) {976 // set headers on successful transfer977 file.on('headers', function headers(res) {13 unmodified linesmodified:test/res.sendFile.js+93 added
32 unmodified lines33 .get('/')34 .expect(200, '20%', done);35 });36 37+ it('should include ETag', function (done) {38+ var app = createApp(path.resolve(fixtures, 'name.txt'));39+40+ request(app)41+ .get('/')42+ .expect('ETag', /^(?:W\/)?"[^"]+"$/)43+ .expect(200, 'tobi', done);44+ });45+46+ it('should 304 when ETag matches', function (done) {47+ var app = createApp(path.resolve(fixtures, 'name.txt'));48+49+ 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+ });61+62 it('should 404 for directory', function (done) {63 var app = createApp(path.resolve(fixtures, 'blog'));64 65 request(app)170 unmodified lines236 var test = request(app).get('/');237 test.expect(200, cb);238 })239 240+ it('should invoke the callback without error when HEAD', function (done) {241+ var app = express();242+ var cb = after(2, done);243+244+ app.use(function (req, res) {245+ res.sendFile(path.resolve(fixtures, 'name.txt'), cb);246+ });247+248+ request(app)249+ .head('/')250+ .expect(200, cb);251+ });252+253+ it('should invoke the callback without error when 304', function (done) {254+ var app = express();255+ var cb = after(3, done);256+257+ app.use(function (req, res) {258+ res.sendFile(path.resolve(fixtures, 'name.txt'), cb);259+ });260+261+ 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+ });273+274 it('should invoke the callback on 404', function(done){275 var app = express();276 277 app.use(function (req, res) {74 unmodified lines352 var test = request(app).get('/');353 test.expect(200, cb);354 })355 356+ it('should invoke the callback without error when HEAD', function (done) {357+ var app = express();358+ var cb = after(2, done);359+360+ app.use(function (req, res) {361+ res.sendfile('test/fixtures/name.txt', cb);362+ });363+364+ request(app)365+ .head('/')366+ .expect(200, cb);367+ });368+369+ it('should invoke the callback without error when 304', function (done) {370+ var app = express();371+ var cb = after(3, done);372+373+ app.use(function (req, res) {374+ res.sendfile('test/fixtures/name.txt', cb);375+ });376+377+ 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+ });389+390 it('should invoke the callback on 404', function(done){391 var app = express()392 , calls = 0;393 339 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4+ * 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.02533 unmodified lines