From e6d6bb202d9fa21fb0ccb743395eeb889b9a1ebb Mon Sep 17 00:00:00 2001 From: deiu Date: Wed, 27 Jan 2016 16:41:56 -0500 Subject: [PATCH 1/3] Fixes #219; serve requested resource if missing mime type --- lib/handlers/get.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/handlers/get.js b/lib/handlers/get.js index 6cd8cb187..404867cec 100644 --- a/lib/handlers/get.js +++ b/lib/handlers/get.js @@ -30,10 +30,12 @@ function handler (req, res, next) { var ldp = req.app.locals.ldp var includeBody = req.method === 'GET' var negotiator = new Negotiator(req) - var requestedType = negotiator.mediaType() - var possibleRDFType = negotiator.mediaType(RDFs) var baseUri = utils.uriBase(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') @@ -65,11 +67,14 @@ function handler (req, res, next) { } // Handle defaultApp - if (container && - requestedType.indexOf('text/html') === 0 && - ldp.defaultApp) { - var address = req.protocol + '/' + req.get('host') + req.originalUrl - return res.redirect(303, ldp.defaultApp + address) + if (requestedType.indexOf('text/html') === 0) { + if (container && ldp.defaultApp) { + var address = req.protocol + '/' + req.get('host') + req.originalUrl + return res.redirect(303, ldp.defaultApp + address) + } + // Serve (pipe) resource as requested + res.setHeader('Content-Type', contentType) + return stream.pipe(res) } // If request accepts the content-type we found @@ -82,7 +87,7 @@ function handler (req, res, next) { // If it is not in our RDFs we can't even translate, // Sorry, we can't help if (!possibleRDFType) { - return next(error(406, 'Cannot server your type')) + return next(error(406, 'Cannot serve requested type: ' + contentType)) } // Translate from the contentType found to the possibleRDFType desired From 7e6439a281ce6e8e1eab6f68be3564aa763641e3 Mon Sep 17 00:00:00 2001 From: deiu Date: Wed, 27 Jan 2016 16:58:29 -0500 Subject: [PATCH 2/3] Fix path if falling back to get handler --- lib/handlers/try-index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/handlers/try-index.js b/lib/handlers/try-index.js index c1b12dd81..195ac83cb 100644 --- a/lib/handlers/try-index.js +++ b/lib/handlers/try-index.js @@ -20,7 +20,10 @@ function handler (req, res, next) { debug('looking for ' + res.locals.path) // Check if file exists in first place ldp.exists(req.hostname, res.locals.path, function (err) { - if (err) return next() + if (err) { + res.locals.path = res.path + return next() + } debug('found an index for current path') // Since it exists, can the user read this? acl.allow('Read')(req, res, function (err) { From 73c20eec3bf036bed60c548032873e40a6beb972 Mon Sep 17 00:00:00 2001 From: deiu Date: Wed, 27 Jan 2016 16:58:58 -0500 Subject: [PATCH 3/3] Added tests for text/html conneg --- test/http.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/http.js b/test/http.js index 5ca259908..a5b003657 100644 --- a/test/http.js +++ b/test/http.js @@ -204,6 +204,18 @@ describe('HTTP APIs', function () { .expect('Link', /; rel="type"/) .expect(200, done) }) + it('should serve the right content type if requested as text/html', function (done) { + server.get('/sampleContainer/example1.ttl') + .set('Accept', 'text/html') + .expect('content-type', /text\/turtle/) + .expect(200, done) + }) + it('should redirect to defaultApp if container was requested as text/html', function (done) { + server.get('/sampleContainer/') + .set('Accept', 'text/html') + .expect('content-type', /text\/html/) + .expect(200, done) // Can't check for 303 because of internal redirects + }) it('should return 404 for non-existent resource', function (done) { server.get('/invalidfile.foo') .expect(404, done)