Skip to content

Commit 6241b69

Browse files
committed
moving tests and renaming xssProxy to proxy
2 parents c7a4fde + 3d82b7c commit 6241b69

10 files changed

Lines changed: 150 additions & 71 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Linked Data Platform server based on [rdflib.js](https://github.com/linkeddata/r
1414
- [x] WebID+TLS Authentication
1515
- [x] Mount as express' router
1616
- [x] Command line tool
17-
- [ ] Real-time live updates (using websokets)
17+
- [x] Real-time live updates (using websokets)
1818

1919

2020
## Install
@@ -44,7 +44,8 @@ In case the `settings` is not passed, then it will start with the following defa
4444
webid: false, // Enable WebID+TLS authentication
4545
suffixAcl: '.acl', // Suffix for acl files
4646
suffixChanges: '.changes', // Suffix for acl files
47-
suffixSSE: '.events' // Suffix for SSE files
47+
suffixSSE: '.events', // Suffix for SSE files
48+
proxy: false // Where to mount the proxy
4849
}
4950
```
5051

bin/ldnode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ var argv = require('nomnom')
5454
help: 'HTTP Session secret key (e.g. "your secret phrase")',
5555
abbr: 's'
5656
})
57-
.option('xssProxy', {
58-
full: 'xss-proxy',
59-
help: 'Use a proxy on example.tld/xssProxyPath',
57+
.option('proxy', {
58+
full: 'proxy',
59+
help: 'Use a proxy on example.tld/proxyPath',
6060
abbr: 'P'
6161
})
6262
.option('noLive', {

index.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ var putHandler = require('./lib/handlers/put.js');
3232
var deleteHandler = require('./lib/handlers/delete.js');
3333
var patchHandler = require('./lib/handlers/patch.js');
3434

35+
// Setting up cors
36+
var corsSettings = cors({
37+
methods: [
38+
'OPTIONS', 'HEAD', 'GET',
39+
'PATCH', 'POST', 'PUT', 'DELETE'
40+
],
41+
exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
42+
credentials: true,
43+
maxAge: 1728000,
44+
origin: true
45+
});
46+
3547
function ldnode (argv) {
3648
var ldp = new LDP(argv);
3749
var app = express();
@@ -47,7 +59,7 @@ function ldnode (argv) {
4759
}));
4860

4961
// Adding proxy
50-
if (ldp.xssProxy) {
62+
if (ldp.proxy) {
5163
proxy(app, ldp.proxyFilter);
5264
}
5365

@@ -117,7 +129,7 @@ function createServer(argv) {
117129

118130
function proxy (app, path) {
119131
debug.settings('XSS Proxy listening to ' + path);
120-
app.get(path, function (req, res) {
132+
app.get(path, corsSettings, function (req, res) {
121133
debug.settings('originalUrl: ' + req.originalUrl);
122134
var uri = req.query.uri;
123135
if (!uri) {
@@ -138,16 +150,7 @@ function routes () {
138150
router.use(header.linksHandler);
139151

140152
// Setting CORS
141-
router.use(cors({
142-
methods: [
143-
'OPTIONS', 'HEAD', 'GET',
144-
'PATCH', 'POST', 'PUT', 'DELETE'
145-
],
146-
exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
147-
credentials: true,
148-
maxAge: 1728000,
149-
origin: true
150-
}));
153+
router.use(corsSettings);
151154

152155
router.use('/*', function(req, res, next) {
153156
getRawBody(req,

lib/handlers/get.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,6 @@ function get(req, res, includeBody) {
5757
debug('HEAD -- ' + req.originalUrl);
5858
}
5959

60-
// Add ACL and Meta Link in header
61-
var aclLink = utils.getResourceLink(
62-
filename, uri,
63-
ldp.root, ldp.suffixAcl,
64-
ldp.suffixMeta);
65-
66-
var metaLink = utils.getResourceLink(
67-
filename, uri,
68-
ldp.root, ldp.suffixMeta,
69-
ldp.suffixAcl);
70-
71-
header.addLink(res, aclLink, 'acl');
72-
header.addLink(res, metaLink, 'describedBy');
73-
7460
// Get resource or container
7561
ldp.get(filename, uri, includeBody, function(err, data, container) {
7662

lib/header.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var path = require('path');
44
var S = require('string');
55
var Negotiator = require('negotiator');
66
var metadata = require('./metadata.js');
7+
var ldp = require('./ldp.js');
78
var utils = require('./utils.js');
89
var ldpVocab = require('./vocab/ldp.js');
910

@@ -30,6 +31,7 @@ function addLinks(res, fileMetadata) {
3031
}
3132

3233
function linksHandler(req, res, next) {
34+
var uri = utils.uriBase(req);
3335
var ldp = req.app.locals.ldp;
3436
var filename = utils.uriToFilename(req.url, ldp.root);
3537

@@ -45,6 +47,11 @@ function linksHandler(req, res, next) {
4547
} else {
4648
fileMetadata.isResource = true;
4749
}
50+
51+
// Add ACL and Meta Link in header
52+
addLink(res, utils.pathBasename(req.path) + ldp.suffixAcl, 'acl');
53+
addLink(res, utils.pathBasename(req.path) + ldp.suffixMeta, 'describedBy');
54+
// Add other Link headers
4855
addLinks(res, fileMetadata);
4956
next();
5057
}

lib/ldp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ function LDP(argv) {
5353
}
5454

5555
ldp.pathFilter = regexp().start(ldp.mount).toRegExp();
56-
ldp.xssProxy = argv.xssProxy;
57-
ldp.proxyFilter = regexp().start(ldp.xssProxy).toRegExp();
56+
ldp.proxy = argv.proxy;
57+
ldp.proxyFilter = regexp().start(ldp.proxy).toRegExp();
5858

5959
// Cache of usedURIs
6060
ldp.usedURIs = {};

lib/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ function uriBase(req) {
4242
return uriAbs(req) + (req.baseUrl || '');
4343
}
4444

45+
function pathBasename(fullpath) {
46+
var bname = '';
47+
if (fullpath) {
48+
bname = (fullpath.lastIndexOf('/') === fullpath.length - 1)?'':path.basename(fullpath);
49+
}
50+
return bname;
51+
}
52+
4553
function getResourceLink(filename, uri, base, suffix, otherSuffix) {
4654
var link = filenameToBaseUri(filename, uri, base);
4755
if (S(link).endsWith(suffix)) {
@@ -76,6 +84,7 @@ exports.uriToFilename = uriToFilename;
7684
exports.uriToRelativeFilename = uriToRelativeFilename;
7785
exports.filenameToBaseUri = filenameToBaseUri;
7886
exports.uriAbs = uriAbs;
87+
exports.pathBasename = pathBasename;
7988
exports.uriBase = uriBase;
8089
exports.getResourceLink = getResourceLink;
8190
exports.formatDateTime = formatDateTime;

test/http.js

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ describe('HTTP APIs', function() {
1515
};
1616
var getLink = function(res, rel) {
1717
var links = res.headers.link.split(',');
18-
for (var linkIndex in links) {
19-
var link = links[linkIndex];
18+
for (var i in links) {
19+
var link = links[i];
2020
var parsedLink = li.parse(link);
21-
for (var linkRel in parsedLink) {
22-
if (linkRel == rel) {
23-
return parsedLink[rel];
24-
}
21+
if (parsedLink[rel]) {
22+
return parsedLink[rel];
2523
}
2624
}
2725
return undefined;
@@ -31,30 +29,25 @@ describe('HTTP APIs', function() {
3129
var link = getLink(res, rel);
3230
if (link) {
3331
if (link !== value) {
34-
console.log("Not same value");
32+
console.log("Not same value:", value, '!=', link);
3533
}
3634
} else {
37-
console.log("Header does not exist");
35+
console.log(rel, "header does not exist:", link);
3836
}
3937
};
4038
return handler;
4139
};
4240

4341
var address = 'http://localhost:3457';
44-
var ldp = ldnode.createServer({
45-
root: __dirname + '/resources',
46-
xssProxy: '/proxy'
42+
var ldpServer = ldnode.createServer({
43+
root: __dirname + '/resources'
4744
});
48-
ldp.listen(3457);
45+
ldpServer.listen(3457);
4946

50-
var server = supertest(address);
47+
var suffixAcl = ".acl";
48+
var suffixMeta = ".meta";
5149

52-
describe('Proxy', function() {
53-
it('should return the website in ?uri', function(done) {
54-
server.get('/proxy?uri=http://google.com')
55-
.expect(200, done);
56-
});
57-
});
50+
var server = supertest(address);
5851

5952
describe('GET Root container', function() {
6053
it('should have Access-Control-Allow-Origin as the req.Origin', function(done) {
@@ -89,26 +82,40 @@ describe('HTTP APIs', function() {
8982
.end(done);
9083
});
9184

92-
it('should have Access-Control-Allow-Origin on OPTIONS', function(done) {
85+
it('should have Access-Control-Allow-Origin', function(done) {
9386
server.options('/sampleContainer/example1.ttl')
9487
.set('Origin', 'http://example.com')
9588
.expect('Access-Control-Allow-Origin', 'http://example.com')
9689
.end(done);
9790
});
9891

99-
it('should have set Link as resource in OPTIONS', function(done) {
92+
it('should have set acl and describedBy Links for resource', function(done) {
93+
server.options('/sampleContainer/example1.ttl')
94+
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
95+
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
96+
.end(done);
97+
});
98+
99+
it('should have set Link as resource', function(done) {
100100
server.options('/sampleContainer/example1.ttl')
101101
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
102102
.end(done);
103103
});
104104

105-
it('should have set Link as Container/BasicContainer in OPTIONS', function(done) {
105+
it('should have set Link as Container/BasicContainer', function(done) {
106106
server.options('/sampleContainer/')
107107
.set('Origin', 'http://example.com')
108108
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
109109
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
110110
.end(done);
111111
});
112+
113+
it('should have set acl and describedBy Links for container', function(done) {
114+
server.options('/sampleContainer/')
115+
.expect(hasHeader('acl', suffixAcl))
116+
.expect(hasHeader('describedBy', suffixMeta))
117+
.end(done);
118+
});
112119
});
113120

114121
describe('GET API', function() {
@@ -131,36 +138,48 @@ describe('HTTP APIs', function() {
131138
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
132139
.expect(200, done);
133140
});
141+
it('should have set acl and describedBy Links for resource', function(done) {
142+
server.get('/sampleContainer/example1.ttl')
143+
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
144+
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
145+
.end(done);
146+
});
134147

135148
it('should have set Link as Container/BasicContainer', function(done) {
136149
server.head('/sampleContainer/')
137150
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
138151
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
139152
.expect(200, done);
140153
});
141-
it('Should return 404 for non-existent resource', function(done) {
154+
it('should return 404 for non-existent resource', function(done) {
142155
server.get('/invalidfile.foo')
143156
.expect(404, done);
144157
});
145-
it('Should return basic container link for directories', function(done) {
158+
it('should return basic container link for directories', function(done) {
146159
server.get('/')
147160
.expect('Link', /http:\/\/www.w3.org\/ns\/ldp#BasicContainer/)
148161
.expect(200, done);
149162
});
150-
it('Should return resource link for files', function(done) {
163+
it('should return resource link for files', function(done) {
151164
server.get('/hello.html')
152165
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
153166
.expect(200, done);
154167
});
155-
it('Should have glob support', function(done) {
168+
it('should have glob support', function(done) {
156169
server.get('/sampleContainer/example*')
157170
.expect('content-type', /text\/turtle/)
158171
.expect(200, done);
159172
});
173+
174+
it('should have set acl and describedBy Links for container', function(done) {
175+
server.get('/sampleContainer/')
176+
.expect(hasHeader('acl', suffixAcl))
177+
.expect(hasHeader('describedBy', suffixMeta))
178+
.end(done);
179+
});
160180
});
161181

162182
describe('HEAD API', function() {
163-
164183
it('should have Access-Control-Allow-Origin as Origin', function(done) {
165184
server.head('/sampleContainer/example1.ttl')
166185
.set('Origin', 'http://example.com')
@@ -179,25 +198,26 @@ describe('HTTP APIs', function() {
179198
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Resource>; rel="type"/)
180199
.expect(200, done);
181200
});
201+
it('should have set acl and describedBy Links for resource', function(done) {
202+
server.get('/sampleContainer/example1.ttl')
203+
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
204+
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
205+
.end(done);
206+
});
182207

183208
it('should have set Link as Container/BasicContainer', function(done) {
184209
server.get('/sampleContainer/')
185210
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
186-
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
211+
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
187212
.expect(200, done);
188213
});
189214

190-
it('Should return meta header', function(done) {
191-
server.head('/')
192-
.expect(hasHeader('\'describedBy\'', address + '/' + '.meta'))
193-
.expect(200, done);
194-
});
195-
it('Should return acl header', function(done) {
196-
server.head('/')
197-
.expect(hasHeader('\'acl\'', address + '/' + '.acl'))
198-
.expect(200, done);
215+
it('should have set acl and describedBy Links for container', function(done) {
216+
server.get('/sampleContainer/')
217+
.expect(hasHeader('acl', suffixAcl))
218+
.expect(hasHeader('describedBy', suffixMeta))
219+
.end(done);
199220
});
200-
201221
});
202222

203223
describe('PUT API', function() {
@@ -214,6 +234,8 @@ describe('HTTP APIs', function() {
214234
server.put('/foo/bar/baz.ttl')
215235
.send(putRequestBody)
216236
.set('content-type', 'text/turtle')
237+
.expect(hasHeader('describedBy', 'baz.ttl' + suffixMeta))
238+
.expect(hasHeader('acl', 'baz.ttl' + suffixAcl))
217239
.expect(function() {
218240
fs.unlinkSync(__dirname + '/resources/foo/bar/baz.ttl');
219241
fs.rmdirSync(__dirname + '/resources/foo/bar/');
@@ -252,6 +274,8 @@ describe('HTTP APIs', function() {
252274
.set('content-type', 'text/turtle')
253275
.set('slug', 'post-resource-1')
254276
.expect('location', address + '/post-resource-1.ttl')
277+
.expect(hasHeader('describedBy', suffixMeta))
278+
.expect(hasHeader('acl', suffixAcl))
255279
.expect(201, done);
256280
});
257281
it('Should reject requests to existing resources', function(done) {

0 commit comments

Comments
 (0)