Skip to content

Commit 71eb537

Browse files
author
Andrei
committed
Merge pull request #111 from nicola/proxy
Making proxy work
2 parents f6217f3 + 57783d4 commit 71eb537

7 files changed

Lines changed: 88 additions & 21 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ var argv = require('nomnom')
5454
help: 'HTTP Session secret key (e.g. "your secret phrase")',
5555
abbr: 's'
5656
})
57+
.option('proxy', {
58+
full: 'proxy',
59+
help: 'Use a proxy on example.tld/proxyPath',
60+
abbr: 'P'
61+
})
5762
.option('noLive', {
5863
full: 'no-live',
5964
help: 'Disable live support through WebSockets',

‎index.js‎

Lines changed: 19 additions & 16 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();
@@ -46,14 +58,14 @@ function ldnode (argv) {
4658
resave: false
4759
}));
4860

49-
// Setting up routes
50-
app.use('/', routes());
51-
5261
// Adding proxy
53-
if (ldp.xssProxy) {
54-
proxy(app, ldp.proxyFilter);
62+
if (ldp.proxy) {
63+
proxy(app, ldp.proxy);
5564
}
5665

66+
// Setting up routes
67+
app.use('/', routes());
68+
5769
// Setup Express app
5870
if (ldp.live) {
5971
ws(app);
@@ -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/ldp.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ 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;
5857

5958
// Cache of usedURIs
6059
ldp.usedURIs = {};

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"devDependencies": {
5454
"chai": "^3.0.0",
5555
"mocha": "^2.2.5",
56+
"nock": "^2.10.0",
5657
"supertest": "^1.0.1"
5758
},
5859
"main": "index.js",

‎test/http.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('HTTP APIs', function() {
4040

4141
var address = 'http://localhost:3457';
4242
var ldpServer = ldnode.createServer({
43-
root: __dirname + '/resources',
43+
root: __dirname + '/resources'
4444
});
4545
ldpServer.listen(3457);
4646

‎test/params.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var assert = require('chai').assert;
22
var supertest = require('supertest');
3+
var nock = require('nock');
4+
var async = require('async');
35
// Helper functions for the FS
46
var rm = require('./test-utils').rm;
57
var write = require('./test-utils').write;
@@ -10,6 +12,62 @@ var ldnode = require('../index');
1012

1113
describe('LDNODE params', function () {
1214

15+
describe('proxy', function() {
16+
17+
var ldp = ldnode({
18+
root: __dirname + '/resources',
19+
proxy: '/proxy'
20+
});
21+
var server = supertest(ldp);
22+
23+
it('should return the website in /proxy?uri', function(done) {
24+
nock('https://amazingwebsite.tld').get('/').reply(200);
25+
server.get('/proxy?uri=https://amazingwebsite.tld/')
26+
.expect(200, done);
27+
});
28+
29+
it('should also work on /proxy/ ?uri', function(done) {
30+
nock('https://amazingwebsite.tld').get('/').reply(200);
31+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
32+
.expect(200, done);
33+
});
34+
35+
it('should return the same HTTP status code as the uri', function(done) {
36+
async.parallel([
37+
// 500
38+
function(next) {
39+
nock('https://amazingwebsite.tld').get('/404').reply(404);
40+
server.get('/proxy/?uri=https://amazingwebsite.tld/404')
41+
.expect(404, next);
42+
},
43+
function(next) {
44+
nock('https://amazingwebsite.tld').get('/401').reply(401);
45+
server.get('/proxy/?uri=https://amazingwebsite.tld/401')
46+
.expect(401, next);
47+
},
48+
function(next) {
49+
nock('https://amazingwebsite.tld').get('/500').reply(500);
50+
server.get('/proxy/?uri=https://amazingwebsite.tld/500')
51+
.expect(500, next);
52+
},
53+
function(next) {
54+
nock('https://amazingwebsite.tld').get('/').reply(200);
55+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
56+
.expect(200, next);
57+
}
58+
], done);
59+
});
60+
61+
it('should work with cors', function(done) {
62+
nock('https://amazingwebsite.tld').get('/').reply(200);
63+
server.get('/proxy/?uri=https://amazingwebsite.tld/')
64+
.set('Origin', 'http://example.com')
65+
.expect('Access-Control-Allow-Origin', 'http://example.com')
66+
.expect(200, done);
67+
});
68+
});
69+
70+
1371
describe('suffixMeta', function () {
1472
describe('not passed', function() {
1573
it('should fallback on .meta', function() {

0 commit comments

Comments
 (0)