Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making proxy work #111

Merged
merged 4 commits into from
Aug 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Linked Data Platform server based on [rdflib.js](https://github.com/linkeddata/r
- [x] WebID+TLS Authentication
- [x] Mount as express' router
- [x] Command line tool
- [ ] Real-time live updates (using websokets)
- [x] Real-time live updates (using websokets)


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

Expand Down
5 changes: 5 additions & 0 deletions bin/ldnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ var argv = require('nomnom')
help: 'HTTP Session secret key (e.g. "your secret phrase")',
abbr: 's'
})
.option('proxy', {
full: 'proxy',
help: 'Use a proxy on example.tld/proxyPath',
abbr: 'P'
})
.option('noLive', {
full: 'no-live',
help: 'Disable live support through WebSockets',
Expand Down
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var putHandler = require('./lib/handlers/put.js');
var deleteHandler = require('./lib/handlers/delete.js');
var patchHandler = require('./lib/handlers/patch.js');

// Setting up cors
var corsSettings = cors({
methods: [
'OPTIONS', 'HEAD', 'GET',
'PATCH', 'POST', 'PUT', 'DELETE'
],
exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
credentials: true,
maxAge: 1728000,
origin: true
});

function ldnode (argv) {
var ldp = new LDP(argv);
var app = express();
Expand All @@ -46,14 +58,14 @@ function ldnode (argv) {
resave: false
}));

// Setting up routes
app.use('/', routes());

// Adding proxy
if (ldp.xssProxy) {
proxy(app, ldp.proxyFilter);
if (ldp.proxy) {
proxy(app, ldp.proxy);
}

// Setting up routes
app.use('/', routes());

// Setup Express app
if (ldp.live) {
ws(app);
Expand Down Expand Up @@ -117,7 +129,7 @@ function createServer(argv) {

function proxy (app, path) {
debug.settings('XSS Proxy listening to ' + path);
app.get(path, function (req, res) {
app.get(path, corsSettings, function (req, res) {
debug.settings('originalUrl: ' + req.originalUrl);
var uri = req.query.uri;
if (!uri) {
Expand All @@ -138,16 +150,7 @@ function routes () {
router.use(header.linksHandler);

// Setting CORS
router.use(cors({
methods: [
'OPTIONS', 'HEAD', 'GET',
'PATCH', 'POST', 'PUT', 'DELETE'
],
exposedHeaders: 'User, Location, Link, Vary, Last-Modified, Content-Length',
credentials: true,
maxAge: 1728000,
origin: true
}));
router.use(corsSettings);

router.use('/*', function(req, res, next) {
getRawBody(req,
Expand Down
3 changes: 1 addition & 2 deletions lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ function LDP(argv) {
}

ldp.pathFilter = regexp().start(ldp.mount).toRegExp();
ldp.xssProxy = argv.xssProxy;
ldp.proxyFilter = regexp().start(ldp.xssProxy).toRegExp();
ldp.proxy = argv.proxy;

// Cache of usedURIs
ldp.usedURIs = {};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"devDependencies": {
"chai": "^3.0.0",
"mocha": "^2.2.5",
"nock": "^2.10.0",
"supertest": "^1.0.1"
},
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('HTTP APIs', function() {

var address = 'http://localhost:3457';
var ldpServer = ldnode.createServer({
root: __dirname + '/resources',
root: __dirname + '/resources'
});
ldpServer.listen(3457);

Expand Down
58 changes: 58 additions & 0 deletions test/params.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var assert = require('chai').assert;
var supertest = require('supertest');
var nock = require('nock');
var async = require('async');
// Helper functions for the FS
var rm = require('./test-utils').rm;
var write = require('./test-utils').write;
Expand All @@ -10,6 +12,62 @@ var ldnode = require('../index');

describe('LDNODE params', function () {

describe('proxy', function() {

var ldp = ldnode({
root: __dirname + '/resources',
proxy: '/proxy'
});
var server = supertest(ldp);

it('should return the website in /proxy?uri', function(done) {
nock('https://amazingwebsite.tld').get('/').reply(200);
server.get('/proxy?uri=https://amazingwebsite.tld/')
.expect(200, done);
});

it('should also work on /proxy/ ?uri', function(done) {
nock('https://amazingwebsite.tld').get('/').reply(200);
server.get('/proxy/?uri=https://amazingwebsite.tld/')
.expect(200, done);
});

it('should return the same HTTP status code as the uri', function(done) {
async.parallel([
// 500
function(next) {
nock('https://amazingwebsite.tld').get('/404').reply(404);
server.get('/proxy/?uri=https://amazingwebsite.tld/404')
.expect(404, next);
},
function(next) {
nock('https://amazingwebsite.tld').get('/401').reply(401);
server.get('/proxy/?uri=https://amazingwebsite.tld/401')
.expect(401, next);
},
function(next) {
nock('https://amazingwebsite.tld').get('/500').reply(500);
server.get('/proxy/?uri=https://amazingwebsite.tld/500')
.expect(500, next);
},
function(next) {
nock('https://amazingwebsite.tld').get('/').reply(200);
server.get('/proxy/?uri=https://amazingwebsite.tld/')
.expect(200, next);
}
], done);
});

it('should work with cors', function(done) {
nock('https://amazingwebsite.tld').get('/').reply(200);
server.get('/proxy/?uri=https://amazingwebsite.tld/')
.set('Origin', 'http://example.com')
.expect('Access-Control-Allow-Origin', 'http://example.com')
.expect(200, done);
});
});


describe('suffixMeta', function () {
describe('not passed', function() {
it('should fallback on .meta', function() {
Expand Down