Skip to content
Myr

fix(res.send): add Content-Length header only if Transfer-Encoding is not present (#4893)

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

* fix(respond): add Content-Length header only if Transfer-Encoding is not present * fix(response): used get('Transfer-Encoding') * test(res.send): covered Transfer-Encoding and Content-Length headers logic * chore(response): removed comment * chore(res.redirect): revert changes to focus on res.send * test(res.send): added tests for all transfer encodings * docs: update History.md to include bug fix for HTTP header conflict in res.send() ---------

authored by Yury Shkoda · committed by GitHub · parent 59e205a · Browse files · 3 files+36 added−3 removed
This commit

State

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

Trailers

  • Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>

Diff stats

3 files+36 added−3 removed

  • tests:+25 added−0 removed
  • other:+11 added−3 removed

3 files+36 added−3 removed

modified:lib/response.js+4 added-3 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 // populate Content-Length166 var len167 if (chunk !== undefined) {165 // 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 header167 var len;168 if (chunk !== undefined && !this.get('Transfer-Encoding')) {169 if (Buffer.isBuffer(chunk)) {170 // get length of Buffer171 len = chunk.length172 } else if (!generateETag && chunk.length < 1000) {
878 unmodified lines
modified:test/res.send.js+25 added
590 unmodified lines
591 .expect(200, done);592 })593 })594 })595596 describe('when Transfer-Encoding header is present', function(){597 var transferEncodings = [598 'chunked',599 'compress',600 'deflate',601 'gzip'602 ];603604 transferEncodings.forEach(function(encoding){605 it('should not add Content-Length header if Transfer-Encoding header is equal to ' + encoding, function(done){606 var app = express();607608 app.use(function(_, res){609 res.status(200).set('Transfer-Encoding', encoding).send('');610 });611612 request(app)613 .get('/')614 .expect(utils.shouldNotHaveHeader('Content-Length'))615 .expect(utils.shouldHaveHeader('Transfer-Encoding'))616 .expect(200, '', done);617 })618 });619 })620})
modified:History.md+7 added
1# Unreleased Changes23## 🐞 Bug fixes45- Fixed HTTP header conflict between Content-Length and Transfer-Encoding in res.send - by [@YuryShkoda](https://github.com/YuryShkoda) in [#4893](https://github.com/expressjs/express/pull/4893)678 Fixed the behavior of `res.send()` to prevent conflicts between `Content-Length` and `Transfer-Encoding` HTTP headers in responses. The `Content-Length` header in `res.send()` is now only added when a `Transfer-Encoding` header is not present, complying with the HTTP specification that states both headers should not coexist in the same response910## 🚀 Improvements1112* Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding `<!DOCTYPE html>`, `<title>`, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167)13
3898 unmodified lines