-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathget.js
More file actions
216 lines (188 loc) · 6.55 KB
/
get.js
File metadata and controls
216 lines (188 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
module.exports = handler
var fs = require('fs')
var glob = require('glob')
var _path = require('path')
var $rdf = require('rdflib')
var S = require('string')
var async = require('async')
var Negotiator = require('negotiator')
const url = require('url')
const mime = require('mime-types')
var debug = require('debug')('solid:get')
var debugGlob = require('debug')('solid:glob')
var acl = require('./allow')
var utils = require('../utils.js')
var translate = require('../utils.js').translate
var error = require('../http-error')
const RDFs = require('../ldp').RDF_MIME_TYPES
function handler (req, res, next) {
var ldp = req.app.locals.ldp
var includeBody = req.method === 'GET'
var negotiator = new Negotiator(req)
var baseUri = utils.getFullUri(req)
var path = res.locals.path || req.path
var requestedType = negotiator.mediaType()
var possibleRDFType = negotiator.mediaType(RDFs)
// Fallback to text/turtle if content type is unknown
possibleRDFType = (!possibleRDFType) ? 'text/turtle' : possibleRDFType
res.header('MS-Author-Via', 'SPARQL')
// Set live updates
if (ldp.live) {
res.header('Updates-Via', req.protocol.replace(/^http/, 'ws') + '://' + req.get('host'))
}
debug(req.originalUrl + ' on ' + req.hostname)
var options = {
'hostname': req.hostname,
'path': path,
'baseUri': baseUri,
'includeBody': includeBody,
'possibleRDFType': possibleRDFType,
'range': req.headers.range
}
ldp.get(options, function (err, ret) {
// use globHandler if magic is detected
if (err && err.status === 404 && glob.hasMagic(path)) {
debug('forwarding to glob request')
return globHandler(req, res, next)
}
// Handle error
if (err) {
debug(req.method + ' -- Error: ' + err.status + ' ' + err.message)
return next(err)
}
if (ret) {
var stream = ret.stream
var contentType = ret.contentType
var container = ret.container
var contentRange = ret.contentRange
var chunksize = ret.chunksize
}
// Till here it must exist
if (!includeBody) {
debug('HEAD only')
res.sendStatus(200)
return next()
}
// Handle fileBrowser and dataBrowser
if (requestedType && requestedType.includes('text/html')) {
if (container && ldp.fileBrowser) {
var address = req.protocol + '/' + req.get('host') + req.originalUrl
return res.redirect(303, ldp.fileBrowser + address)
}
let mimeTypeByExt = mime.lookup(_path.basename(path))
let isHtmlResource = mimeTypeByExt && mimeTypeByExt.includes('html')
let useDataBrowser = RDFs.includes(contentType) &&
!isHtmlResource && // filter out .html which IS an RDF type, but does not use data browser
!ldp.suppressDataBrowser &&
ldp.dataBrowserPath
if (useDataBrowser) {
res.set('Content-Type', 'text/html')
var defaultDataBrowser = _path.join(__dirname, '../../static/databrowser.html')
var dataBrowserPath = ldp.dataBrowserPath === 'default' ? defaultDataBrowser : ldp.dataBrowserPath
debug(' sending data browser file: ' + dataBrowserPath)
res.sendFile(dataBrowserPath)
return
} else {
res.setHeader('Content-Type', contentType)
return stream.pipe(res)
}
}
// If request accepts the content-type we found
if (negotiator.mediaType([contentType])) {
debug('no translation necessary ' + contentType)
res.setHeader('Content-Type', contentType)
if (contentRange) {
var headers = { 'Content-Range': contentRange, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize }
res.writeHead(206, headers)
return stream.pipe(res)
} else {
return stream.pipe(res)
}
}
// If it is not in our RDFs we can't even translate,
// Sorry, we can't help
if (!possibleRDFType) {
return next(error(406, 'Cannot serve requested type: ' + contentType))
}
// Translate from the contentType found to the possibleRDFType desired
translate(stream, baseUri, contentType, possibleRDFType, function (err, data) {
if (err) {
debug('error translating: ' + req.originalUrl + ' ' + contentType + ' -> ' + possibleRDFType + ' -- ' + 500 + ' ' + err.message)
return next(error(500, 'Error translating between RDF formats'))
}
debug(req.originalUrl + ' translating ' + contentType + ' -> ' + possibleRDFType)
res.setHeader('Content-Type', possibleRDFType)
res.send(data)
return next()
})
})
}
function globHandler (req, res, next) {
var ldp = req.app.locals.ldp
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/'
var filename = utils.uriToFilename(req.path, root)
var uri = utils.getFullUri(req)
const requestUri = url.resolve(uri, req.path)
var globOptions = {
noext: true,
nobrace: true,
nodir: true
}
glob(filename, globOptions, function (err, matches) {
if (err || matches.length === 0) {
debugGlob('No files matching the pattern')
return next(error(404, 'No files matching glob pattern'))
}
// Matches found
var globGraph = $rdf.graph()
let reqOrigin = utils.getBaseUri(req)
debugGlob('found matches ' + matches)
async.each(matches, function (match, done) {
var baseUri = utils.filenameToBaseUri(match, reqOrigin, root)
fs.readFile(match, {encoding: 'utf8'}, function (err, fileData) {
if (err) {
debugGlob('error ' + err)
return done(null)
}
aclAllow(match, req, res, function (allowed) {
if (!S(match).endsWith('.ttl') || !allowed) {
return done(null)
}
try {
$rdf.parse(
fileData,
globGraph,
baseUri,
'text/turtle')
} catch (parseErr) {
debugGlob('error in parsing the files' + parseErr)
}
return done(null)
})
})
}, function () {
var data = $rdf.serialize(
undefined,
globGraph,
requestUri,
'text/turtle')
// TODO this should be added as a middleware in the routes
res.setHeader('Content-Type', 'text/turtle')
debugGlob('returning turtle')
res.send(data)
return next()
})
})
}
function aclAllow (match, req, res, callback) {
var ldp = req.app.locals.ldp
if (!ldp.webid) {
return callback(true)
}
var root = ldp.idp ? ldp.root + req.hostname + '/' : ldp.root
var relativePath = '/' + _path.relative(root, match)
res.locals.path = relativePath
acl.allow('Read', req, res, function (err) {
callback(err)
})
}