Skip to content

Commit

Permalink
http2: use Object.create(null) for getHeaders
Browse files Browse the repository at this point in the history
refactor `getHeaders` to initialize headers using
`Object.create(null)`

Refs: #29829

PR-URL: #33188
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sebastiaan Deckers <sebdeckers83@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
rexagod authored and codebytere committed Jun 18, 2020
1 parent e83642f commit cc74f3c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ class Http2ServerResponse extends Stream {
}

getHeaders() {
return { ...this[kHeaders] };
const headers = ObjectCreate(null);
return ObjectAssign(headers, this[kHeaders]);
}

hasHeader(name) {
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-compat-serverresponse-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ server.listen(0, common.mustCall(function() {
response.setHeader(real, expectedValue);
const expectedHeaderNames = [real];
assert.deepStrictEqual(response.getHeaderNames(), expectedHeaderNames);
const expectedHeaders = { [real]: expectedValue };
const expectedHeaders = Object.create(null);
expectedHeaders[real] = expectedValue;
assert.deepStrictEqual(response.getHeaders(), expectedHeaders);

response.getHeaders()[fake] = fake;
assert.strictEqual(response.hasHeader(fake), false);
assert.strictEqual(Object.getPrototypeOf(response.getHeaders()), null);

assert.strictEqual(response.sendDate, true);
response.sendDate = false;
Expand Down

0 comments on commit cc74f3c

Please sign in to comment.