Skip to content

Commit d177717

Browse files
author
Nicola Greco
committed
Merge pull request #107 from deiu/master
Added proper content negotiation. Several smaller fixes.
2 parents 2dfb9bc + eadf46e commit d177717

10 files changed

Lines changed: 105 additions & 55 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
*~
33
*.swp
44
.tern-port
5+
npm-debug.log

‎lib/handlers/get.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var subscription = require('../subscription.js');
2020
var ldpVocab = require('../vocab/ldp.js');
2121
var turtleExtension = '.ttl';
2222

23+
// this should be moved to options
24+
var browseSkin = 'https://linkeddata.github.io/warp/#/list/';
25+
2326
function get(req, res, includeBody) {
2427
var ldp = req.app.locals.ldp;
2528
var uri = utils.uriBase(req);
@@ -32,6 +35,9 @@ function get(req, res, includeBody) {
3235
return subscription.subscribeToChanges(req, res);
3336
}
3437

38+
// Parse accept mime types into a priority (q) ordered array
39+
res.acceptTypes = header.negotiateContentType(req) || 'text/turtle';
40+
3541
// Set headers
3642
res.header('MS-Author-Via', 'SPARQL');
3743

@@ -109,6 +115,11 @@ function get(req, res, includeBody) {
109115
}
110116
}
111117

118+
// redirect to file browser if we got text/html with highest priority
119+
if (container && res.acceptTypes.indexOf('text/html') === 0) {
120+
return res.redirect(303, browseSkin + req.protocol + '/' + req.get('host') + req.originalUrl);
121+
}
122+
112123
// TODO this should be added as a middleware in the routes
113124
res.locals.turtleData = data;
114125
return parseLinkedData(req, res);
@@ -190,7 +201,7 @@ function parseLinkedData(req, res) {
190201
var uri = utils.uriBase(req);
191202
var turtleData = res.locals.turtleData;
192203

193-
var accept = header.parseAcceptHeader(req) || 'text/turtle';
204+
var accept = header.parseAcceptRDFHeader(req) || 'text/turtle';
194205
var baseUri = utils.filenameToBaseUri(filename, uri, ldp.root);
195206

196207
// Handle Turtle Accept header
@@ -228,7 +239,6 @@ function parseLinkedData(req, res) {
228239
});
229240
}
230241

231-
232242
function getHandler(req, res) {
233243
get(req, res, true);
234244
}

