Skip to content

Commit

Permalink
Avoid globbing hack.
Browse files Browse the repository at this point in the history
Also reduces globbing in scope to all files in one folder,
as in solid/solid-spec#148
  • Loading branch information
RubenVerborgh committed Jun 2, 2019
1 parent f246e44 commit bb5ba24
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions lib/handlers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,26 @@ async function handler (req, res, next) {
}

async function globHandler (req, res, next) {
const ldp = req.app.locals.ldp
// TODO: This is a hack, that does not check if the target file exists, as this is quite complex with globbing.
// TODO: Proper support for this is not implemented, as globbing support might be removed in the future.
const filename = ldp.resourceMapper.getFilePath(req)
const { ldp } = req.app.locals

// Ensure this is a glob for all files in a single folder
// https://github.com/solid/solid-spec/pull/148
const requestUrl = await ldp.resourceMapper.getRequestUrl(req)
if (!/^[^*]+\/\*$/.test(requestUrl)) {
return next(error(404, 'Unsupported glob pattern'))
}

// Extract the folder on the file system from the URL glob
const folderUrl = requestUrl.substr(0, requestUrl.length - 1)
const folderPath = (await ldp.resourceMapper.mapUrlToFile({ url: folderUrl })).path

const globOptions = {
noext: true,
nobrace: true,
nodir: true
}

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

const requestUri = ldp.resourceMapper.getRequestUrl(req)
const data = $rdf.serialize(undefined, globGraph, requestUri, 'text/turtle')
const data = $rdf.serialize(undefined, globGraph, requestUrl, 'text/turtle')
// TODO this should be added as a middleware in the routes
res.setHeader('Content-Type', 'text/turtle')
debugGlob('returning turtle')
Expand Down
2 changes: 1 addition & 1 deletion test/integration/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ describe('HTTP APIs', function () {
.expect(200, done)
})
it('should have glob support', function (done) {
server.get('/sampleContainer/example*')
server.get('/sampleContainer/*')
.expect('content-type', /text\/turtle/)
.expect(200)
.expect((res) => {
Expand Down

0 comments on commit bb5ba24

Please sign in to comment.