-
Notifications
You must be signed in to change notification settings - Fork 4
/
snickers-spdy.js
108 lines (98 loc) · 3.27 KB
/
snickers-spdy.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
var backends = require('./backends'),
dispatcher = require('./dispatcher'),
listener = require('./listener'),
configReader = require('./config-reader'),
statics = require('./statics'),
stats = require('./stats'),
alarm = require('./alarm');
function handlerWebBackend(host, config, req, res) {
console.log('handlerWebBackend');
backends.ensureStarted(host, config, function(err, data) {
if (err) {
res.writeHead(500);
res.end('Error starting ' + config.application + ' for ' + host + ' - ' + JSON.stringify(err));
} else {
req.headers['X-Forwarded-Proto'] = 'https';
if (!config.port) {
config.port = 80;
}
dispatcher.proxyTo(req, res, data.ipaddr, config.port);
}
});
}
function handlerWebStatic(host, config, req, res) {
statics.serveStatic('/data/domains/' + host + (config.folder ? '/' + config.folder : ''), req, res);
}
function handlerWeb(req, res) {
var host, config;
host = req.headers.host;
if (typeof host !== 'string') {
console.log('no host header, trying :authority header', req.headers);
host = req.headers[':authority'];
}
if (typeof host === 'string') {
if (host === host.toLowerCase()) {
config = configReader.getConfig(host);
alarm.debug('got config', host, config);
if (config.type === 'backend') {
handlerWebBackend(host, config, req, res);
} else if (config.type === 'static') {
handlerWebStatic(host, config, req, res);
} else if (config.type === 'redirect') {
res.writeHead(301, {
Location: 'https://' + config.redirectHost + req.url
});
res.end('Location: https://' + config.redirectHost + req.url);
} else {
res.writeHead(404);
res.end('Snickers says: That site is not configured on this server.');
}
stats.inc(host);
} else {
res.writeHead(301, {
Location: 'https://' + host.toLowerCase() + req.url
});
req.end('Please specify the host header in lower case');
}
} else {
res.writeHead(406);
res.end('Cannot serve http request without host header '+ JSON.stringify(req.headers));
}
}
function handlerWsBackend(host, config, req, socket, head) {
backends.ensureStarted(host, configReader.getConfig(host), function(err, ipaddr) {
if (err) {
alarm.raise('Error starting site, closing socket', host);
socket.close();
} else {
req.headers['X-Forwarded-Proto'] = 'https';
dispatcher.proxyWsTo(req, socket, head, ipaddr, config.port);
}
});
}
function handlerWs (req, socket, head) {
var host, config;
host = req.headers.host;
if (typeof host === 'string') {
host = host.toLowerCase();
config = configReader.getConfig(host);
if (config.type === 'backend') {
handlerWsBackend(host, config, req, socket, head);
} else {
socket.close();
}
stats.inc(host);
} else {
res.writeHead(406);
res.end('Cannot serve upgrade request without host header');
}
}
function whitelist(servername) {
return (configReader.getConfig(servername).type !== undefined);
}
module.exports.start = function(callback) {
backends.init(function(err) {
console.log('backends initialized', err);
});
listener.startSpdy(handlerWeb, handlerWs, whitelist, callback);
}