http: improve validation performance

The new table-based lookups perform significantly better for the
common cases (checking latin1 characters).

PR-URL: https://github.com/nodejs/node/pull/6533
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Brian White 2016-12-18 21:21:57 -05:00
parent 832271c88f
commit a54972c195
No known key found for this signature in database
GPG Key ID: 606D7358F94DA209

View File

@ -246,44 +246,44 @@ exports.httpSocketSetup = httpSocketSetup;
* so take care when making changes to the implementation so that the source * 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. * code size does not exceed v8's default max_inlined_source_size setting.
**/ **/
function isValidTokenChar(ch) { var validTokens = [
if (ch >= 94 && ch <= 122) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
return true; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
if (ch >= 65 && ch <= 90) 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
return true; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
if (ch === 45) 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
return true; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
if (ch >= 48 && ch <= 57) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
return true; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, // 112 - 127
if (ch === 34 || ch === 40 || ch === 41 || ch === 44) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128 ...
return false; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
if (ch >= 33 && ch <= 46) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
return true; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
if (ch === 124 || ch === 126) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
return true; 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
return false; 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 (typeof val !== 'string' || val.length === 0) if (typeof val !== 'string' || val.length === 0)
return false; return false;
if (!isValidTokenChar(val.charCodeAt(0))) if (!validTokens[val.charCodeAt(0)])
return false; return false;
const len = val.length; if (val.length < 2)
if (len > 1) { return true;
if (!isValidTokenChar(val.charCodeAt(1))) 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 false;
if (len > 2) {
if (!isValidTokenChar(val.charCodeAt(2)))
return false;
if (len > 3) {
if (!isValidTokenChar(val.charCodeAt(3)))
return false;
for (var i = 4; i < len; i++) {
if (!isValidTokenChar(val.charCodeAt(i)))
return false;
}
}
}
} }
return true; return true;
} }
@ -299,26 +299,44 @@ exports._checkIsHttpToken = checkIsHttpToken;
* so take care when making changes to the implementation so that the source * 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. * code size does not exceed v8's default max_inlined_source_size setting.
**/ **/
var validHdrChars = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32 - 47
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48 - 63
1, 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, 1, 1, 1, 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, 1, 1, 1, 1, 0, // 112 - 127
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128 ...
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // ... 255
];
function checkInvalidHeaderChar(val) { function checkInvalidHeaderChar(val) {
val += ''; val += '';
if (val.length < 1) if (val.length < 1)
return false; return false;
var c = val.charCodeAt(0); if (!validHdrChars[val.charCodeAt(0)])
if ((c <= 31 && c !== 9) || c > 255 || c === 127)
return true; return true;
if (val.length < 2) if (val.length < 2)
return false; return false;
c = val.charCodeAt(1); if (!validHdrChars[val.charCodeAt(1)])
if ((c <= 31 && c !== 9) || c > 255 || c === 127)
return true; return true;
if (val.length < 3) if (val.length < 3)
return false; return false;
c = val.charCodeAt(2); if (!validHdrChars[val.charCodeAt(2)])
if ((c <= 31 && c !== 9) || c > 255 || c === 127)
return true; return true;
for (var i = 3; i < val.length; ++i) { if (val.length < 4)
c = val.charCodeAt(i); return false;
if ((c <= 31 && c !== 9) || c > 255 || c === 127) if (!validHdrChars[val.charCodeAt(3)])
return true;
for (var i = 4; i < val.length; ++i) {
if (!validHdrChars[val.charCodeAt(i)])
return true; return true;
} }
return false; return false;