Skip to content

Commit 3d43f72

Browse files
committed
Avoid globbing hack.
Also reduces globbing in scope to all files in one folder, as in solid/solid-spec#148
1 parent 661ceda commit 3d43f72

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/handlers/get.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,26 @@ async function handler (req, res, next) {
132132
}
133133

134134
async function globHandler (req, res, next) {
135-
const ldp = req.app.locals.ldp
136-
// TODO: This is a hack, that does not check if the target file exists, as this is quite complex with globbing.
137-
// TODO: Proper support for this is not implemented, as globbing support might be removed in the future.
138-
const filename = ldp.resourceMapper.getFilePath(req)
135+
const { ldp } = req.app.locals
136+
137+
// Ensure this is a glob for all files in a single folder
138+
// https://github.com/solid/solid-spec/pull/148
139+
const requestUrl = await ldp.resourceMapper.getPublicUrl(req)
140+
if (!/^[^*]+\/\*$/.test(requestUrl)) {
141+
return next(error(404, 'Unsupported glob pattern'))
142+
}
143+
144+
// Extract the folder on the file system from the URL glob
145+
const folderUrl = requestUrl.substr(0, requestUrl.length - 1)
146+
const folderPath = (await ldp.resourceMapper.mapUrlToFile({ url: folderUrl })).path
139147

140148
const globOptions = {
141149
noext: true,
142150
nobrace: true,
143151
nodir: true
144152
}
145153

146-
glob(filename, globOptions, async (err, matches) => {
154+
glob(`${folderPath}*`, globOptions, async (err, matches) => {
147155
if (err || matches.length === 0) {
148156
debugGlob('No files matching the pattern')
149157
return next(error(404, 'No files matching glob pattern'))
@@ -178,8 +186,7 @@ async function globHandler (req, res, next) {
178186
})
179187
})))
180188

181-
const requestUri = await ldp.resourceMapper.getPublicUrl(req)
182-
const data = $rdf.serialize(undefined, globGraph, requestUri, 'text/turtle')
189+
const data = $rdf.serialize(undefined, globGraph, requestUrl, 'text/turtle')
183190
// TODO this should be added as a middleware in the routes
184191
res.setHeader('Content-Type', 'text/turtle')
185192
debugGlob('returning turtle')

test/integration/http-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe('HTTP APIs', function () {
323323
.expect(200, done)
324324
})
325325
it('should have glob support', function (done) {
326-
server.get('/sampleContainer/example*')
326+
server.get('/sampleContainer/*')
327327
.expect('content-type', /text\/turtle/)
328328
.expect(200)
329329
.expect((res) => {

0 commit comments

Comments
 (0)