GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http: add isValidHeaderName() and isValidHeaderValue() by jasnell · Pull Request #66334 · nodejs/node

http: add isValidHeaderName() and isValidHeaderValue() #66334

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.

Sign up for GitHub

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

Closed
jasnell wants to merge 2 commits into nodejs:main from jasnell:jasnell/http-header-validators

Uh oh!

There was an error while loading. Please reload this page.

Closed

http: add isValidHeaderName() and isValidHeaderValue() #66334

Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
a58e605
http: add isValidHeaderName() and isValidHeaderValue()
jasnell Sep 27, 2026
1b50a1d
benchmark: add http header validator benchmark
jasnell Sep 27, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
benchmark: add http header validator benchmark
Compare http.isValidHeaderName() and http.isValidHeaderValue() with
http.validateHeaderName() and http.validateHeaderValue() wrapped in
try/catch, for valid and invalid input, and for both 'strict' and
'relaxed' header value validation.

Signed-off-by: James M Snell <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell committed Sep 27, 2026
commit 1b50a1d4a2a4bab095ab252976abaeb456852480

There are no files selected for viewing

120 changes: 120 additions & 0 deletions 120 benchmark/http/is_valid_header.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
'use strict';

// Compares the non-throwing header validators (http.isValidHeaderName() and
// http.isValidHeaderValue()) with the throwing ones (http.validateHeaderName()
// and http.validateHeaderValue()) used inside try/catch.

const common = require('../common.js');
const assert = require('assert');
const {
isValidHeaderName,
isValidHeaderValue,
validateHeaderName,
validateHeaderValue,
} = require('http');

const inputs = {
name: {
valid: [
'ETag', 'date', 'Vary', 'server', 'Expires', 'location', 'Connection',
'content-type', 'Cache-Control', 'content-length', 'x-frame-options',
'Transfer-Encoding', 'x-request-id',
],
invalid: [
'', ':', 'bad header', 'x-forwarded-fםr', '中文呢', '((((())))',
':alternate-protocol', 'alternate-protocol:', 'x\r\ninjected',
],
},
value: {
valid: [
'W/"2-d4cbb29"', 'OK', 'Express', 'application/json',
'application/json; charset=utf-8', 'sessionid=; Path=/',
'text/html; charset=utf-8', '10', 'max-age=0, no-cache', 'gzip, br',
],
// Invalid under both 'strict' and 'relaxed' validation.
invalid: [
'a\r\nb', 'value\n', 'cr\r', 'nul\0byte', 'לא תקין', 'emoji \u{1F600}',
'x'.repeat(64) + '\r\n',
],
},
};

const bench = common.createBenchmark(main, {
method: [
'isValidHeaderName',
'validateHeaderName',
'isValidHeaderValue',
'validateHeaderValue',
],
input: ['valid', 'invalid'],
httpValidation: ['strict', 'relaxed'],
n: [1e6],
}, {
// httpValidation only applies to isValidHeaderValue().
combinationFilter: (p) =>
p.httpValidation === 'strict' || p.method === 'isValidHeaderValue',
});

function main({ n, method, input, httpValidation }) {
let valid = 0;

switch (method) {
case 'isValidHeaderName': {
const list = inputs.name[input];
const len = list.length;
bench.start();
for (let i = 0; i < n; i++) {
if (isValidHeaderName(list[i % len])) valid++;
}
bench.end(n);
break;
}
case 'validateHeaderName': {
const list = inputs.name[input];
const len = list.length;
bench.start();
for (let i = 0; i < n; i++) {
try {
validateHeaderName(list[i % len]);
valid++;
} catch {
// Invalid name.
}
}
bench.end(n);
break;
}
case 'isValidHeaderValue': {
const list = inputs.value[input];
const len = list.length;
const options = httpValidation === 'strict' ? undefined : { httpValidation };
bench.start();
for (let i = 0; i < n; i++) {
if (isValidHeaderValue(list[i % len], options)) valid++;
}
bench.end(n);
break;
}
case 'validateHeaderValue': {
const list = inputs.value[input];
const len = list.length;
bench.start();
for (let i = 0; i < n; i++) {
try {
validateHeaderValue('x-header', list[i % len]);
valid++;
} catch {
// Invalid value.
}
}
bench.end(n);
break;
}
default:
throw new Error(`Unexpected method: ${method}`);
}

// Consume the result so the loop cannot be optimized away, and make sure
// the inputs are what they claim to be.
assert.strictEqual(valid, input === 'valid' ? n : 0);
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.