Skip to content

Commit 23d5dad

Browse files
authored
Merge pull request #1028 from joachimvh/fix/windows
Fix path-related Windows problems
2 parents 728eef6 + 72b1856 commit 23d5dad

File tree

8 files changed

+137
-52
lines changed

8 files changed

+137
-52
lines changed

lib/handlers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const debug = require('debug')('solid:index')
55
const utils = require('../utils')
66
const Negotiator = require('negotiator')
77
const url = require('url')
8+
const URI = require('urijs')
89

910
function handler (req, res, next) {
1011
const indexFile = 'index.html'
@@ -21,7 +22,7 @@ function handler (req, res, next) {
2122
}
2223
// redirect to the right container if missing trailing /
2324
if (req.path.lastIndexOf('/') !== req.path.length - 1) {
24-
return res.redirect(301, path.join(req.path, '/'))
25+
return res.redirect(301, URI.joinPaths(req.path, '/', '//').toString())
2526
}
2627

2728
if (requestedType && requestedType.indexOf('text/html') !== 0) {

lib/header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function linksHandler (req, res, next) {
5353
return next(error(404, 'Trying to access metadata file as regular file'))
5454
}
5555
let fileMetadata = new metadata.Metadata()
56-
if (filename.endsWith('/')) {
56+
if (req.path.endsWith('/')) {
5757
fileMetadata.isContainer = true
5858
fileMetadata.isBasicContainer = true
5959
} else {

lib/ldp.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ldpContainer = require('./ldp-container')
1717
const parse = require('./utils').parse
1818
const fetch = require('node-fetch')
1919
const { promisify } = require('util')
20+
const URI = require('urijs')
2021

2122
const DEFAULT_CONTENT_TYPE = 'text/turtle'
2223

@@ -263,7 +264,7 @@ class LDP {
263264
const filePath = utils.uriToFilename(resourcePath, root, host)
264265

265266
// PUT requests not supported on containers. Use POST instead
266-
if (filePath.endsWith('/')) {
267+
if (resourcePath.endsWith('/')) {
267268
return callback(error(409,
268269
'PUT not supported on containers, use POST instead'))
269270
}
@@ -420,6 +421,11 @@ class LDP {
420421
return callback(error(err, 'Can\'t find file requested: ' + filename))
421422
}
422423

424+
// windows does not differentiate between file paths with or without trailing slash
425+
if (reqPath.substr(-1) === '/' && !stats.isDirectory()) {
426+
return callback(error(404, 'Can\'t find directory requested: ' + filename))
427+
}
428+
423429
// Just return, since resource exists
424430
if (!includeBody) {
425431
return callback(null, {'stream': stats, 'contentType': contentType, 'container': stats.isDirectory()})
@@ -540,13 +546,13 @@ class LDP {
540546
// Verify whether the new path already exists
541547
return self.exists(host, newPath).then(
542548
// If it does, generate another one
543-
() => ensureNotExists(self, path.join(containerURI,
544-
`${uuid.v1().split('-')[0]}-${filename}`)),
549+
() => ensureNotExists(self, URI.joinPaths(containerURI,
550+
`${uuid.v1().split('-')[0]}-${filename}`).toString()),
545551
// If not, we found an appropriate path
546552
() => newPath
547553
)
548554
}
549-
return ensureNotExists(this, path.join(containerURI, filename))
555+
return ensureNotExists(this, URI.joinPaths(containerURI, filename).toString())
550556
}
551557
}
552558
module.exports = LDP

lib/resource-mapper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class ResourceMapper {
5858
// Maps a given server file to a URL
5959
async mapFileToUrl ({ path, hostname }) {
6060
// Determine the URL by chopping off everything after the dollar sign
61-
const pathname = this._removeDollarExtension(path.substring(this._rootPath.length))
61+
let pathname = this._removeDollarExtension(path.substring(this._rootPath.length))
62+
pathname = this._replaceBackslashes(pathname)
6263
const url = `${this.getBaseUrl(hostname)}${encodeURI(pathname)}`
6364
return { url, contentType: this._getContentTypeByExtension(path) }
6465
}
@@ -101,6 +102,10 @@ class ResourceMapper {
101102
const dollarPos = path.lastIndexOf('$')
102103
return dollarPos < 0 ? path : path.substr(0, dollarPos)
103104
}
105+
106+
_replaceBackslashes (path) {
107+
return path.replace(/\\/g, '/')
108+
}
104109
}
105110

106111
module.exports = ResourceMapper

0 commit comments

Comments
 (0)