Skip to content

Commit 6170cc8

Browse files
authored
Merge pull request #952 from rubensworks/feature/wire-legacy-resource-mapper-2
Wire up LegacyResourceMapper
2 parents b568142 + 825472e commit 6170cc8

32 files changed

+926
-1028
lines changed

config/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports = {
1313
'webid': true,
1414
'strictOrigin': true,
1515
'originsAllowed': ['https://apps.solid.invalid'],
16-
'dataBrowserPath': 'default'
16+
'dataBrowserPath': 'default',
17+
'defaultContentType': 'text/turtle'
1718

1819
// For use in Enterprises to configure a HTTP proxy for all outbound HTTP requests from the SOLID server (we use
1920
// https://www.npmjs.com/package/global-tunnel-ng).

lib/capability-discovery.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @module capability-discovery
44
*/
55
const express = require('express')
6-
const util = require('./utils')
76

87
const serviceConfigDefaults = {
98
'api': {
@@ -47,7 +46,7 @@ function capabilityDiscovery () {
4746
function serviceCapabilityDocument (serviceConfig) {
4847
return (req, res) => {
4948
// Add the server root url
50-
serviceConfig.root = util.getFullUri(req) // TODO make sure we align with the rest
49+
serviceConfig.root = req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, req.path)
5150
// Add the 'apps' urls section
5251
serviceConfig.apps = req.app.locals.appUrls
5352
res.json(serviceConfig)

lib/create-app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const options = require('./handlers/options')
2323
const debug = require('./debug').authentication
2424
const path = require('path')
2525
const { routeResolvedFile } = require('./utils')
26+
const LegacyResourceMapper = require('./legacy-resource-mapper')
2627

2728
const corsSettings = cors({
2829
methods: [
@@ -41,6 +42,13 @@ function createApp (argv = {}) {
4142

4243
argv.host = SolidHost.from({ port: argv.port, serverUri: argv.serverUri })
4344

45+
argv.resourceMapper = new LegacyResourceMapper({
46+
rootUrl: argv.serverUri,
47+
rootPath: argv.root || process.cwd(),
48+
includeHost: argv.multiuser,
49+
defaultContentType: argv.defaultContentType
50+
})
51+
4452
const configPath = config.initConfigDir(argv)
4553
argv.templates = config.initTemplateDirs(configPath)
4654

lib/handlers/allow.js

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
module.exports = allow
22

33
const ACL = require('../acl-checker')
4-
const $rdf = require('rdflib')
5-
const utils = require('../utils')
64
const debug = require('../debug.js').ACL
7-
const LegacyResourceMapper = require('../legacy-resource-mapper')
85

96
function allow (mode) {
10-
return function allowHandler (req, res, next) {
7+
return async function allowHandler (req, res, next) {
118
const ldp = req.app.locals.ldp || {}
129
if (!ldp.webid) {
1310
return next()
1411
}
1512

1613
// 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-
})
14+
const rootUrl = ldp.resourceMapper.resolveUrl(req.hostname)
2315

2416
// Determine the actual path of the request
2517
// (This is used as an ugly hack to check the ACL status of other resources.)
@@ -28,37 +20,41 @@ function allow (mode) {
2820
: req.path
2921

3022
// Check whether the resource exists
31-
ldp.exists(req.hostname, reqPath, (err, ret) => {
32-
// Ensure directories always end in a slash
33-
const stat = err ? null : ret.stream
34-
if (!reqPath.endsWith('/') && stat && stat.isDirectory()) {
35-
reqPath += '/'
36-
}
23+
let stat
24+
try {
25+
const ret = await ldp.exists(req.hostname, reqPath)
26+
stat = ret.stream
27+
} catch (err) {
28+
stat = null
29+
}
3730

38-
// Obtain and store the ACL of the requested resource
39-
req.acl = new ACL(rootUrl + reqPath, {
40-
origin: req.get('origin'),
41-
host: req.protocol + '://' + req.get('host'),
42-
fetch: fetchFromLdp(mapper, ldp),
43-
fetchGraph: (uri, options) => {
44-
// first try loading from local fs
45-
return ldp.getGraph(uri, options.contentType)
46-
// failing that, fetch remote graph
47-
.catch(() => ldp.fetchGraph(uri, options))
48-
},
49-
suffix: ldp.suffixAcl,
50-
strictOrigin: ldp.strictOrigin,
51-
originsAllowed: ldp.originsAllowed
52-
})
31+
// Ensure directories always end in a slash
32+
if (!reqPath.endsWith('/') && stat && stat.isDirectory()) {
33+
reqPath += '/'
34+
}
5335

54-
// Ensure the user has the required permission
55-
const userId = req.session.userId
56-
req.acl.can(userId, mode)
57-
.then(() => next(), err => {
58-
debug(`${mode} access denied to ${userId || '(none)'}`)
59-
next(err)
60-
})
36+
// Obtain and store the ACL of the requested resource
37+
req.acl = new ACL(rootUrl + reqPath, {
38+
origin: req.get('origin'),
39+
host: req.protocol + '://' + req.get('host'),
40+
fetch: fetchFromLdp(ldp.resourceMapper, ldp),
41+
fetchGraph: (uri, options) => {
42+
// first try loading from local fs
43+
return ldp.getGraph(uri, options.contentType)
44+
// failing that, fetch remote graph
45+
.catch(() => ldp.fetchGraph(uri, options))
46+
},
47+
suffix: ldp.suffixAcl,
48+
strictOrigin: ldp.strictOrigin
6149
})
50+
51+
// Ensure the user has the required permission
52+
const userId = req.session.userId
53+
req.acl.can(userId, mode)
54+
.then(() => next(), err => {
55+
debug(`${mode} access denied to ${userId || '(none)'}`)
56+
next(err)
57+
})
6258
}
6359
}
6460

@@ -72,19 +68,6 @@ function allow (mode) {
7268
*/
7369
function fetchFromLdp (mapper, ldp) {
7470
return function fetch (url, callback) {
75-
// Convert the URL into a filename
76-
mapper.mapUrlToFile({ url })
77-
// Read the file from disk
78-
.then(({ path }) => new Promise((resolve, reject) => {
79-
ldp.readFile(path, (e, c) => e ? reject(e) : resolve(c))
80-
}))
81-
// Parse the file as Turtle
82-
.then(body => {
83-
const graph = $rdf.graph()
84-
$rdf.parse(body, graph, url, 'text/turtle')
85-
return graph
86-
})
87-
// Return the ACL graph
88-
.then(graph => callback(null, graph), callback)
71+
ldp.getGraph(url).then(g => callback(null, g), callback)
8972
}
9073
}

lib/handlers/copy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module.exports = handler
33
const debug = require('../debug')
44
const error = require('../http-error')
55
const ldpCopy = require('../ldp-copy')
6-
const utils = require('../utils')
76
const url = require('url')
87

98
/**
@@ -14,16 +13,17 @@ const url = require('url')
1413
* "Save an external resource to Solid" type apps.
1514
* @method handler
1615
*/
17-
function handler (req, res, next) {
16+
async function handler (req, res, next) {
1817
const copyFrom = req.header('Source')
1918
if (!copyFrom) {
2019
return next(error(400, 'Source header required'))
2120
}
2221
const fromExternal = !!url.parse(copyFrom).hostname
23-
const serverRoot = utils.getBaseUri(req)
22+
const ldp = req.app.locals.ldp
23+
const serverRoot = ldp.resourceMapper.resolveUrl(req.hostname)
2424
const copyFromUrl = fromExternal ? copyFrom : serverRoot + copyFrom
2525
const copyTo = res.locals.path || req.path
26-
const copyToPath = utils.reqToPath(req)
26+
const { path: copyToPath } = await ldp.resourceMapper.mapUrlToFile({ url: req })
2727
ldpCopy(copyToPath, copyFromUrl, function (err) {
2828
if (err) {
2929
let statusCode = err.statusCode || 500

lib/handlers/delete.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ module.exports = handler
22

33
const debug = require('../debug').handlers
44

5-
function handler (req, res, next) {
5+
async function handler (req, res, next) {
66
debug('DELETE -- Request on' + req.originalUrl)
77

88
const ldp = req.app.locals.ldp
9-
ldp.delete(req.hostname, req.path, function (err) {
10-
if (err) {
11-
debug('DELETE -- Failed to delete: ' + err)
12-
return next(err)
13-
}
9+
try {
10+
await ldp.delete(req)
1411
debug('DELETE -- Ok.')
1512
res.sendStatus(200)
16-
return next()
17-
})
13+
next()
14+
} catch (err) {
15+
debug('DELETE -- Failed to delete: ' + err)
16+
next(err)
17+
}
1818
}

0 commit comments

Comments
 (0)