Merge branch 'API_Refactor' of https://github.com/M2Ys4U/KiwiIRC
[KiwiIRC.git] / server / kiwi_modules / forcessl.js
1 /*
2 * forcessl Kiwi module
3 * Force clients to use an SSL port by redirecting them
4 */
5
6 var kiwi = require('../kiwi.js');
7
8
9 exports.onhttp = function (ev, opts) {
10 var host, port = null, i;
11
12 if (!ev.ssl) {
13 host = ev.request.headers.host;
14
15 // Remove the port if one is set
16 if (host.search(/:/) > -1) {
17 host = host.substring(0, host.search(/:/));
18 }
19
20 for (i in kiwi.config.servers) {
21 if (kiwi.config.servers[i].secure) {
22 port = kiwi.config.servers[i].port;
23 break;
24 }
25 }
26
27 // If we didn't find an SSL listener, don't redirect
28 if (port == null) {
29 return ev;
30 }
31
32 // No need to specify port 443 since it's the standard
33 if (port !== 443) {
34 host += ':' + port.toString();
35 }
36
37 ev.response.writeHead(302, {'Location': 'https://' + host + ev.request.url});
38 ev.response.end();
39
40 return null;
41 }
42
43 return ev;
44 }