summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-11-29 18:37:09 -0800
committerAnatoli Papirovski <apapirovski@mac.com>2017-12-10 16:50:47 -0500
commit9f55eac346dea4fba6ace9becc1e01d893f56c5c (patch)
tree80057aa5f50fffe887f1bfc99da25caa050c6dc9 /lib
parentf373a1d8145785b03aa535c739984d832fa0000a (diff)
downloadandroid-node-v8-9f55eac346dea4fba6ace9becc1e01d893f56c5c.tar.gz
android-node-v8-9f55eac346dea4fba6ace9becc1e01d893f56c5c.tar.bz2
android-node-v8-9f55eac346dea4fba6ace9becc1e01d893f56c5c.zip
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/_http_common.js62
1 files changed, 3 insertions, 59 deletions
diff --git a/lib/_http_common.js b/lib/_http_common.js
index ad0dec520d..cf37bbebe3 100644
--- a/lib/_http_common.js
+++ b/lib/_http_common.js
@@ -233,70 +233,14 @@ function httpSocketSetup(socket) {
socket.on('drain', ondrain);
}
+const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
/**
* Verifies that the given val is a valid HTTP token
* per the rules defined in RFC 7230
* 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) {
- if (!validTokens[val.charCodeAt(0)])
- 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;
+ return tokenRegExp.test(val);
}
/**