Skip to content

Commit b6cb614

Browse files
Tim Berners-LeeRubenVerborgh
authored andcommitted
Avoid problem with URIs with spaces.
1 parent bdc5acb commit b6cb614

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

lib/handlers/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async function globHandler (req, res, next) {
135135
const ldp = req.app.locals.ldp
136136
// TODO: This is a hack, that does not check if the target file exists, as this is quite complex with globbing.
137137
// TODO: Proper support for this is not implemented, as globbing support might be removed in the future.
138-
const filename = ldp.resourceMapper.getFullPath(req)
138+
const filename = ldp.resourceMapper.getFilePath(req)
139139
const requestUri = (await ldp.resourceMapper.mapFileToUrl({ path: filename, hostname: req.hostname })).url
140140

141141
const globOptions = {

lib/handlers/put.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ async function putStream (req, res, next, stream = req) {
3636
async function putAcl (req, res, next) {
3737
const ldp = req.app.locals.ldp
3838
const contentType = req.get('content-type')
39-
const path = ldp.resourceMapper.getFullPath(req)
40-
const requestUri = await ldp.resourceMapper.resolveUrl(req.hostname, path)
39+
const filename = ldp.resourceMapper.getFilePath(req) // @@ Note: weird to go through filename @@
40+
const requestUri = (await ldp.resourceMapper.mapFileToUrl({ path: filename, hostname: req.hostname })).url
41+
4142
if (ldp.isValidRdf(req.body, requestUri, contentType)) {
4243
const stream = stringToStream(req.body)
4344
return putStream(req, res, next, stream)

lib/resource-mapper.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class ResourceMapper {
4444
// Maps the request for a given resource and representation format to a server file
4545
// When searchIndex is true and the URL ends with a '/', and contentType includes 'text/html' indexFilename will be matched.
4646
async mapUrlToFile ({ url, contentType, createIfNotExists, searchIndex = true }) {
47-
let fullPath = this.getFullPath(url)
48-
let isIndex = searchIndex && fullPath.endsWith('/')
47+
let fullFilePath = this.getFilePath(url)
48+
let isIndex = searchIndex && fullFilePath.endsWith('/')
4949
let path
5050

5151
// Append index filename if the URL ends with a '/'
@@ -54,21 +54,21 @@ class ResourceMapper {
5454
throw new Error(`Index file needs to have ${this._indexContentType} as content type`)
5555
}
5656
if (contentType && contentType.includes(this._indexContentType)) {
57-
fullPath += this._indexFilename
57+
fullFilePath += this._indexFilename
5858
}
5959
}
6060
// Create the path for a new file
6161
if (createIfNotExists) {
62-
path = fullPath
62+
path = fullFilePath
6363
// If the extension is not correct for the content type, append the correct extension
6464
if (searchIndex && this._getContentTypeByExtension(path) !== contentType) {
6565
path += `$${contentType in extensions ? `.${extensions[contentType][0]}` : '.unknown'}`
6666
}
6767
// Determine the path of an existing file
6868
} else {
6969
// Read all files in the corresponding folder
70-
const filename = fullPath.substr(fullPath.lastIndexOf('/') + 1)
71-
const folder = fullPath.substr(0, fullPath.length - filename.length)
70+
const filename = fullFilePath.substr(fullFilePath.lastIndexOf('/') + 1)
71+
const folder = fullFilePath.substr(0, fullFilePath.length - filename.length)
7272

7373
// Find a file with the same name (minus the dollar extension)
7474
let match = searchIndex ? await this._getMatchingFile(folder, filename, isIndex, contentType) : ''
@@ -79,7 +79,7 @@ class ResourceMapper {
7979
if (isIndex) {
8080
match = ''
8181
} else {
82-
throw new HTTPError(404, `File not found: ${fullPath}`)
82+
throw new HTTPError(404, `File not found: ${fullFilePath}`)
8383
}
8484
}
8585
path = `${folder}${match}`
@@ -101,17 +101,17 @@ class ResourceMapper {
101101
}
102102

103103
async getRepresentationUrlForResource (resourceUrl) {
104-
let fullPath = this.getFullPath(resourceUrl)
105-
let isIndex = fullPath.endsWith('/')
104+
let fullFilePath = this.getFilePath(resourceUrl)
105+
let isIndex = fullFilePath.endsWith('/')
106106

107107
// Append index filename if the URL ends with a '/'
108108
if (isIndex) {
109-
fullPath += this._indexFilename
109+
fullFilePath += this._indexFilename
110110
}
111111

112112
// Read all files in the corresponding folder
113-
const filename = fullPath.substr(fullPath.lastIndexOf('/') + 1)
114-
const folder = fullPath.substr(0, fullPath.length - filename.length)
113+
const filename = fullFilePath.substr(fullFilePath.lastIndexOf('/') + 1)
114+
const folder = fullFilePath.substr(0, fullFilePath.length - filename.length)
115115
const files = await this._readdir(folder)
116116

117117
// Find a file with the same name (minus the dollar extension)
@@ -141,13 +141,13 @@ class ResourceMapper {
141141
}
142142

143143
// Determine the full file path corresponding to a URL
144-
getFullPath (url) {
144+
getFilePath (url) {
145145
const { pathname, hostname } = this._parseUrl(url)
146-
const fullPath = decodeURIComponent(`${this.getBasePath(hostname)}${pathname}`)
147-
if (fullPath.indexOf('/..') >= 0) {
146+
const fullFilePath = decodeURIComponent(`${this.getBasePath(hostname)}${pathname}`)
147+
if (fullFilePath.indexOf('/..') >= 0) {
148148
throw new Error('Disallowed /.. segment in URL')
149149
}
150-
return fullPath
150+
return fullFilePath
151151
}
152152

153153
// Parses a URL into a hostname and pathname

0 commit comments

Comments
 (0)