Skip to content

Commit 6aa3ae0

Browse files
committed
update regex and code cleaning and tests
1 parent 475ad71 commit 6aa3ae0

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

lib/resource-mapper.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,29 @@ class ResourceMapper {
147147
return { path, contentType: contentType || this._defaultContentType }
148148
}
149149

150-
// encode/decode path except slash/%encodedSlash/double%encodedSlash
151-
_exceptSlash () { return /(\/|%2f|%2F|%252F|%252f)/g }
150+
// encode/decode path except slash (/) / %encodedSlash (%2F|%2f) /ntimes%encodedSlash (%2525...2F|%2525...2f)
151+
// see https://github.com/solid/node-solid-server/issues/1666
152+
_exceptSlash () { return /(\/|%(?:25)*(?:2[Ff]))/g }
152153

153154
_encodePath (pathname) {
154-
const pathArray = pathname.split(this._exceptSlash())
155-
pathArray.forEach((el, i) => {
155+
return pathname.split(this._exceptSlash())
156+
.map((el, i) => i % 2 === 0 ? encodeURIComponent(el) : el)
157+
.join('')
158+
/* pathArray.forEach((el, i) => {
156159
if (i % 2 === 0) pathArray[i] = encodeURIComponent(el)
157-
})
158-
return pathArray.join('')
160+
}) */
161+
// return pathArray.join('')
159162
}
160163

161164
_decodePath (pathname) {
162-
const pathArray = pathname.split(this._exceptSlash())
165+
return pathname.split(this._exceptSlash())
166+
.map((el, i) => i % 2 === 0 ? decodeURIComponent(el) : el)
167+
.join('')
168+
/* const pathArray = pathname.split(this._exceptSlash())
163169
pathArray.forEach((el, i) => {
164170
if (i % 2 === 0) pathArray[i] = decodeURIComponent(el)
165171
})
166-
return pathArray.join('')
172+
return pathArray.join('') */
167173
}
168174

169175
// Parses a URL into hostname and pathname

test/unit/resource-mapper-test.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ describe('ResourceMapper', () => {
224224
contentType: 'text/html'
225225
})
226226

227-
itMapsUrl(mapper, 'a URL of a new file with encoded characters',
227+
itMapsUrl(mapper, 'a URL of a new file with encoded characters and encoded /',
228228
{
229-
url: 'http://localhost/space%2Ffoo%20bar%20bar.html',
229+
url: 'http://localhost/%25252fspace%2Ffoo%20bar%20bar.html',
230230
contentType: 'text/html',
231231
createIfNotExists: true
232232
},
233-
{ // alain
234-
path: `${rootPath}space%2Ffoo bar bar.html`,
233+
{
234+
path: `${rootPath}%25252fspace%2Ffoo bar bar.html`,
235235
contentType: 'text/html'
236236
})
237237

@@ -333,15 +333,6 @@ describe('ResourceMapper', () => {
333333
contentType: 'application/octet-stream'
334334
})
335335

336-
itMapsUrl(mapper, 'a URL ending with an encoded slash to a folder when no index is available',
337-
{
338-
url: 'http://localhost/space/'
339-
},
340-
{
341-
path: `${rootPath}space/`,
342-
contentType: 'application/octet-stream'
343-
})
344-
345336
itMapsUrl(mapper, 'a URL of that has an accompanying acl file, but no actual file',
346337
{
347338
url: 'http://localhost/space/'
@@ -418,14 +409,6 @@ describe('ResourceMapper', () => {
418409
},
419410
new Error('Disallowed /.. segment in URL'))
420411

421-
/* itMapsUrl(mapper, 'a URL with an encoded /.. path segment',
422-
{
423-
url: 'http://localhost/space%2F..%2Fbar'
424-
},
425-
new Error('Disallowed /.. segment in URL'))
426-
*/
427-
// File to URL mapping
428-
429412
itMapsFile(mapper, 'an HTML file',
430413
{ path: `${rootPath}space/foo.html` },
431414
{
@@ -511,9 +494,9 @@ describe('ResourceMapper', () => {
511494
})
512495

513496
itMapsFile(mapper, 'a file with %encoded /',
514-
{ path: `${rootPath}%2Fspace/foo%2f.html` },
497+
{ path: `${rootPath}%2Fspace/%25252Ffoo%2f.html` },
515498
{
516-
url: 'http://localhost/%2Fspace/foo%2f.html',
499+
url: 'http://localhost/%2Fspace/%25252Ffoo%2f.html',
517500
contentType: 'text/html'
518501
})
519502

0 commit comments

Comments
 (0)