Skip to content
Myr

fix(res.send): preserve ETag generation with Transfer-Encoding (#7459)

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

authored by cui fliter · committed by GitHub · parent 3ce6d0e · Browse files · 2 files+21 added−4 removed
This commit

State

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

Trailers

  • Signed-off-by: cuishuang <imcusg@gmail.com>

Diff stats

2 files+21 added−4 removed

  • tests:+15 added−0 removed
  • other:+6 added−4 removed

2 files+21 added−4 removed

modified:lib/response.js+6 added-4 removed
160 unmodified lines
161 // determine if ETag should be generated162 var etagFn = app.get('etag fn')163 var generateETag = !this.get('ETag') && typeof etagFn === 'function'164165 // Because Content-Length and Transfer-Encoding can't be present in the response headers together,166 // Content-Length should be added only if there is no Transfer-Encoding header165 var len;168 if (chunk !== undefined && !this.get('Transfer-Encoding')) {166 if (chunk !== undefined) {167 if (Buffer.isBuffer(chunk)) {168 // get length of Buffer169 len = chunk.length170 } else if (!generateETag && chunk.length < 1000) {
5 unmodified lines
176 encoding = undefined;177 len = chunk.length178 }179180 // Because Content-Length and Transfer-Encoding can't be present in the response headers together,181 // Content-Length should be added only if there is no Transfer-Encoding header182 if (!this.get('Transfer-Encoding')) {182 this.set('Content-Length', len);183 this.set('Content-Length', len);184 }185 }186187 // populate ETag188 var etag;
861 unmodified lines
modified:test/res.send.js+15 added
614 unmodified lines
615 .expect(utils.shouldHaveHeader('Transfer-Encoding'))616 .expect(200, '', done);617 })618 });619620 it('should still generate an ETag', function(done){621 var app = express();622623 app.use(function(_, res){624 res.set('Transfer-Encoding', 'chunked').send('hello');625 });626627 request(app)628 .get('/')629 .expect(utils.shouldNotHaveHeader('Content-Length'))630 .expect(utils.shouldHaveHeader('Transfer-Encoding'))631 .expect('ETag', 'W/"5-qvTGHdzF6KLavt4PO0gs2a6pQ00"')632 .expect(200, 'hello', done);633 })634 })635})