HTTP plugin handler SSL detection fix
[KiwiIRC.git] / server / kiwi_modules / forcessl.js
CommitLineData
f97fe3d2
D
1/*
2 * forcessl Kiwi module
3 * Force clients to use an SSL port by redirecting them
4 */
5
6var kiwi = require('../kiwi.js');
7
8
3010bd6c 9exports.onhttp = function (ev, opts) {
dccec90a
D
10 var host, port = null, i;
11
f97fe3d2
D
12 // TODO: request.socket.pair seems to only be set in a SSL req, is this
13 // the best way to check for this?
3010bd6c 14 if (!opts.ssl) {
f97fe3d2 15 host = ev.request.headers.host;
dccec90a
D
16
17 // Remove the port if one is set
0d9401e0 18 if (host.search(/:/) > -1) {
f97fe3d2
D
19 host = host.substring(0, host.search(/:/));
20 }
dccec90a
D
21
22 for (i in kiwi.config.ports) {
23 if (kiwi.config.ports[i].secure) {
0d9401e0 24 port = kiwi.config.ports[i].number;
dccec90a
D
25 break;
26 }
27 }
28
29 // If we didn't find an SSL listener, don't redirect
30 if (port == null) {
31 return ev;
32 }
33
34 // No need to specify port 443 since it's the standard
35 if (port !== 443) {
36 host += ':' + port.toString();
f97fe3d2
D
37 }
38
39 console.log('https://' + host + ev.request.url);
40 ev.response.writeHead(302, {'Location': 'https://' + host + ev.request.url});
41 ev.response.end();
42
43 return null;
44 }
45
46 return ev;
47}