Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions benchmark/http/set-header-rest.js
Comment thread
araujogui marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

const common = require('../common.js');
const http = require('http');

const bench = common.createBenchmark(main, {
headers: [2, 12],
values: ['static', 'dynamic'],
// 'many' rotates through more unique names than the validation cache holds.
names: ['fixed', 'many'],
duration: 5,
});

const manyNames = [];
for (let i = 0; i < 1024; i++) {
manyNames.push(`X-Custom-Header-${i}`);
}

const restHeaders = [
['Content-Type', 'application/json; charset=utf-8'],
['Cache-Control', 'no-store'],
['X-Content-Type-Options', 'nosniff'],
['X-Frame-Options', 'DENY'],
['Strict-Transport-Security', 'max-age=31536000; includeSubDomains'],
['Access-Control-Allow-Origin', '*'],
['Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'],
['Vary', 'Origin, Accept-Encoding'],
['X-RateLimit-Limit', '1000'],
['X-RateLimit-Remaining', '999'],
['ETag', 'W/"2a-abc"'],
['X-Request-Id', '00000000'],
];

function main({ headers, values, names, duration }) {
const list = restHeaders.slice(0, headers);
const body = '{"ok":true}';
let id = 0;
let next = 0;

const server = http.createServer((req, res) => {
for (const { 0: name, 1: value } of list) {
res.setHeader(
names === 'many' ? manyNames[next++ % manyNames.length] : name,
values === 'dynamic' ? `${value}-${id}` : value,
);
}
id++;
res.end(body);
});

server.listen(0, () => {
bench.http({
path: '/',
connections: 100,
duration,
port: server.address().port,
}, () => {
server.close();
});
});
}
94 changes: 76 additions & 18 deletions lib/internal/http/outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
ObjectKeys,
ObjectSetPrototypeOf,
ObjectValues,
SafeMap,
SafeSet,
Symbol,
} = primordials;
Expand Down Expand Up @@ -760,6 +761,75 @@ const validateHeaderName = assignFunctionName('validateHeaderName', hideStackFra
}
}));

// Only strings are cached: arrays are mutable and must be re-validated.
const kMaxCachedHeaderNames = 256;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So what happens after 256? Lets add a benchmark for this case too. In some networking applications it is very common for there to be over 256 unique header names across all the requests a process might receive.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good question: I implemented a two-generation LRU (256 names per generation), so new names keep getting cached after the first 256, and hits are still a single Map.get().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Only 1 in 8 new names gets admitted, so one-off names don't churn it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the benchmark result include this change too?

const kMaxCachedHeaderValueLength = 256;
// Admit one in eight unseen names, so one-off names do not churn the cache
// and a miss usually costs no allocation or insertion.
const kHeaderAdmissionMask = 7;
// Two-generation approximate LRU: when the current generation fills up it
// becomes the previous one and the old previous one is dropped. Names used
// since the last rotation survive it. A hit costs one Map lookup.
let validatedHeaders = new SafeMap();
let previousValidatedHeaders = new SafeMap();
let headerAdmissionCounter = 0;

class ValidatedHeader {
constructor(name) {
this.lowerCaseName = name.toLowerCase();
this.lastStrictValue = undefined;
this.lastLenientValue = undefined;
}
}

// Returns undefined for a valid name that was not admitted to the cache.
const validateHeaderNameCached = hideStackFrames((name) => {
let header = validatedHeaders.get(name);
if (header !== undefined) return header;

header = previousValidatedHeaders.get(name);
if (header === undefined) {
if (!isValidHeaderName(name)) {
throw new ERR_INVALID_HTTP_TOKEN.HideStackFramesError('Header name', name);
}
headerAdmissionCounter = (headerAdmissionCounter + 1) & kHeaderAdmissionMask;
if (headerAdmissionCounter !== 0) return undefined;
header = new ValidatedHeader(name);
}
if (validatedHeaders.size >= kMaxCachedHeaderNames) {
previousValidatedHeaders = validatedHeaders;
validatedHeaders = new SafeMap();
}
validatedHeaders.set(name, header);
return header;
});

