Skip to content

Commit 1ff9728

Browse files
authored
Merge pull request #1327 from solid/fix/1324
Resource mapper properly encodes special characters in the uri
2 parents 0a958f2 + 68f8fca commit 1ff9728

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/resource-mapper.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { promisify } = require('util')
44
const { types, extensions } = require('mime-types')
55
const readdir = promisify(fs.readdir)
66
const HTTPError = require('./http-error')
7+
const pathUtil = require('path')
78

89
/*
910
* A ResourceMapper maintains the mapping between HTTP URLs and server filenames,
@@ -78,7 +79,9 @@ class ResourceMapper {
7879

7980
// Determine the URL by chopping off everything after the dollar sign
8081
const pathname = this._removeDollarExtension(path)
81-
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
82+
const url = `${this.resolveUrl(hostname)}${
83+
pathname.split(pathUtil.sep).map((component) => encodeURIComponent(component)).join('/')
84+
}`
8285
return { url, contentType: this._getContentTypeFromExtension(path) }
8386
}
8487

test/unit/resource-mapper-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,13 @@ describe('ResourceMapper', () => {
489489
url: 'http://localhost/space/foo%20bar%20bar.html',
490490
contentType: 'text/html'
491491
})
492+
493+
itMapsFile(mapper, 'a file with even stranger disallowed IRI characters',
494+
{ path: `${rootPath}space/Blog discovery for the future? · Issue #96 · scripting:Scripting-News · GitHub.pdf` },
495+
{
496+
url: 'http://localhost/space/Blog%20discovery%20for%20the%20future%3F%20%C2%B7%20Issue%20%2396%20%C2%B7%20scripting%3AScripting-News%20%C2%B7%20GitHub.pdf',
497+
contentType: 'application/pdf'
498+
})
492499
})
493500

494501
describe('A ResourceMapper instance for a multi-host setup', () => {

0 commit comments

Comments
 (0)