Skip to content

Commit b58725c

Browse files
ronagBethGriggs
authored andcommitted
http: lazy create IncomingMessage.headers
When rawHeaders is enough don't create the headers object. PR-URL: #35281 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0de3f56 commit b58725c

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

lib/_http_incoming.js

+56-6
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@
2424
const {
2525
ObjectDefineProperty,
2626
ObjectSetPrototypeOf,
27+
Symbol
2728
} = primordials;
2829

2930
const Stream = require('stream');
3031

32+
const kHeaders = Symbol('kHeaders');
33+
const kHeadersCount = Symbol('kHeadersCount');
34+
const kTrailers = Symbol('kTrailers');
35+
const kTrailersCount = Symbol('kTrailersCount');
36+
3137
function readStart(socket) {
3238
if (socket && !socket._paused && socket.readable)
3339
socket.resume();
@@ -58,9 +64,11 @@ function IncomingMessage(socket) {
5864
this.httpVersionMinor = null;
5965
this.httpVersion = null;
6066
this.complete = false;
61-
this.headers = {};
67+
this[kHeaders] = null;
68+
this[kHeadersCount] = 0;
6269
this.rawHeaders = [];
63-
this.trailers = {};
70+
this[kTrailers] = null;
71+
this[kTrailersCount] = 0;
6472
this.rawTrailers = [];
6573

6674
this.aborted = false;
@@ -93,6 +101,44 @@ ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
93101
}
94102
});
95103

104+
ObjectDefineProperty(IncomingMessage.prototype, 'headers', {
105+
get: function() {
106+
if (!this[kHeaders]) {
107+
this[kHeaders] = {};
108+
109+
const src = this.rawHeaders;
110+
const dst = this[kHeaders];
111+
112+
for (let n = 0; n < this[kHeadersCount]; n += 2) {
113+
this._addHeaderLine(src[n + 0], src[n + 1], dst);
114+
}
115+
}
116+
return this[kHeaders];
117+
},
118+
set: function(val) {
119+
this[kHeaders] = val;
120+
}
121+
});
122+
123+
ObjectDefineProperty(IncomingMessage.prototype, 'trailers', {
124+
get: function() {
125+
if (!this[kTrailers]) {
126+
this[kTrailers] = {};
127+
128+
const src = this.rawTrailers;
129+
const dst = this[kTrailers];
130+
131+
for (let n = 0; n < this[kTrailersCount]; n += 2) {
132+
this._addHeaderLine(src[n + 0], src[n + 1], dst);
133+
}
134+
}
135+
return this[kTrailers];
136+
},
137+
set: function(val) {
138+
this[kTrailers] = val;
139+
}
140+
});
141+
96142
IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
97143
if (callback)
98144
this.on('timeout', callback);
@@ -131,14 +177,18 @@ function _addHeaderLines(headers, n) {
131177
let dest;
132178
if (this.complete) {
133179
this.rawTrailers = headers;
134-
dest = this.trailers;
180+
this[kTrailersCount] = n;
181+
dest = this[kTrailers];
135182
} else {
136183
this.rawHeaders = headers;
137-
dest = this.headers;
184+
this[kHeadersCount] = n;
185+
dest = this[kHeaders];
138186
}
139187

140-
for (let i = 0; i < n; i += 2) {
141-
this._addHeaderLine(headers[i], headers[i + 1], dest);
188+
if (dest) {
189+
for (let i = 0; i < n; i += 2) {
190+
this._addHeaderLine(headers[i], headers[i + 1], dest);
191+
}
142192
}
143193
}
144194
}

0 commit comments

Comments
 (0)