Skip to content

Commit

Permalink
fix: escape double colon in multi-parametrical node (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored Oct 7, 2022
1 parent 917645d commit 6b59926
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,18 @@ Router.prototype._on = function _on (method, path, opts, handler, store) {
let lastParamEndIndex = j
for (; lastParamEndIndex < path.length; lastParamEndIndex++) {
const charCode = path.charCodeAt(lastParamEndIndex)
if (charCode === 58 || charCode === 47) {
break
const nextCharCode = path.charCodeAt(lastParamEndIndex + 1)
if (charCode === 58 && nextCharCode === 58) {
lastParamEndIndex++
continue
}
if (charCode === 58 || charCode === 47) break
}

const staticPart = path.slice(j, lastParamEndIndex)
let staticPart = path.slice(j, lastParamEndIndex)
if (staticPart) {
staticPart = staticPart.split('::').join(':')
staticPart = staticPart.split('%').join('%25')
regexps.push(escapeRegExp(staticPart))
}

Expand Down
21 changes: 21 additions & 0 deletions test/issue-17.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ test('Multi parametric route with regexp / 1', t => {
findMyWay.lookup({ method: 'GET', url: '/at/0h42m', headers: {} }, null)
})

test('Multi parametric route with colon separator', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/:param(.*)::suffix', (req, res, params) => {
t.equal(params.param, 'foo')
})

findMyWay.on('GET', '/:param1(.*)::suffix1-:param2(.*)::suffix2/static', (req, res, params) => {
t.equal(params.param1, 'foo')
t.equal(params.param2, 'bar')
})

findMyWay.lookup({ method: 'GET', url: '/foo:suffix', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/foo:suffix1-bar:suffix2/static', headers: {} }, null)
})

test('Multi parametric route with regexp / 2', t => {
t.plan(8)
const findMyWay = FindMyWay({
Expand Down
15 changes: 15 additions & 0 deletions test/issue-206.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ test('Special chars on path parameter', t => {
findMyWay.lookup(get('/reg/123%20.png'), null, { expect: { regExeParam: '123' }, handler: regexPathParam })
})

test('Multi parametric route with encoded colon separator', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})

findMyWay.on('GET', '/:param(.*)::suffix', (req, res, params) => {
t.equal(params.param, 'foo-bar')
})

findMyWay.lookup({ method: 'GET', url: '/foo-bar%3Asuffix', headers: {} }, null)
})

function get (url) {
return { method: 'GET', url, headers: {} }
}
Expand Down

0 comments on commit 6b59926

Please sign in to comment.