Skip to content

Commit a8cc800

Browse files
author
deiu
committed
Merged master
2 parents 68aa040 + 03a9b37 commit a8cc800

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

lib/handlers/get.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ function handler (req, res, next) {
3030
var ldp = req.app.locals.ldp
3131
var includeBody = req.method === 'GET'
3232
var negotiator = new Negotiator(req)
33-
var requestedType = negotiator.mediaType()
34-
var possibleRDFType = negotiator.mediaType(RDFs)
3533
var baseUri = utils.uriBase(req)
3634
var path = res.locals.path || req.path
35+
var requestedType = negotiator.mediaType()
36+
var possibleRDFType = negotiator.mediaType(RDFs)
37+
// Fallback to text/turtle if content type is unknown
38+
possibleRDFType = (!possibleRDFType) ? 'text/turtle' : possibleRDFType
3739

3840
res.header('MS-Author-Via', 'SPARQL')
3941

@@ -65,11 +67,14 @@ function handler (req, res, next) {
6567
}
6668

6769
// Handle defaultApp
68-
if (container &&
69-
requestedType.indexOf('text/html') === 0 &&
70-
ldp.defaultApp) {
71-
var address = req.protocol + '/' + req.get('host') + req.originalUrl
72-
return res.redirect(303, ldp.defaultApp + address)
70+
if (requestedType.indexOf('text/html') === 0) {
71+
if (container && ldp.defaultApp) {
72+
var address = req.protocol + '/' + req.get('host') + req.originalUrl
73+
return res.redirect(303, ldp.defaultApp + address)
74+
}
75+
// Serve (pipe) resource as requested
76+
res.setHeader('Content-Type', contentType)
77+
return stream.pipe(res)
7378
}
7479

7580
// If request accepts the content-type we found
@@ -82,7 +87,7 @@ function handler (req, res, next) {
8287
// If it is not in our RDFs we can't even translate,
8388
// Sorry, we can't help
8489
if (!possibleRDFType) {
85-
return next(error(406, 'Cannot server your type'))
90+
return next(error(406, 'Cannot serve requested type: ' + contentType))
8691
}
8792

8893
// Translate from the contentType found to the possibleRDFType desired

lib/handlers/try-index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function handler (req, res, next) {
2424
// Check if file exists in first place
2525
ldp.exists(req.hostname, res.locals.path, function (err) {
2626
if (err) {
27-
res.locals.path = req.path
27+
res.locals.path = res.path
2828
return next()
2929
}
3030
debug('found an index for current path')

test/http.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ describe('HTTP APIs', function () {
204204
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
205205
.expect(200, done)
206206
})
207+
it('should serve the right content type if requested as text/html', function (done) {
208+
server.get('/sampleContainer/example1.ttl')
209+
.set('Accept', 'text/html')
210+
.expect('content-type', /text\/turtle/)
211+
.expect(200, done)
212+
})
213+
it('should redirect to defaultApp if container was requested as text/html', function (done) {
214+
server.get('/sampleContainer/')
215+
.set('Accept', 'text/html')
216+
.expect('content-type', /text\/html/)
217+
.expect(200, done) // Can't check for 303 because of internal redirects
218+
})
207219
it('should return 404 for non-existent resource', function (done) {
208220
server.get('/invalidfile.foo')
209221
.expect(404, done)

0 commit comments

Comments
 (0)