‎lib/handlers/post.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function handler(req, res) {
3434
if (contentType != 'text/turtle' &&
3535
contentType != 'text/n3' &&
3636
contentType != 'application/rdf+xml' &&
37-
contentType != 'application/json+ld' &&
37+
contentType != 'application/ld+json' &&
3838
contentType != 'application/nquads' &&
3939
contentType != 'application/n-quads') {
4040
debug("POST -- Invalid Content Type: " + contentType);

‎lib/header.js‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ var li = require('li');
22

33
var path = require('path');
44
var S = require('string');
5+
var Negotiator = require('negotiator');
56
var metadata = require('./metadata.js');
67
var utils = require('./utils.js');
78
var ldpVocab = require('./vocab/ldp.js');
89

910
function addLink(res, value, rel) {
1011
var oldLink = res.get('Link');
1112
if (oldLink === undefined)
12-
res.set('Link', '<' + value + '>; rel=\'' + rel + '\'');
13+
res.set('Link', '<' + value + '>; rel="' + rel + '"');
1314
else
1415
res.set('Link', oldLink + ', ' + '<' + value +
15-
'>; rel=\'' + rel + '\'');
16+
'>; rel="' + rel + '"');
1617
}
1718

1819
function addLinks(res, fileMetadata) {
@@ -74,25 +75,38 @@ function parseMetadataFromHeader(linkHeader) {
7475
return fileMetadata;
7576
}
7677

77-
function parseAcceptHeader(req) {
78+
79+
// Returns an array containing mime types from Accept header, ordered by priority
80+
// If no Accept value, serve turtle by default
81+
function negotiateContentType(req) {
82+
var negotiator = new Negotiator(req);
83+
return negotiator.mediaTypes();
84+
}
85+
86+
function parseAcceptRDFHeader(req) {
7887
var acceptFinalValue;
79-
var acceptHeader = req.get('Accept');
80-
if (acceptHeader === undefined) {
88+
var acceptHeader = negotiateContentType(req);
89+
if (!acceptHeader || acceptHeader.length === 0) {
8190
acceptFinalValue = undefined;
8291
} else {
83-
switch (acceptHeader) {
84-
case 'text/turtle':
85-
case 'applicatoin/x-turtle':
86-
case 'text/n3':
87-
case 'application/rdf+xml':
88-
case 'application/n3':
89-
case 'application/json+ld':
90-
case 'application/nquads':
91-
case 'application/n-quads':
92-
acceptFinalValue = acceptHeader;
92+
for (var i in acceptHeader) {
93+
switch (acceptHeader[i]) {
94+
case 'text/turtle':
95+
case 'application/x-turtle':
96+
case 'text/n3':
97+
case 'application/rdf+xml':
98+
case 'application/n3':
99+
case 'application/ld+json':
100+
case 'application/nquads':
101+
case 'application/n-quads':
102+
acceptFinalValue = acceptHeader[i];
103+
break;
104+
default:
105+
acceptFinalValue = undefined;
106+
}
107+
if (acceptFinalValue !== undefined) {
93108
break;
94-
default:
95-
acceptFinalValue = undefined;
109+
}
96110
}
97111
}
98112
return acceptFinalValue;
@@ -101,5 +115,6 @@ function parseAcceptHeader(req) {
101115
module.exports.addLink = addLink;
102116
module.exports.addLinks = addLinks;
103117
module.exports.parseMetadataFromHeader = parseMetadataFromHeader;
104-
module.exports.parseAcceptHeader = parseAcceptHeader;
118+
module.exports.parseAcceptRDFHeader = parseAcceptRDFHeader;
105119
module.exports.linksHandler = linksHandler;
120+
module.exports.negotiateContentType = negotiateContentType;

‎lib/ldp.js‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
177177
if (S(file).endsWith(ldp.suffixMeta) || S(file).endsWith(ldp.suffixAcl)) {
178178
return callback();
179179
}
180-
180+
181181
// Get file stats
182182
ldp.stat(container + file, function (err, stats) {
183183
if (err) {
@@ -186,23 +186,23 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
186186
}
187187

188188
var fileSubject = file + (stats.isDirectory() ? '/' : '');
189-
var fileBaseUri = utils.filenameToBaseUri(file, uri, ldp.root);
189+
var fileBaseUri = utils.filenameToBaseUri(fileSubject, uri, ldp.root);
190190

191191
// Add fileStats to resource Graph
192192
addStats(resourceGraph, fileSubject, stats);
193193

194194
// Add to `contains` list
195195
resourceGraph.add(
196-
resourceGraph.sym(baseUri),
196+
resourceGraph.sym(''),
197197
ns.ldp('contains'),
198-
resourceGraph.sym(file));
198+
resourceGraph.sym(fileSubject));
199199

200200
// Set up a metaFile path
201201
var metaFile = container + file +
202202
(stats.isDirectory() ? '/' : '') +
203203
(S(file).endsWith(turtleExtension) ? '' : ldp.suffixMeta);
204204

205-
getMetadataGraph(metaFile, fileBaseUri, function(err, metadataGraph) {
205+
getMetadataGraph(metaFile, baseUri, function(err, metadataGraph) {
206206
if (err) {
207207
metadataGraph = $rdf.graph();
208208
}
@@ -233,7 +233,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
233233
// Add type from metadataGraph
234234
var typeStatements = metadataGraph
235235
.statementsMatching(
236-
metadataGraph.sym(fileBaseUri),
236+
metadataGraph.sym(baseUri),
237237
ns.rdf('type'),
238238
undefined)
239239
.forEach(function (typeStatement) {
@@ -275,7 +275,21 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
275275
function (next) {
276276
ldp.stat(filename, function(err, containerStats) {
277277
if (!err) {
278-
addStats(resourceGraph, baseUri, containerStats);
278+
addStats(resourceGraph, '', containerStats);
279+
resourceGraph.add(
280+
resourceGraph.sym(''),
281+
ns.rdf('type'),
282+
ns.ldp('BasicContainer'));
283+
284+
resourceGraph.add(
285+
resourceGraph.sym(''),
286+
ns.rdf('type'),
287+
ns.ldp('Container'));
288+
289+
resourceGraph.add(
290+
resourceGraph.sym(''),
291+
ns.rdf('type'),
292+
ns.stat('Directory'));
279293
}
280294
next();
281295
});

‎lib/login.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function loginHandler(req, res, next) {
3232
message = 'Your certificate public key is not the one of the FOAF file';
3333
break;
3434
case 'profileAllKeysWellFormed':
35-
message = "Missformed WebID";
35+
message = "Certificate public key does not match the one found in the WebID profile.";
3636
break;
3737
default:
3838
message = "Unknown WebID error";

‎lib/parse.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function parseHandler(req, res, next) {
1919

2020
function convertToTurtle(rawDocument, req,
2121
convertCallback) {
22-
if (req.is('application/json+ld') ||
22+
if (req.is('application/ld+json') ||
2323
req.is('application/nquads') || req.is('application/n-quads')) {
2424
var contentType = req.get('content-type').split(';')[0].trim();
2525
parse(rawDocument, contentType, convertCallback);
@@ -34,7 +34,7 @@ function parse(rawDocument, contentType, convertCallback) {
3434
var triples = [];
3535
var prefixes = {};
3636

37-
if (contentType === 'application/json+ld') {
37+
if (contentType === 'application/ld+json') {
3838
var jsonDocument;
3939
try {
4040
jsonDocument = JSON.parse(rawDocument);

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"request": "^2.58.0",
4848
"response-time": "^2.3.1",
4949
"string": "^3.3.0",
50-
"webid": "^0.2.2"
50+
"webid": "^0.2.2",
51+
"negotiator": "^0.5.3"
5152
},
5253
"devDependencies": {
5354
"chai": "^3.0.0",

‎test/formats.js‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ describe('formats', function () {
1414

1515
var server = supertest(address);
1616
describe('HTML', function() {
17-
it('Should return "Hello, World!"', function(done) {
17+
it('Should return HTML containing "Hello, World!" if Accept is set to text/html', function(done) {
1818
server.get('/hello.html')
19+
.set('accept', 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5')
1920
.expect('Content-type', /text\/html/)
2021
.expect(/Hello, world!/)
2122
.expect(200, done);
@@ -26,35 +27,43 @@ describe('formats', function () {
2627
var isValidJSON = function(res) {
2728
var json = JSON.parse(res.text);
2829
};
29-
it('Should return JSON-LD document', function(done) {
30+
it('Should return JSON-LD document if Accept is set to application/ld+json', function(done) {
3031
server.get('/patch-5-initial.ttl')
31-
.set('accept', 'application/json+ld')
32-
.expect('content-type', /application\/json\+ld/)
32+
.set('accept', 'application/ld+json;q=0.9,text/turtle;q=0.8,text/plain;q=0.7,*/*;q=0.5')
33+
.expect('content-type', /application\/ld\+json/)
3334
.expect(200, done);
3435
});
35-
it('Should return valid JSON', function(done) {
36+
it('Should return valid JSON if Accept is set to JSON-LD', function(done) {
3637
server.get('/patch-5-initial.ttl')
37-
.set('accept', 'application/json+ld')
38+
.set('accept', 'application/ld+json;q=0.9,text/turtle;q=0.8,text/plain;q=0.7,*/*;q=0.5')
3839
.expect(isValidJSON)
3940
.expect(200, done);
4041
});
4142
});
4243

4344
describe('N-Quads', function() {
44-
it('Should return N-Quads document', function(done) {
45+
it('Should return N-Quads document is Accept is set to application/n-quads', function(done) {
4546
server.get('/patch-5-initial.ttl')
46-
.set('accept', 'application/n-quads')
47+
.set('accept', 'application/n-quads;q=0.9,text/turtle;q=0.8,text/plain;q=0.7,*/*;q=0.5')
4748
.expect('content-type', /application\/n-quads/)
4849
.expect(200, done);
4950
});
5051
});
5152

5253
describe('n3', function() {
53-
it('Should return turtle document if content-type set to n3', function(done) {
54+
it('Should return turtle document if Accept is set to text/n3', function(done) {
5455
server.get('/patch-5-initial.ttl')
55-
.set('accept', 'text/n3')
56+
.set('accept', 'text/n3;q=0.9,text/turtle;q=0.8,text/plain;q=0.7,*/*;q=0.5')
5657
.expect('content-type', /text\/n3/)
5758
.expect(200, done);
5859
});
5960
});
61+
62+
describe('none', function() {
63+
it('Should return turtle document if no Accept header is set', function(done) {
64+
server.get('/patch-5-initial.ttl')
65+
.expect('content-type', /text\/turtle/)
66+
.expect(200, done);
67+
});
68+
});
6069
});

0 commit comments

Comments
 (0)