Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize parameters parsing #289

Merged
merged 1 commit into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions lib/url-sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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
Expand All @@ -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

Expand Down