Skip to content

Commit 24dc57b

Browse files
TrottMylesBorins
authored andcommitted
http: simplify checkIsHttpToken()
Replace code optimized for older versions of V8 with more straightforward code in checkIsHttpToken(). PR-URL: #17399 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Timothy Gu <[email protected]>
1 parent bb780d2 commit 24dc57b

File tree

1 file changed

+3
-59
lines changed

1 file changed

+3
-59
lines changed

lib/_http_common.js

+3-59
Original file line numberDiff line numberDiff line change
@@ -233,70 +233,14 @@ function httpSocketSetup(socket) {
233233
socket.on('drain', ondrain);
234234
}
235235

236+
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
236237
/**
237238
* Verifies that the given val is a valid HTTP token
238239
* per the rules defined in RFC 7230
239240
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
240-
*
241-
* Allowed characters in an HTTP token:
242-
* ^_`a-z 94-122
243-
* A-Z 65-90
244-
* - 45
245-
* 0-9 48-57
246-
* ! 33
247-
* #$%&' 35-39
248-
* *+ 42-43
249-
* . 46
250-
* | 124
251-
* ~ 126
252-
*
253-
* This implementation of checkIsHttpToken() loops over the string instead of
254-
* using a regular expression since the former is up to 180% faster with v8 4.9
255-
* depending on the string length (the shorter the string, the larger the
256-
* performance difference)
257-
*
258-
* Additionally, checkIsHttpToken() is currently designed to be inlinable by v8,
259-
* so take care when making changes to the implementation so that the source
260-
* code size does not exceed v8's default max_inlined_source_size setting.
261-
**/
262-
var validTokens = [
263-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
264-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
265-
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
266-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
267-
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
268-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
269-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
270-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, // 112 - 127
271-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128 ...
272-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
273-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
274-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
275-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
276-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
277-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 255
279-
];
241+
**/
280242
function checkIsHttpToken(val) {
281-
if (!validTokens[val.charCodeAt(0)])
282-
return false;
283-
if (val.length < 2)
284-
return true;
285-
if (!validTokens[val.charCodeAt(1)])
286-
return false;
287-
if (val.length < 3)
288-
return true;
289-
if (!validTokens[val.charCodeAt(2)])
290-
return false;
291-
if (val.length < 4)
292-
return true;
293-
if (!validTokens[val.charCodeAt(3)])
294-
return false;
295-
for (var i = 4; i < val.length; ++i) {
296-
if (!validTokens[val.charCodeAt(i)])
297-
return false;
298-
}
299-
return true;
243+
return tokenRegExp.test(val);
300244
}
301245

302246
/**

0 commit comments

Comments
 (0)