Skip to content

Commit 7a02161

Browse files
LJ1102Jan Scheurer
authored and
Jan Scheurer
committed
Do not remove content headers for 304
According to [RFC 2616 (HTTP/1.1 spec)](https://datatracker.ietf.org/doc/html/rfc2616#page-54) a `HEAD` request is supposed to return *exactly* the same entity-headers as a `GET` request would but without body. [RFC 7230](https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2) further specifies that: "Transfer-Encoding MAY be sent in a response to a HEAD request or in a 304 (Not Modified) response" and "A server MAY send a Content-Length header field in a 304 (Not Modified) response to a conditional GET request"
1 parent 2a00da2 commit 7a02161

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/response.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,26 @@ res.send = function send(body) {
209209
// freshness
210210
if (req.fresh) this.statusCode = 304;
211211

212-
// strip irrelevant headers
213-
if (204 === this.statusCode || 304 === this.statusCode) {
212+
// remove content headers for 204
213+
if (this.statusCode === 204) {
214214
this.removeHeader('Content-Type');
215215
this.removeHeader('Content-Length');
216216
this.removeHeader('Transfer-Encoding');
217-
chunk = '';
218217
}
219218

220219
// alter headers for 205
221220
if (this.statusCode === 205) {
222221
this.set('Content-Length', '0')
223222
this.removeHeader('Transfer-Encoding')
224-
chunk = ''
225223
}
226224

227-
if (req.method === 'HEAD') {
228-
// skip body for HEAD
225+
if (
226+
req.method === 'HEAD' ||
227+
this.statusCode === 204 ||
228+
this.statusCode === 205 ||
229+
this.statusCode === 304
230+
) {
231+
// skip body
229232
this.end();
230233
} else {
231234
// respond

test/res.send.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ describe('res', function(){
300300
})
301301

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

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

310310
request(app)
311311
.get('/')
312-
.expect(utils.shouldNotHaveHeader('Content-Type'))
313-
.expect(utils.shouldNotHaveHeader('Content-Length'))
314-
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
315-
.expect(304, '', done);
312+
.expect(304)
313+
.expect(utils.shouldNotHaveBody())
314+
.end(done);
316315
})
317316
})
318317

0 commit comments

Comments
 (0)