http: simplify checkIsHttpToken()
Replace code optimized for older versions of V8 with more straightforward code in checkIsHttpToken(). PR-URL: https://github.com/nodejs/node/pull/17399 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
This commit is contained in:
parent
f373a1d814
commit
9f55eac346
@ -233,70 +233,14 @@ function httpSocketSetup(socket) {
|
|||||||
socket.on('drain', ondrain);
|
socket.on('drain', ondrain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
|
||||||
/**
|
/**
|
||||||
* Verifies that the given val is a valid HTTP token
|
* Verifies that the given val is a valid HTTP token
|
||||||
* per the rules defined in RFC 7230
|
* per the rules defined in RFC 7230
|
||||||
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
|
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
|
||||||
*
|
**/
|
||||||
* Allowed characters in an HTTP token:
|
|
||||||
* ^_`a-z 94-122
|
|
||||||
* A-Z 65-90
|
|
||||||
* - 45
|
|
||||||
* 0-9 48-57
|
|
||||||
* ! 33
|
|
||||||
* #$%&' 35-39
|
|
||||||
* *+ 42-43
|
|
||||||
* . 46
|
|
||||||
* | 124
|
|
||||||
* ~ 126
|
|
||||||
*
|
|
||||||
* This implementation of checkIsHttpToken() loops over the string instead of
|
|
||||||
* using a regular expression since the former is up to 180% faster with v8 4.9
|
|
||||||
* depending on the string length (the shorter the string, the larger the
|
|
||||||
* performance difference)
|
|
||||||
*
|
|
||||||
* Additionally, checkIsHttpToken() is currently designed to be inlinable by v8,
|
|
||||||
* so take care when making changes to the implementation so that the source
|
|
||||||
* code size does not exceed v8's default max_inlined_source_size setting.
|
|
||||||
**/
|
|
||||||
var validTokens = [
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
|
|
||||||
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
|
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
|
|
||||||
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
|
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
|
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
|
|
||||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, // 112 - 127
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128 ...
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 255
|
|
||||||
];
|
|
||||||
function checkIsHttpToken(val) {
|
function checkIsHttpToken(val) {
|
||||||
if (!validTokens[val.charCodeAt(0)])
|
return tokenRegExp.test(val);
|
||||||
return false;
|
|
||||||
if (val.length < 2)
|
|
||||||
return true;
|
|
||||||
if (!validTokens[val.charCodeAt(1)])
|
|
||||||
return false;
|
|
||||||
if (val.length < 3)
|
|
||||||
return true;
|
|
||||||
if (!validTokens[val.charCodeAt(2)])
|
|
||||||
return false;
|
|
||||||
if (val.length < 4)
|
|
||||||
return true;
|
|
||||||
if (!validTokens[val.charCodeAt(3)])
|
|
||||||
return false;
|
|
||||||
for (var i = 4; i < val.length; ++i) {
|
|
||||||
if (!validTokens[val.charCodeAt(i)])
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user