Permalink
Browse files

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>
  • Loading branch information...
mscdex committed Mar 6, 2017
1 parent 86996c5 commit 940b5303bef7aee9b24214c62e4b6f182f23f82a
Showing with 26 additions and 16 deletions.
  1. +3 −2 lib/_http_client.js
  2. +14 −12 lib/_http_outgoing.js
  3. +3 −2 lib/_http_server.js
  4. +5 −0 lib/internal/http.js
  5. +1 −0 node.gyp
View
@@ -14,6 +14,7 @@ const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const Agent = require('_http_agent');
const Buffer = require('buffer').Buffer;
const urlToOptions = require('internal/url').urlToOptions;
const outHeadersKey = require('internal/http').outHeadersKey;
// The actual list of disallowed characters in regexp form is more like:
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
@@ -182,7 +183,7 @@ function ClientRequest(options, cb) {
'client');
}
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
self._headers);
self[outHeadersKey]);
}
this._ended = false;
@@ -278,7 +279,7 @@ ClientRequest.prototype._implicitHeader = function _implicitHeader() {
throw new Error('Can\'t render headers after they are sent to the client');
}
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
this._headers);
this[outHeadersKey]);
};
ClientRequest.prototype.abort = function abort() {
View
@@ -9,6 +9,8 @@ const Buffer = require('buffer').Buffer;
const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
const outHeadersKey = require('internal/http').outHeadersKey;
const StorageObject = require('internal/querystring').StorageObject;
const CRLF = common.CRLF;
const debug = common.debug;
@@ -74,7 +76,7 @@ function OutgoingMessage() {
this.socket = null;
this.connection = null;
this._header = null;
this._headers = null;
this[outHeadersKey] = null;
this._onPendingData = null;
}
@@ -201,7 +203,7 @@ function _storeHeader(firstLine, headers) {
var value;
var i;
var j;
if (headers === this._headers) {
if (headers === this[outHeadersKey]) {
for (key in headers) {
var entry = headers[key];
field = entry[0];
@@ -393,11 +395,11 @@ function validateHeader(msg, name, value) {
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
validateHeader(this, name, value);
if (!this._headers)
this._headers = {};
if (!this[outHeadersKey])
this[outHeadersKey] = {};
const key = name.toLowerCase();
this._headers[key] = [name, value];
this[outHeadersKey][key] = [name, value];
switch (key.length) {
case 10:
@@ -421,9 +423,9 @@ OutgoingMessage.prototype.getHeader = function getHeader(name) {
throw new TypeError('"name" argument must be a string');
}
if (!this._headers) return;
if (!this[outHeadersKey]) return;
var entry = this._headers[name.toLowerCase()];
var entry = this[outHeadersKey][name.toLowerCase()];
if (!entry)
return;
return entry[1];
@@ -432,13 +434,13 @@ OutgoingMessage.prototype.getHeader = function getHeader(name) {
// Returns an array of the names of the current outgoing headers.
OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
return (this._headers ? Object.keys(this._headers) : []);
return (this[outHeadersKey] ? Object.keys(this[outHeadersKey]) : []);
};
// Returns a shallow copy of the current outgoing headers.
OutgoingMessage.prototype.getHeaders = function getHeaders() {
const headers = this._headers;
const headers = this[outHeadersKey];
const ret = new OutgoingHeaders();
if (headers) {
const keys = Object.keys(headers);
@@ -457,7 +459,7 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
throw new TypeError('"name" argument must be a string');
}
return !!(this._headers && this._headers[name.toLowerCase()]);
return !!(this[outHeadersKey] && this[outHeadersKey][name.toLowerCase()]);
};
@@ -491,8 +493,8 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
break;
}
if (this._headers) {
delete this._headers[key];
if (this[outHeadersKey]) {
delete this[outHeadersKey][key];
}
};
View
@@ -13,6 +13,7 @@ const continueExpression = common.continueExpression;
const chunkExpression = common.chunkExpression;
const httpSocketSetup = common.httpSocketSetup;
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;
const outHeadersKey = require('internal/http').outHeadersKey;
const STATUS_CODES = exports.STATUS_CODES = {
100: 'Continue',
@@ -179,7 +180,7 @@ function writeHead(statusCode, reason, obj) {
this.statusCode = statusCode;
var headers;
if (this._headers) {
if (this[outHeadersKey]) {
// Slow-case: when progressive API and header fields are passed.
var k;
if (obj) {
@@ -196,7 +197,7 @@ function writeHead(statusCode, reason, obj) {
}
}
// only progressive api is used
headers = this._headers;
headers = this[outHeadersKey];
} else {
// only writeHead() called
headers = obj;
View
@@ -0,0 +1,5 @@
'use strict';
module.exports = {
outHeadersKey: Symbol('outHeadersKey')
};
View
@@ -85,6 +85,7 @@
'lib/internal/errors.js',
'lib/internal/freelist.js',
'lib/internal/fs.js',
'lib/internal/http.js',
'lib/internal/linkedlist.js',
'lib/internal/net.js',
'lib/internal/module.js',

0 comments on commit 940b530

Please sign in to comment.