aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSeth Brenith <sethb@microsoft.com>2018-01-25 09:48:38 -0800
committerRuben Bridgewater <ruben@bridgewater.de>2018-02-16 19:27:28 +0100
commit862389b0aacfd41770c3fa6f692aed11c2a4b3ed (patch)
tree5edd14da298c47d94c27c662f7ded7b37db51ce0 /lib
parent3c29adb84fac00dfc16c51725e4832789610c6a7 (diff)
downloadandroid-node-v8-862389b0aacfd41770c3fa6f692aed11c2a4b3ed.tar.gz
android-node-v8-862389b0aacfd41770c3fa6f692aed11c2a4b3ed.tar.bz2
android-node-v8-862389b0aacfd41770c3fa6f692aed11c2a4b3ed.zip
http: simplify checkInvalidHeaderChar
In the spirit of [17399](https://github.com/nodejs/node/pull/17399), we can also simplify checkInvalidHeaderChar to use regex matching instead of a loop. This makes it faster on long matches and slower on short matches or non-matches. This change also includes some sample data from an AcmeAir benchmark run, as a rough proxy for real-world data. PR-URL: https://github.com/nodejs/node/pull/18381 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/_http_common.js46
1 files changed, 2 insertions, 44 deletions
diff --git a/lib/_http_common.js b/lib/_http_common.js
index a7e8b0c59b..ffb90407c6 100644
--- a/lib/_http_common.js
+++ b/lib/_http_common.js
@@ -242,57 +242,15 @@ function checkIsHttpToken(val) {
return tokenRegExp.test(val);
}
+const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
/**
* True if val contains an invalid field-vchar
* field-value = *( field-content / obs-fold )
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
* field-vchar = VCHAR / obs-text
- *
- * checkInvalidHeaderChar() 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 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) {
- val += '';
- if (val.length < 1)
- return false;
- if (!validHdrChars[val.charCodeAt(0)])
- return true;
- if (val.length < 2)
- return false;
- if (!validHdrChars[val.charCodeAt(1)])
- return true;
- if (val.length < 3)
- return false;
- if (!validHdrChars[val.charCodeAt(2)])
- return true;
- if (val.length < 4)
- return false;
- if (!validHdrChars[val.charCodeAt(3)])
- return true;
- for (var i = 4; i < val.length; ++i) {
- if (!validHdrChars[val.charCodeAt(i)])
- return true;
- }
- return false;
+ return headerCharRegex.test(val);
}
module.exports = {