From a6d66ed4521a3f912a518278e860708196418e69 Mon Sep 17 00:00:00 2001 From: ivan-tymoshenko Date: Sat, 21 May 2022 13:18:58 +0300 Subject: [PATCH] Optimize parameters parsing --- index.js | 24 +++++++++--------------- lib/url-sanitizer.js | 10 ++++++++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 654fabd..033d825 100644 --- a/index.js +++ b/index.js @@ -343,11 +343,13 @@ Router.prototype.find = function find (method, path, derivedConstraints) { let sanitizedUrl let querystring + let shouldDecodeParam try { sanitizedUrl = safeDecodeURI(path) path = sanitizedUrl.path querystring = sanitizedUrl.querystring + shouldDecodeParam = sanitizedUrl.shouldDecodeParam } catch (error) { return this._onBadUrl(path) } @@ -448,10 +450,8 @@ Router.prototype.find = function find (method, path, derivedConstraints) { if (currentNode.kind === NODE_TYPES.WILDCARD) { let param = originPath.slice(pathIndex) - - const firstPercentIndex = param.indexOf('%') - if (firstPercentIndex !== -1) { - param = safeDecodeURIComponent(param, firstPercentIndex) + if (shouldDecodeParam) { + param = safeDecodeURIComponent(param) } if (param.length > maxParamLength) { @@ -464,20 +464,14 @@ Router.prototype.find = function find (method, path, derivedConstraints) { } if (currentNode.kind === NODE_TYPES.PARAMETRIC) { - let paramEndIndex = pathIndex - let firstPercentIndex = -1 - for (; paramEndIndex < pathLen; paramEndIndex++) { - const charCode = path.charCodeAt(paramEndIndex) - if (charCode === 47) { - break - } else if (firstPercentIndex === -1 && charCode === 37) { - firstPercentIndex = paramEndIndex - pathIndex - } + let paramEndIndex = originPath.indexOf('/', pathIndex) + if (paramEndIndex === -1) { + paramEndIndex = pathLen } let param = originPath.slice(pathIndex, paramEndIndex) - if (firstPercentIndex !== -1) { - param = safeDecodeURIComponent(param, firstPercentIndex) + if (shouldDecodeParam) { + param = safeDecodeURIComponent(param) } if (currentNode.isRegex) { diff --git a/lib/url-sanitizer.js b/lib/url-sanitizer.js index 9af1f6e..c124e8e 100644 --- a/lib/url-sanitizer.js +++ b/lib/url-sanitizer.js @@ -36,6 +36,8 @@ function decodeComponentChar (highCharCode, lowCharCode) { function safeDecodeURI (path) { let shouldDecode = false + let shouldDecodeParam = false + let querystring = '' for (let i = 1; i < path.length; i++) { @@ -48,6 +50,7 @@ function safeDecodeURI (path) { if (decodeComponentChar(highCharCode, lowCharCode) === null) { shouldDecode = true } else { + shouldDecodeParam = true // %25 - encoded % char. We need to encode one more time to prevent double decoding if (highCharCode === 50 && lowCharCode === 53) { shouldDecode = true @@ -66,10 +69,13 @@ function safeDecodeURI (path) { } } const decodedPath = shouldDecode ? decodeURI(path) : path - return { path: decodedPath, querystring } + return { path: decodedPath, querystring, shouldDecodeParam } } -function safeDecodeURIComponent (uriComponent, startIndex) { +function safeDecodeURIComponent (uriComponent) { + const startIndex = uriComponent.indexOf('%') + if (startIndex === -1) return uriComponent + let decoded = '' let lastIndex = startIndex