Skip to content

Commit d9021c1

Browse files
committed
Retain content-length header for HEAD requests
Fixes hapijs#2540
1 parent 366737e commit d9021c1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/transmit.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ internals.marshal = function (request, next) {
112112
// Set empty stream
113113

114114
response._payload = new internals.Empty();
115-
delete response.headers['content-length'];
115+
if (request.method !== 'head') {
116+
delete response.headers['content-length'];
117+
}
118+
116119
return Auth.response(request, next); // Must be last in case requires access to headers
117120
}
118121

test/transmit.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,25 @@ describe('transmission', function () {
16561656
});
16571657
});
16581658

1659+
it('head request retains content-length header', function (done) {
1660+
1661+
var server = new Hapi.Server();
1662+
server.connection();
1663+
var handler = function (request, reply) {
1664+
1665+
return reply('x').bytes(1);
1666+
};
1667+
1668+
server.route({ method: 'GET', path: '/', handler: handler });
1669+
1670+
server.inject({ method: 'HEAD', url: '/' }, function (res) {
1671+
1672+
expect(res.statusCode).to.equal(200);
1673+
expect(res.headers['content-length']).to.equal(1);
1674+
done();
1675+
});
1676+
});
1677+
16591678
describe('response range', function () {
16601679

16611680
var fileStreamHandler = function (request, reply) {

0 commit comments

Comments
 (0)