Skip to content

Commit 5555318

Browse files
James Hallidayisaacs
James Halliday
authored andcommitted
http: removed headers stay removed
This allows automatically-inserted headers to be removed permanently by calling OutgoingMessage.removeHeader() on them, as if they were normal headers.
1 parent fe0f12b commit 5555318

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

lib/_http_outgoing.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ var contentLengthExpression = /Content-Length/i;
3838
var dateExpression = /Date/i;
3939
var expectExpression = /Expect/i;
4040

41+
var automaticHeaders = {
42+
connection: true,
43+
'content-length': true,
44+
'transfer-encoding': true,
45+
date: true
46+
};
47+
4148

4249
var dateCache;
4350
function utcDate() {
@@ -68,6 +75,7 @@ function OutgoingMessage() {
6875
this.shouldKeepAlive = true;
6976
this.useChunkedEncodingByDefault = true;
7077
this.sendDate = false;
78+
this._removedHeader = {};
7179

7280
this._hasBody = true;
7381
this._trailer = '';
@@ -245,7 +253,10 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
245253
}
246254

247255
// keep-alive logic
248-
if (state.sentConnectionHeader === false) {
256+
if (this._removedHeader.connection) {
257+
this._last = true;
258+
this.shouldKeepAlive = false;
259+
} else if (state.sentConnectionHeader === false) {
249260
var shouldSendKeepAlive = this.shouldKeepAlive &&
250261
(state.sentContentLengthHeader ||
251262
this.useChunkedEncodingByDefault ||
@@ -260,7 +271,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
260271

261272
if (state.sentContentLengthHeader == false &&
262273
state.sentTransferEncodingHeader == false) {
263-
if (this._hasBody) {
274+
if (this._hasBody && !this._removedHeader['transfer-encoding']) {
264275
if (this.useChunkedEncodingByDefault) {
265276
state.messageHeader += 'Transfer-Encoding: chunked\r\n';
266277
this.chunkedEncoding = true;
@@ -325,6 +336,10 @@ OutgoingMessage.prototype.setHeader = function(name, value) {
325336
this._headerNames = this._headerNames || {};
326337
this._headers[key] = value;
327338
this._headerNames[key] = name;
339+
340+
if (automaticHeaders[key]) {
341+
this._removedHeader[key] = false;
342+
}
328343
};
329344

330345

@@ -349,11 +364,17 @@ OutgoingMessage.prototype.removeHeader = function(name) {
349364
throw new Error('Can\'t remove headers after they are sent.');
350365
}
351366

352-
if (!this._headers) return;
353-
354367
var key = name.toLowerCase();
355-
delete this._headers[key];
356-
delete this._headerNames[key];
368+
369+
if (key === 'date')
370+
this.sendDate = false;
371+
else if (automaticHeaders[key])
372+
this._removedHeader[key] = true;
373+
374+
if (this._headers) {
375+
delete this._headers[key];
376+
delete this._headerNames[key];
377+
}
357378
};
358379

359380

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var http = require('http');
26+
27+
var server = http.createServer(function(request, response) {
28+
// removed headers should stay removed, even if node automatically adds them
29+
// to the output:
30+
response.removeHeader('connection');
31+
response.removeHeader('transfer-encoding');
32+
33+
// make sure that removing and then setting still works:
34+
response.removeHeader('date');
35+
response.setHeader('date', 'coffee o clock');
36+
37+
response.end('beep boop\n');
38+
39+
this.close();
40+
});
41+
42+
var response = '';
43+
44+
process.on('exit', function() {
45+
assert.equal('beep boop\n', response);
46+
console.log('ok');
47+
});
48+
49+
server.listen(common.PORT, function() {
50+
http.get({ port: common.PORT }, function(res) {
51+
assert.equal(200, res.statusCode);
52+
assert.deepEqual(res.headers, { date : 'coffee o clock' });
53+
54+
res.setEncoding('ascii');
55+
res.on('data', function(chunk) {
56+
response += chunk;
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)