Skip to content

Commit ebf7ea6

Browse files
author
Andrei
committed
Merge pull request #84 from nicola/fixingPaths
Fixing abs/originalUrl path issue
2 parents 5023db5 + 52897b3 commit ebf7ea6

10 files changed

Lines changed: 73 additions & 49 deletions

File tree

‎acl.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function allow(mode, req, res) {
3838
for (var i = 0; i < depth.length; i++) {
3939
var pathAcl = S(filepath).endsWith(options.suffixAcl) ?
4040
filepath : filepath + options.suffixAcl;
41-
var uri = req.protocol + '://' + req.get('host') + options.mount ;
41+
var uri = file.uriBase(req);
4242
var pathUri = file.filenameToBaseUri(filepath, uri, options.root);
4343
relativePath = path.relative(options.root, filepath);
4444

‎fileStore.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ function filenameToBaseUri(filename, uri, base) {
3030
return uri + '/' + uriPath;
3131
}
3232

33+
function uriAbs(req) {
34+
return req.protocol + '://' + req.get('host');
35+
}
36+
37+
function uriBase(req) {
38+
return uriAbs(req) + (req.baseUrl || '');
39+
}
3340

3441
exports.uriToFilename = uriToFilename;
3542
exports.uriToRelativeFilename = uriToRelativeFilename;
3643
exports.filenameToBaseUri = filenameToBaseUri;
44+
exports.uriAbs = uriAbs;
45+
exports.uriBase = uriBase;

‎handlers/delete.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var metadata = require('../metadata.js');
88

99
// Delete a container or resource
1010
function handler(req, res) {
11-
debug('DELETE -- ' + req.path);
11+
debug('DELETE -- ' + req.originalUrl);
1212

1313
var options = req.app.locals.ldp;
1414
var filename = file.uriToFilename(req.path, options.root);

‎handlers/get.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ var turtleExtension = '.ttl';
2323

2424
function get(req, res, includeBody) {
2525
var options = req.app.locals.ldp;
26-
var uri = req.protocol + '://' + req.get('host') + options.mount; // URI to mount point only
26+
var uri = file.uriBase(req);
2727

2828
// Add request to subscription service
2929
if (req.path.slice(-options.suffixChanges.length) ===
3030
options.suffixChanges) {
31-
debug("GET -- Subscribed to ", req.path);
31+
debug("GET -- Subscribed to ", req.originalUrl);
3232
return subscription.subscribeToChanges(req, res);
3333
}
3434

@@ -39,16 +39,16 @@ function get(req, res, includeBody) {
3939
if (options.live) {
4040
// Note not yet in
4141
// http://www.iana.org/assignments/link-relations/link-relations.xhtml
42-
header.addLink(res, req.path + options.suffixChanges, 'changes');
42+
header.addLink(res, req.originalUrl + options.suffixChanges, 'changes');
4343
// res.header('Link' , '' + req.path + options.suffixSSE + ' ; rel=events' );
4444
// overwrites the pevious
45-
res.header('Updates-Via', req.path + options.suffixChanges);
45+
res.header('Updates-Via', req.originalUrl + options.suffixChanges);
4646
}
4747

4848
if (includeBody) {
49-
debug('GET -- ' + req.path);
49+
debug('GET -- ' + req.originalUrl);
5050
} else {
51-
debug('HEAD -- ' + req.path);
51+
debug('HEAD -- ' + req.originalUrl);
5252
}
5353

5454
var filename = file.uriToFilename(req.path, options.root);

‎handlers/patch.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ var options = require('../options.js');
1212

1313
function handler(req, res) {
1414
var options = req.app.locals.ldp;
15-
debug('PATCH -- ' +req.path);
15+
debug('PATCH -- ' + req.originalUrl);
1616
debug('PATCH -- text length: ' + (req.text ? req.text.length : 'undefined2'));
1717
res.header('MS-Author-Via' , 'SPARQL' );
1818
var filename = file.uriToFilename(req.path, options.root);
1919
var patchContentType = req.get('content-type').split(';')[0].trim(); // Ignore parameters
2020
var targetContentType = mime.lookup(filename);
21-
var targetURI = req.protocol + '://' + req.get('host') + options.mount + req.path;
21+
var targetURI = file.uriAbs(req) + req.originalUrl;
2222
var patchURI = targetURI ; // @@@ beware the triples from the patch ending up in the same place
2323
debug("PATCH -- Content-type " + patchContentType + " patching target " + targetContentType + " <" + targetURI + '>');
2424
var targetKB = $rdf.graph();

‎handlers/post.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function handler(req, res) {
7777
var resourceGraph = $rdf.graph();
7878
// TODO make sure correct text is selected
7979
var requestText = req.convertedText || req.text;
80-
var uri = req.protocol + '://' + req.get('host') + options.mount;
80+
var uri = file.uriBase(req);
8181
var resourceBaseUri = file.filenameToBaseUri(
8282
resourcePath,
8383
uri,

‎handlers/put.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var options = require('../options.js');
1414

1515
function handler(req, res) {
1616
var options = req.app.locals.ldp;
17-
debug("PUT -- Request path: " + req.path);
17+
debug("PUT -- Request path: " + req.originalUrl);
1818
debug("PUT -- Text length: " + (req.text ? req.text.length : 'undefined'));
1919
res.header('MS-Author-Via' , 'SPARQL' );
2020

‎options.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ function params(argv) {
2121
opts.cert = argv.cert;
2222
opts.key = argv.key;
2323
opts.mount = argv.mount || '/';
24+
// Removing ending '/'
25+
if (opts.mount.length > 1 &&
26+
opts.mount[opts.mount.length - 1] === '/') {
27+
opts.mount = opts.mount.slice(0, -1);
28+
}
29+
2430
opts.verbose = argv.verbose;
2531
opts.webid = argv.webid;
2632

‎subscription.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.subscribeToChanges_SSE = function(req, res) {
1717

1818
var messageCount;
1919
debug("Server Side Events subscription");
20-
var targetPath = req.path.slice(0, - options.suffixChanges.length); // lop off ',events'
20+
var targetPath = req.originalUrl.slice(0, - options.suffixChanges.length); // lop off ',events'
2121
if (SSEsubscriptions[targetPath] === undefined) {
2222
SSEsubscriptions[targetPath] = redis.createClient();
2323
}
@@ -61,7 +61,7 @@ exports.subscribeToChanges_SSE = function(req, res) {
6161
exports.publishDelta_SSE = function (req, res, patchKB, targetURI){
6262
// @@ TODO
6363
var options = req.app.locals.ldp;
64-
var targetPath = req.path.slice(0, - options.suffixChanges.length); // lop off ',changes'
64+
var targetPath = req.originalUrl.slice(0, - options.suffixChanges.length); // lop off ',changes'
6565
var publisherClient = SSEsubscriptions[targetPath];
6666
publisherClient.publish( 'updates', ('"' + targetPath + '" data changed visited') );
6767
};
@@ -74,7 +74,7 @@ var DelayedResponse = require('http-delayed-response');
7474

7575
exports.subscribeToChangesLongPoll = function(req, res) {
7676
var options = req.app.locals.ldp;
77-
var targetPath = req.path.slice(0, - options.suffixChanges.length); // lop off ',changes'
77+
var targetPath = req.originalUrl.slice(0, - options.suffixChanges.length); // lop off ',changes'
7878
if (subscriptions[targetPath] === undefined) {
7979
subscriptions[targetPath] = [];
8080
}
@@ -144,7 +144,7 @@ exports.publishDelta = function (req, res, patchKB, targetURI){
144144
patchKB.add(operation, PATCH('logged'), new Date()); // @@ also add user
145145
var patchData = $rdf.serialize(undefined, patchKB, targetURI, 'text/n3');
146146

147-
debug("Distributing change to " + req.path + ", patch is: " );
147+
debug("Distributing change to " + req.originalUrl + ", patch is: " );
148148
debug("[[[" + patchData + "]]]\n");
149149

150150
this.publishDelta_LongPoll(req, res, patchData, targetURI);
@@ -153,10 +153,10 @@ exports.publishDelta = function (req, res, patchKB, targetURI){
153153

154154
exports.publishDelta_LongPoll = function (req, res, patchData, targetURI){
155155
var options = req.app.locals.ldp;
156-
debug(" Long poll change subscription count " + (subscriptions[req.path] || []).length);
157-
if (! subscriptions[req.path]) return;
158-
subscriptions[req.path].map(function(subscription){
159-
debug(" Long poll change to " + req.path);
156+
debug(" Long poll change subscription count " + (subscriptions[req.originalUrl] || []).length);
157+
if (! subscriptions[req.originalUrl]) return;
158+
subscriptions[req.originalUrl].map(function(subscription){
159+
debug(" Long poll change to " + req.originalUrl);
160160
if (options.leavePatchConnectionOpen) {
161161
subscription.response.write(patchData);
162162
} else {
@@ -166,6 +166,6 @@ exports.publishDelta_LongPoll = function (req, res, patchData, targetURI){
166166
}
167167
});
168168

169-
subscriptions[req.path] = []; // one-off polll
169+
subscriptions[req.originalUrl] = []; // one-off polll
170170
debug("LONG POLL : Now NO subscriptions for " + targetURI);
171171
};

‎test/params.js‎

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,43 @@ function write (text, file) {
2525

2626
describe('params', function () {
2727

28-
describe('uri', function () {
29-
30-
// describe('not passed', function () {
31-
32-
// var ldp = ldnode.createServer({
33-
// base: __dirname
34-
// });
35-
// ldp.listen(3456);
36-
37-
// it('should be the proxy value if exist', function (done) {
38-
// done();
39-
// })
40-
41-
// it('should be localhost if no proxy', function (done) {
42-
// done();
43-
// })
44-
// })
45-
46-
// describe('passed', function() {
47-
// var ldp = ldnode.createServer({
48-
// uri: 'http://example.com',
49-
// base: __dirname
50-
// });
51-
// ldp.listen(3456);
52-
// it ('should not use proxy', function (done) {
53-
// done();
54-
// })
55-
// })
28+
describe('mount', function () {
29+
30+
describe('not passed', function () {
31+
it('should fallback on /', function (done) {
32+
var ldp = ldnode();
33+
assert.equal(ldp.locals.ldp.mount, '/');
34+
done();
35+
});
36+
37+
});
38+
39+
describe('passed', function() {
40+
it ('should properly set the opts.mount', function (done) {
41+
var ldp1 = ldnode({
42+
mount: '/'
43+
});
44+
assert.equal(ldp1.locals.ldp.mount, '/');
45+
46+
var ldp2 = ldnode({
47+
mount: '/test'
48+
});
49+
assert.equal(ldp2.locals.ldp.mount, '/test');
50+
51+
done();
52+
});
53+
it('should drop tha trailing /', function () {
54+
var ldp1 = ldnode({
55+
mount: '/test/'
56+
});
57+
assert.equal(ldp1.locals.ldp.mount, '/test');
58+
59+
var ldp2 = ldnode({
60+
mount: '/test/test'
61+
});
62+
assert.equal(ldp1.locals.ldp.mount, '/test');
63+
});
64+
});
5665
});
5766

5867
describe('root', function () {

0 commit comments

Comments
 (0)