Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: lazy create IncomingMessage.headers #35281

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 56 additions & 6 deletions lib/_http_incoming.js
Expand Up @@ -24,10 +24,16 @@
const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
Symbol
} = primordials;

const Stream = require('stream');

const kHeaders = Symbol('kHeaders');
const kHeadersCount = Symbol('kHeadersCount');
const kTrailers = Symbol('kTrailers');
const kTrailersCount = Symbol('kTrailersCount');

function readStart(socket) {
if (socket && !socket._paused && socket.readable)
socket.resume();
Expand Down Expand Up @@ -58,9 +64,11 @@ function IncomingMessage(socket) {
this.httpVersionMinor = null;
this.httpVersion = null;
this.complete = false;
this.headers = {};
this[kHeaders] = null;
this[kHeadersCount] = 0;
this.rawHeaders = [];
this.trailers = {};
this[kTrailers] = null;
this[kTrailersCount] = 0;
this.rawTrailers = [];

this.aborted = false;
Expand Down Expand Up @@ -93,6 +101,44 @@ ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
}
});

ObjectDefineProperty(IncomingMessage.prototype, 'headers', {
get: function() {
if (!this[kHeaders]) {
this[kHeaders] = {};

const src = this.rawHeaders;
const dst = this[kHeaders];

for (let n = 0; n < this[kHeadersCount]; n += 2) {
this._addHeaderLine(src[n + 0], src[n + 1], dst);
}
}
return this[kHeaders];
},
set: function(val) {
this[kHeaders] = val;
}
});

ObjectDefineProperty(IncomingMessage.prototype, 'trailers', {
get: function() {
if (!this[kTrailers]) {
this[kTrailers] = {};

const src = this.rawTrailers;
const dst = this[kTrailers];

for (let n = 0; n < this[kTrailersCount]; n += 2) {
this._addHeaderLine(src[n + 0], src[n + 1], dst);
}
}
return this[kTrailers];
},
set: function(val) {
this[kTrailers] = val;
}
});

IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback)
this.on('timeout', callback);
Expand Down Expand Up @@ -133,14 +179,18 @@ function _addHeaderLines(headers, n) {
let dest;
if (this.complete) {
this.rawTrailers = headers;
dest = this.trailers;
this[kTrailersCount] = n;
dest = this[kTrailers];
} else {
this.rawHeaders = headers;
dest = this.headers;
this[kHeadersCount] = n;
dest = this[kHeaders];
}

for (let i = 0; i < n; i += 2) {
this._addHeaderLine(headers[i], headers[i + 1], dest);
if (dest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (dest) {
if (dest !== null) {

for (let i = 0; i < n; i += 2) {
this._addHeaderLine(headers[i], headers[i + 1], dest);
}
}
}
}
Expand Down