summaryrefslogtreecommitdiff
path: root/lib/_http_common.js
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2016-05-07 17:03:35 -0400
committerEvan Lucas <evanlucas@me.com>2016-06-15 21:49:27 -0500
commit2a462ba1e2797465e9164399a12b250661df41bd (patch)
treef282f8661a0c76c1cf95a56de52223e3f5fa9ffd /lib/_http_common.js
parent4a63be031fc8fbb0ffa48e1099ed5f064e69ed86 (diff)
downloadnode-new-2a462ba1e2797465e9164399a12b250661df41bd.tar.gz
http: optimize checkInvalidHeaderChar()
This commit optimizes checkInvalidHeaderChar() by unrolling the character checking loop a bit. Additionally, some changes to the benchmark runner are needed in order for the included benchmark to be run correctly. Specifically, the regexp used to parse `key=value` parameters contained a greedy quantifier that was causing the `key` to match part of the `value` if `value` contained an equals sign. PR-URL: https://github.com/nodejs/node/pull/6570 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Diffstat (limited to 'lib/_http_common.js')
-rw-r--r--lib/_http_common.js29
1 files changed, 24 insertions, 5 deletions
diff --git a/lib/_http_common.js b/lib/_http_common.js
index 150d424261..ad0389ba21 100644
--- a/lib/_http_common.js
+++ b/lib/_http_common.js
@@ -301,13 +301,32 @@ exports._checkIsHttpToken = checkIsHttpToken;
* 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.
**/
function checkInvalidHeaderChar(val) {
- val = '' + val;
- for (var i = 0; i < val.length; i++) {
- const ch = val.charCodeAt(i);
- if (ch === 9) continue;
- if (ch <= 31 || ch > 255 || ch === 127) return true;
+ val += '';
+ if (val.length < 1)
+ return false;
+ var c = val.charCodeAt(0);
+ if ((c <= 31 && c !== 9) || c > 255 || c === 127)
+ return true;
+ if (val.length < 2)
+ return false;
+ c = val.charCodeAt(1);
+ if ((c <= 31 && c !== 9) || c > 255 || c === 127)
+ return true;
+ if (val.length < 3)
+ return false;
+ c = val.charCodeAt(2);
+ if ((c <= 31 && c !== 9) || c > 255 || c === 127)
+ return true;
+ for (var i = 3; i < val.length; ++i) {
+ c = val.charCodeAt(i);
+ if ((c <= 31 && c !== 9) || c > 255 || c === 127)
+ return true;
}
return false;
}