-
Notifications
You must be signed in to change notification settings - Fork 4
/
snickers-http.js
38 lines (36 loc) · 994 Bytes
/
snickers-http.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
var http = require('http');
module.exports.start = function(configReader, stats, callback) {
var server = http.createServer(function(req, res) {
var host, config, redirectHost;
host = req.headers.host;
if (typeof host === 'string') {
host = host.toLowerCase();
config = configReader.getConfig(host);
redirectHost = host;
if (config.redirectHost) {
redirectHost = config.redirectHost;
}
res.writeHead(301, {
Location: 'https://' + redirectHost + req.url
});
res.end('Location: https://' + redirectHost + req.url);
stats.inc(host);
} else {
res.writeHead(406);
res.end('Cannot serve http request without host header');
}
});
var listening = false;
server.on('error', function(err) {
if (listening) {
alarm.raise(err);
} else {
callback(err);
}
});
server.on('listening', function() {
listening = true;
callback(null);
});
server.listen(80);
};