Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not auto-remove content headers for body-less status codes #5320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/response.js
Expand Up @@ -209,23 +209,26 @@ res.send = function send(body) {
// freshness
if (req.fresh) this.statusCode = 304;

// strip irrelevant headers
if (204 === this.statusCode || 304 === this.statusCode) {
// remove content headers for 204
if (this.statusCode === 204) {
this.removeHeader('Content-Type');
this.removeHeader('Content-Length');
this.removeHeader('Transfer-Encoding');
chunk = '';
}

// alter headers for 205
if (this.statusCode === 205) {
this.set('Content-Length', '0')
this.removeHeader('Transfer-Encoding')
chunk = ''
}

if (req.method === 'HEAD') {
// skip body for HEAD
if (
req.method === 'HEAD' ||
this.statusCode === 204 ||
this.statusCode === 205 ||
this.statusCode === 304
) {
// skip body
this.end();
} else {
// respond
Expand Down
9 changes: 4 additions & 5 deletions test/res.send.js
Expand Up @@ -300,7 +300,7 @@ describe('res', function(){
})

describe('when .statusCode is 304', function(){
it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){
it('should ignore the body', function(done){
var app = express();

app.use(function(req, res){
Expand All @@ -309,10 +309,9 @@ describe('res', function(){

request(app)
.get('/')
.expect(utils.shouldNotHaveHeader('Content-Type'))
.expect(utils.shouldNotHaveHeader('Content-Length'))
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
.expect(304, '', done);
.expect(304)
.expect(utils.shouldNotHaveBody())
.end(done);
})
})

Expand Down