Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 758271a

Browse filesBrowse files
JinhyeokFangaduh95
authored andcommitted
http: optimize checkIsHttpToken for short strings
Use lookup table instead of regex for strings shorter than 10 characters to improve performance for common short header names while maintaining compatibility. PR-URL: #59832 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent a71dd59 commit 758271a
Copy full SHA for 758271a

File tree

Expand file treeCollapse file tree

1 file changed

+37
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+37
-1
lines changed
Open diff view settings
Collapse file

‎lib/_http_common.js‎

Copy file name to clipboardExpand all lines: lib/_http_common.js
+37-1Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const {
2525
MathMin,
2626
Symbol,
27+
Uint8Array,
2728
} = primordials;
2829
const { setImmediate } = require('timers');
2930

@@ -205,14 +206,49 @@ function freeParser(parser, req, socket) {
205206
}
206207
}
207208

209+
// Character code ranges for valid HTTP tokens
210+
// Valid chars: ^_`a-zA-Z-0-9!#$%&'*+.|~
211+
// Based on RFC 7230 Section 3.2.6 token definition
212+
// See https://tools.ietf.org/html/rfc7230#section-3.2.6
208213
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
214+
const validTokenChars = new Uint8Array([
215+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15
216+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
217+
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32-47 (!"#$%&'()*+,-./)
218+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48-63 (0-9:;<=>?)
219+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64-79 (@A-O)
220+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80-95 (P-Z[\]^_)
221+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96-111 (`a-o)
222+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, // 112-127 (p-z{|}~)
223+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128-143
224+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144-159
225+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160-175
226+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 176-191
227+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192-207
228+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 208-223
229+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 224-239
230+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 240-255
231+
]);
232+
209233
/**
210234
* Verifies that the given val is a valid HTTP token
211235
* per the rules defined in RFC 7230
212236
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
213237
*/
214238
function checkIsHttpToken(val) {
215-
return tokenRegExp.test(val);
239+
if (val.length >= 10) {
240+
return tokenRegExp.test(val);
241+
}
242+
243+
if (val.length === 0) return false;
244+
245+
// Use lookup table for short strings, regex for longer ones
246+
for (let i = 0; i < val.length; i++) {
247+
if (!validTokenChars[val.charCodeAt(i)]) {
248+
return false;
249+
}
250+
}
251+
return true;
216252
}
217253

218254
const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.