From 364be6b296f65b325de35ce3adc71a7f2b3a0e2c Mon Sep 17 00:00:00 2001 From: jaxoncreed Date: Wed, 23 Oct 2019 13:12:10 -0400 Subject: [PATCH 1/4] Resource mapper properly encodes special characters in the uri --- lib/resource-mapper.js | 5 ++++- test/unit/resource-mapper-test.js | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/resource-mapper.js b/lib/resource-mapper.js index bb5f6349c..36a7a645b 100644 --- a/lib/resource-mapper.js +++ b/lib/resource-mapper.js @@ -78,7 +78,10 @@ class ResourceMapper { // Determine the URL by chopping off everything after the dollar sign const pathname = this._removeDollarExtension(path) - const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}` + console.log(pathname) + const url = `${this.resolveUrl(hostname)}${ + pathname.split('/').map((component) => encodeURIComponent(component)).join('/') + }` return { url, contentType: this._getContentTypeByExtension(path) } } diff --git a/test/unit/resource-mapper-test.js b/test/unit/resource-mapper-test.js index fc22521e3..42c9865a8 100644 --- a/test/unit/resource-mapper-test.js +++ b/test/unit/resource-mapper-test.js @@ -478,6 +478,13 @@ describe('ResourceMapper', () => { url: 'http://localhost/space/foo%20bar%20bar.html', contentType: 'text/html' }) + + itMapsFile(mapper, 'a file with even stranger disallowed IRI characters', + { path: `${rootPath}space/Blog discovery for the future? · Issue #96 · scripting:Scripting-News · GitHub.pdf` }, + { + 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', + contentType: 'application/pdf' + }) }) describe('A ResourceMapper instance for a multi-host setup', () => { From 508bc867d3cb6e878dd4af51bce7b9e6b2f10535 Mon Sep 17 00:00:00 2001 From: jaxoncreed Date: Wed, 23 Oct 2019 13:43:04 -0400 Subject: [PATCH 2/4] Use path.sep --- lib/resource-mapper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/resource-mapper.js b/lib/resource-mapper.js index 36a7a645b..492ff451f 100644 --- a/lib/resource-mapper.js +++ b/lib/resource-mapper.js @@ -4,6 +4,7 @@ const { promisify } = require('util') const { types, extensions } = require('mime-types') const readdir = promisify(fs.readdir) const HTTPError = require('./http-error') +const pathUtil = require('path') /* * A ResourceMapper maintains the mapping between HTTP URLs and server filenames, @@ -78,9 +79,8 @@ class ResourceMapper { // Determine the URL by chopping off everything after the dollar sign const pathname = this._removeDollarExtension(path) - console.log(pathname) const url = `${this.resolveUrl(hostname)}${ - pathname.split('/').map((component) => encodeURIComponent(component)).join('/') + pathname.split(pathUtil.sep).map((component) => encodeURIComponent(component)).join('/') }` return { url, contentType: this._getContentTypeByExtension(path) } } From 30f12166ae623fffbd9cf681bfdc6ff876b2b513 Mon Sep 17 00:00:00 2001 From: jaxoncreed Date: Wed, 23 Oct 2019 13:12:10 -0400 Subject: [PATCH 3/4] Resource mapper properly encodes special characters in the uri --- lib/resource-mapper.js | 5 ++++- test/unit/resource-mapper-test.js | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/resource-mapper.js b/lib/resource-mapper.js index bb5f6349c..36a7a645b 100644 --- a/lib/resource-mapper.js +++ b/lib/resource-mapper.js @@ -78,7 +78,10 @@ class ResourceMapper { // Determine the URL by chopping off everything after the dollar sign const pathname = this._removeDollarExtension(path) - const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}` + console.log(pathname) + const url = `${this.resolveUrl(hostname)}${ + pathname.split('/').map((component) => encodeURIComponent(component)).join('/') + }` return { url, contentType: this._getContentTypeByExtension(path) } } diff --git a/test/unit/resource-mapper-test.js b/test/unit/resource-mapper-test.js index fc22521e3..42c9865a8 100644 --- a/test/unit/resource-mapper-test.js +++ b/test/unit/resource-mapper-test.js @@ -478,6 +478,13 @@ describe('ResourceMapper', () => { url: 'http://localhost/space/foo%20bar%20bar.html', contentType: 'text/html' }) + + itMapsFile(mapper, 'a file with even stranger disallowed IRI characters', + { path: `${rootPath}space/Blog discovery for the future? · Issue #96 · scripting:Scripting-News · GitHub.pdf` }, + { + 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', + contentType: 'application/pdf' + }) }) describe('A ResourceMapper instance for a multi-host setup', () => { From 89ba51ef987daa2361857529921b5fccace93199 Mon Sep 17 00:00:00 2001 From: jaxoncreed Date: Wed, 23 Oct 2019 13:43:04 -0400 Subject: [PATCH 4/4] Use path.sep --- lib/resource-mapper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/resource-mapper.js b/lib/resource-mapper.js index 36a7a645b..492ff451f 100644 --- a/lib/resource-mapper.js +++ b/lib/resource-mapper.js @@ -4,6 +4,7 @@ const { promisify } = require('util') const { types, extensions } = require('mime-types') const readdir = promisify(fs.readdir) const HTTPError = require('./http-error') +const pathUtil = require('path') /* * A ResourceMapper maintains the mapping between HTTP URLs and server filenames, @@ -78,9 +79,8 @@ class ResourceMapper { // Determine the URL by chopping off everything after the dollar sign const pathname = this._removeDollarExtension(path) - console.log(pathname) const url = `${this.resolveUrl(hostname)}${ - pathname.split('/').map((component) => encodeURIComponent(component)).join('/') + pathname.split(pathUtil.sep).map((component) => encodeURIComponent(component)).join('/') }` return { url, contentType: this._getContentTypeByExtension(path) } }