forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
221 lines (181 loc) · 5.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*jslint node: true*/
"use strict";
// Third-party dependencies
var express = require('express');
var app = express();
var getRawBody = require('raw-body');
var expressWs = require('express-ws');
var fs = require('fs');
var responseTime = require('response-time'); // Add X-Response-Time headers
var path = require('path');
var session = require('express-session');
var http = require('http');
var https = require('https');
var request = require('request');
var uuid = require('node-uuid');
var cors = require('cors');
// ldnode dependencies
var acl = require('./lib/acl');
var metadata = require('./lib/metadata');
var header = require('./lib/header');
var LDP = require('./lib/ldp');
var login = require('./lib/login');
var parse = require('./lib/parse');
var debug = require('./lib/logging');
// Request handlers
var getHandler = require('./lib/handlers/get.js');
var postHandler = require('./lib/handlers/post.js');
var putHandler = require('./lib/handlers/put.js');
var deleteHandler = require('./lib/handlers/delete.js');
var patchHandler = require('./lib/handlers/patch.js');
// Error page handler
var errorHandler = require('./lib/handlers/error.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();
// Setting options as local variable
app.locals.ldp = ldp;
// Session
app.use(session({
secret: ldp.secret || uuid.v1(),
saveUninitialized: false,
resave: false
}));
// Adding proxy
if (ldp.proxy) {
proxy(app, ldp.proxy);
}
// Setting up routes
app.use('/', routes());
// Setup Express app
if (ldp.live) {
ws(app);
}
debug.server("Router attached to " + ldp.mount);
return app;
}
function createServer(argv) {
var app = express();
var ldpApp = ldnode(argv);
var ldp = ldpApp.locals.ldp;
app.use(ldp.mount, ldpApp);
if (ldp && (ldp.webid || ldp.key || ldp.cert) ) {
debug.settings("SSL Private Key path: " + ldp.key);
debug.settings("SSL Certificate path: " + ldp.cert);
if (!ldp.cert && !ldp.key) {
throw new Error("Missing SSL cert and SSL key to enable WebID");
}
if (!ldp.key && ldp.cert) {
throw new Error("Missing path for SSL key");
}
if (!ldp.cert && ldp.key) {
throw new Error("Missing path for SSL cert");
}
var key;
try {
key = fs.readFileSync(ldp.key);
} catch(e) {
throw new Error("Can't find SSL key in " + ldp.key);
}
var cert;
try {
cert = fs.readFileSync(ldp.cert);
} catch(e) {
throw new Error("Can't find SSL cert in " + ldp.cert);
}
var credentials = {
key: key,
cert: cert,
requestCert: true
};
debug.settings("Private Key: " + credentials.key);
debug.settings("Certificate: " + credentials.cert);
return https.createServer(credentials, app);
}
return app;
}
function proxy (app, path) {
debug.settings('XSS Proxy listening to ' + path);
app.get(path, corsSettings, function (req, res) {
debug.settings('originalUrl: ' + req.originalUrl);
var uri = req.query.uri;
if (!uri) {
return res
.status(400)
.send("Proxy has no uri param ");
}
debug.settings('Proxy destination URI: ' + uri);
request.get(uri).pipe(res);
});
}
function routes () {
var router = express.Router('/');
// Add Link headers
router.use(header.linksHandler);
// Setting CORS
router.use(corsSettings);
router.use('/*', function(req, res, next) {
getRawBody(req,
{
length: req.headers['content-length'],
encoding: 'utf-8' // typer.parse(req.headers['content-type']).parameters.charset
},
function(err, string) {
if (err) {
return next(err);
}
req.text = string;
next();
});
});
router.use('/*', login.loginHandler);
//ACL handlers
router.get("/*", acl.allowReadHandler);
router.head("/*", acl.allowReadHandler);
router.post("/*", acl.allowAppendThenWriteHandler);
router.patch("/*", acl.allowAppendThenWriteHandler);
router.put("/*", acl.allowAppendThenWriteHandler);
router.delete("/*", acl.allowWriteHandler);
// Convert json-ld and nquads to turtle
router.use('/*', parse.parseHandler);
// Add response time
router.use(responseTime());
// HTTP methods handlers
router.get('/*', getHandler.handler);
router.head('/*', getHandler.headHandler);
router.put('/*', putHandler.handler);
router.delete('/*', deleteHandler.handler);
router.post('/*', postHandler.handler);
router.patch('/*', patchHandler.handler);
//Error handling
router.use(errorHandler.handler);
return router;
}
function ws (app) {
expressWs(app);
app.mountpath = ''; // needs to be set for addSocketRoute aka .ws()
// was options.pathFilter
app.ws('/', function(socket, res) {
debug.subscription("incoming on " + socket.path);
socket.on('message', function(msg) {
debug.subscription("message = " + msg);
// subscribeToChanges(socket, res);
});
});
}
ldnode.proxy = proxy;
ldnode.ws = ws;
ldnode.routes = routes;
ldnode.createServer = createServer;
module.exports = ldnode;