Skip to content

Commit 940b530

Browse files
committed
http: use Symbol for outgoing headers
PR-URL: #10941 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 86996c5 commit 940b530

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

lib/_http_client.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
1414
const Agent = require('_http_agent');
1515
const Buffer = require('buffer').Buffer;
1616
const urlToOptions = require('internal/url').urlToOptions;
17+
const outHeadersKey = require('internal/http').outHeadersKey;
1718

1819
// The actual list of disallowed characters in regexp form is more like:
1920
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
@@ -182,7 +183,7 @@ function ClientRequest(options, cb) {
182183
'client');
183184
}
184185
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
185-
self._headers);
186+
self[outHeadersKey]);
186187
}
187188

188189
this._ended = false;
@@ -278,7 +279,7 @@ ClientRequest.prototype._implicitHeader = function _implicitHeader() {
278279
throw new Error('Can\'t render headers after they are sent to the client');
279280
}
280281
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
281-
this._headers);
282+
this[outHeadersKey]);
282283
};
283284

284285
ClientRequest.prototype.abort = function abort() {

lib/_http_outgoing.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const Buffer = require('buffer').Buffer;
99
const common = require('_http_common');
1010
const checkIsHttpToken = common._checkIsHttpToken;
1111
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
12+
const outHeadersKey = require('internal/http').outHeadersKey;
13+
const StorageObject = require('internal/querystring').StorageObject;
1214

1315
const CRLF = common.CRLF;
1416
const debug = common.debug;
@@ -74,7 +76,7 @@ function OutgoingMessage() {
7476
this.socket = null;
7577
this.connection = null;
7678
this._header = null;
77-
this._headers = null;
79+
this[outHeadersKey] = null;
7880

7981
this._onPendingData = null;
8082
}
@@ -201,7 +203,7 @@ function _storeHeader(firstLine, headers) {
201203
var value;
202204
var i;
203205
var j;
204-
if (headers === this._headers) {
206+
if (headers === this[outHeadersKey]) {
205207
for (key in headers) {
206208
var entry = headers[key];
207209
field = entry[0];
@@ -393,11 +395,11 @@ function validateHeader(msg, name, value) {
393395
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
394396
validateHeader(this, name, value);
395397

396-
if (!this._headers)
397-
this._headers = {};
398+
if (!this[outHeadersKey])
399+
this[outHeadersKey] = {};
398400

399401
const key = name.toLowerCase();
400-
this._headers[key] = [name, value];
402+
this[outHeadersKey][key] = [name, value];
401403

402404
switch (key.length) {
403405
case 10:
@@ -421,9 +423,9 @@ OutgoingMessage.prototype.getHeader = function getHeader(name) {
421423
throw new TypeError('"name" argument must be a string');
422424
}
423425

424-
if (!this._headers) return;
426+
if (!this[outHeadersKey]) return;
425427

426-
var entry = this._headers[name.toLowerCase()];
428+
var entry = this[outHeadersKey][name.toLowerCase()];
427429
if (!entry)
428430
return;
429431
return entry[1];
@@ -432,13 +434,13 @@ OutgoingMessage.prototype.getHeader = function getHeader(name) {
432434

433435
// Returns an array of the names of the current outgoing headers.
434436
OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
435-
return (this._headers ? Object.keys(this._headers) : []);
437+
return (this[outHeadersKey] ? Object.keys(this[outHeadersKey]) : []);
436438
};
437439

438440

439441
// Returns a shallow copy of the current outgoing headers.
440442
OutgoingMessage.prototype.getHeaders = function getHeaders() {
441-
const headers = this._headers;
443+
const headers = this[outHeadersKey];
442444
const ret = new OutgoingHeaders();
443445
if (headers) {
444446
const keys = Object.keys(headers);
@@ -457,7 +459,7 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
457459
throw new TypeError('"name" argument must be a string');
458460
}
459461

460-
return !!(this._headers && this._headers[name.toLowerCase()]);
462+
return !!(this[outHeadersKey] && this[outHeadersKey][name.toLowerCase()]);
461463
};
462464

463465

@@ -491,8 +493,8 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
491493
break;
492494
}
493495

494-
if (this._headers) {
495-
delete this._headers[key];
496+
if (this[outHeadersKey]) {
497+
delete this[outHeadersKey][key];
496498
}
497499
};
498500

lib/_http_server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const continueExpression = common.continueExpression;
1313
const chunkExpression = common.chunkExpression;
1414
const httpSocketSetup = common.httpSocketSetup;
1515
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
16+
const outHeadersKey = require('internal/http').outHeadersKey;
1617

1718
const STATUS_CODES = exports.STATUS_CODES = {
1819
100: 'Continue',
@@ -179,7 +180,7 @@ function writeHead(statusCode, reason, obj) {
179180
this.statusCode = statusCode;
180181

181182
var headers;
182-
if (this._headers) {
183+
if (this[outHeadersKey]) {
183184
// Slow-case: when progressive API and header fields are passed.
184185
var k;
185186
if (obj) {
@@ -196,7 +197,7 @@ function writeHead(statusCode, reason, obj) {
196197
}
197198
}
198199
// only progressive api is used
199-
headers = this._headers;
200+
headers = this[outHeadersKey];
200201
} else {
201202
// only writeHead() called
202203
headers = obj;

lib/internal/http.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
outHeadersKey: Symbol('outHeadersKey')
5+
};

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
'lib/internal/errors.js',
8686
'lib/internal/freelist.js',
8787
'lib/internal/fs.js',
88+
'lib/internal/http.js',
8889
'lib/internal/linkedlist.js',
8990
'lib/internal/net.js',
9091
'lib/internal/module.js',

0 commit comments

Comments
 (0)