function validateHeaderValueCached(msg, header, name, value) {
if (value === undefined) {
throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
}
const lenient = msg._isLenientHeaderValidation();
if (header !== undefined &&
value === (lenient ? header.lastLenientValue : header.lastStrictValue)) {
return;
}

if (checkInvalidHeaderChar(value, lenient)) {
debug('Header "%s" contains invalid characters', name);
throw new ERR_INVALID_CHAR('header content', name);
}
if (header === undefined || typeof value !== 'string' ||
value.length > kMaxCachedHeaderValueLength) {
return;
}

if (lenient) {
header.lastLenientValue = value;
} else {
header.lastStrictValue = value;
}
}

/**
* Non-throwing counterpart of `validateHeaderName()`.
* @param {any} name
Expand Down Expand Up @@ -822,22 +892,16 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
if (this._header) {
throw new ERR_HTTP_HEADERS_SENT('set');
}
validateHeaderName(name);
if (value === undefined) {
throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
}
if (checkInvalidHeaderChar(value, this._isLenientHeaderValidation())) {
debug('Header "%s" contains invalid characters', name);
throw new ERR_INVALID_CHAR('header content', name);
}
const header = validateHeaderNameCached(name);
validateHeaderValueCached(this, header, name, value);

let headers = this[kOutHeaders];
if (headers === null)
this[kOutHeaders] = headers = new OutgoingHeaders();

// Store a copy of an array value. appendHeader() pushes into the stored
// array, which must not be the caller's (it may also be frozen).
headers[name.toLowerCase()] = [
headers[header !== undefined ? header.lowerCaseName : name.toLowerCase()] = [
name,
ArrayIsArray(value) ? value.slice() : value,
];
Expand Down Expand Up @@ -891,16 +955,10 @@ OutgoingMessage.prototype.appendHeader = function appendHeader(name, value) {
if (this._header) {
throw new ERR_HTTP_HEADERS_SENT('append');
}
validateHeaderName(name);
if (value === undefined) {
throw new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
}
if (checkInvalidHeaderChar(value, this._isLenientHeaderValidation())) {
debug('Header "%s" contains invalid characters', name);
throw new ERR_INVALID_CHAR('header content', name);
}
const header = validateHeaderNameCached(name);
validateHeaderValueCached(this, header, name, value);
const field = header !== undefined ? header.lowerCaseName : name.toLowerCase();

const field = name.toLowerCase();
const headers = this[kOutHeaders];
if (headers === null || !headers[field]) {
return this.setHeader(name, value);
Expand Down
59 changes: 59 additions & 0 deletions test/parallel/test-http-set-header-validation-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

{
// Rotate the name cache through several generations.
const msg = new http.OutgoingMessage();
for (let i = 0; i < 5000; i++) {
msg.setHeader(`X-Many-${i}`, 'v');
}
msg.setHeader('X-Many-0', 'evicted');
msg.setHeader('X-Many-4999', 'cached');
assert.strictEqual(msg.getHeader('x-many-0'), 'evicted');
assert.strictEqual(msg.getHeader('x-many-4999'), 'cached');
// Names that were not admitted to the cache still validate values.
for (let i = 0; i < 8; i++) {
assert.throws(() => {
msg.setHeader(`X-Uncached-${i}`, 'bad\u0007value');
}, { code: 'ERR_INVALID_CHAR' });
}
assert.throws(() => {
msg.setHeader('Bad Header', 'v');
}, { code: 'ERR_INVALID_HTTP_TOKEN' });
}

const server = http.createServer(common.mustCall((req, res) => {
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('X-Test', 'valid-value');
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('X-Test', 'valid-value');

assert.throws(() => {
res.setHeader('X-Test', 'bad\u0007value');
}, { code: 'ERR_INVALID_CHAR' });

const cookies = ['a=1', 'b=2'];
res.setHeader('Set-Cookie', cookies);
cookies[1] = 'bad\r\nvalue';
assert.throws(() => {
res.setHeader('Set-Cookie', cookies);
}, { code: 'ERR_INVALID_CHAR' });
res.setHeader('Set-Cookie', 'ok=1');

assert.throws(() => {
res.setHeader('Bad Header', 'v');
}, { code: 'ERR_INVALID_HTTP_TOKEN' });

res.end('ok');
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
res.resume();
res.on('end', common.mustCall(() => server.close()));
}));
}));
Loading