Skip to content

Commit a704e96

Browse files
committed
Use LegacyResourceMapper in allow handler.
Fixes #656.
1 parent 3a42fd4 commit a704e96

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

lib/handlers/allow.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ module.exports = allow
22

33
var ACL = require('../acl-checker')
44
var $rdf = require('rdflib')
5-
var url = require('url')
65
var utils = require('../utils')
76
var debug = require('../debug.js').ACL
7+
var LegacyResourceMapper = require('../legacy-resource-mapper')
88

99
function allow (mode) {
1010
return function allowHandler (req, res, next) {
@@ -13,6 +13,14 @@ function allow (mode) {
1313
return next()
1414
}
1515

16+
// Set up URL to filesystem mapping
17+
const rootUrl = utils.getBaseUri(req)
18+
const mapper = new LegacyResourceMapper({
19+
rootUrl,
20+
rootPath: ldp.root,
21+
includeHost: ldp.multiuser
22+
})
23+
1624
// Determine the actual path of the request
1725
var reqPath = res && res.locals && res.locals.path
1826
? res.locals.path
@@ -27,11 +35,10 @@ function allow (mode) {
2735
}
2836

2937
// Obtain and store the ACL of the requested resource
30-
const baseUri = utils.getBaseUri(req)
31-
req.acl = new ACL(baseUri + reqPath, {
38+
req.acl = new ACL(rootUrl + reqPath, {
3239
origin: req.get('origin'),
3340
host: req.protocol + '://' + req.get('host'),
34-
fetch: fetchDocument(req.hostname, ldp, baseUri),
41+
fetch: fetchFromLdp(mapper, ldp),
3542
suffix: ldp.suffixAcl,
3643
strictOrigin: ldp.strictOrigin
3744
})
@@ -53,40 +60,23 @@ function allow (mode) {
5360
* The `fetch(uri, callback)` results in the callback, with either:
5461
* - `callback(err, graph)` if any error is encountered, or
5562
* - `callback(null, graph)` with the parsed RDF graph of the fetched resource
56-
* @method fetchDocument
57-
* @param host {string} req.hostname. Used in determining the location of the
58-
* document on the file system (the root directory)
59-
* @param ldp {LDP} LDP instance
60-
* @param baseUri {string} Base URI of the solid server (including any root
61-
* mount), for example `https://example.com/solid`
6263
* @return {Function} Returns a `fetch(uri, callback)` handler
6364
*/
64-
function fetchDocument (host, ldp, baseUri) {
65-
return function fetch (uri, callback) {
66-
readFile(uri, host, ldp, baseUri).then(body => {
65+
function fetchFromLdp (mapper, ldp) {
66+
return function fetch (url, callback) {
67+
// Convert the URL into a filename
68+
mapper.mapUrlToFile({ url })
69+
// Read the file from disk
70+
.then(({ path }) => new Promise((resolve, reject) => {
71+
ldp.readFile(path, (e, c) => e ? reject(e) : resolve(c))
72+
}))
73+
// Parse the file as Turtle
74+
.then(body => {
6775
const graph = $rdf.graph()
68-
$rdf.parse(body, graph, uri, 'text/turtle')
76+
$rdf.parse(body, graph, url, 'text/turtle')
6977
return graph
7078
})
79+
// Return the ACL graph
7180
.then(graph => callback(null, graph), callback)
7281
}
7382
}
74-
75-
// Reads the given file, returning its contents
76-
function readFile (uri, host, ldp, baseUri) {
77-
return new Promise((resolve, reject) => {
78-
// If local request, slice off the initial baseUri
79-
// S(uri).chompLeft(baseUri).s
80-
var newPath = uri.startsWith(baseUri)
81-
? uri.slice(baseUri.length)
82-
: uri
83-
// Determine the root file system folder to look in
84-
// TODO prettify this
85-
var root = !ldp.multiuser ? ldp.root : ldp.root + host + '/'
86-
// Derive the file path for the resource
87-
var documentPath = utils.uriToFilename(newPath, root)
88-
var documentUri = url.parse(documentPath)
89-
documentPath = documentUri.pathname
90-
ldp.readFile(documentPath, (e, c) => e ? reject(e) : resolve(c))
91-
})
92-
}

0 commit comments

Comments
 (0)