Commit | Line | Data |
---|---|---|
f97fe3d2 D |
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 | ||
3010bd6c | 9 | exports.onhttp = function (ev, opts) { |
dccec90a D |
10 | var host, port = null, i; |
11 | ||
480d2fea | 12 | if (!ev.ssl) { |
f97fe3d2 | 13 | host = ev.request.headers.host; |
dccec90a D |
14 | |
15 | // Remove the port if one is set | |
0d9401e0 | 16 | if (host.search(/:/) > -1) { |
f97fe3d2 D |
17 | host = host.substring(0, host.search(/:/)); |
18 | } | |
dccec90a | 19 | |
480d2fea D |
20 | for (i in kiwi.config.servers) { |
21 | if (kiwi.config.servers[i].secure) { | |
22 | port = kiwi.config.servers[i].port; | |
dccec90a D |
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(); | |
f97fe3d2 | 35 | } |
480d2fea | 36 | |
f97fe3d2 D |
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 | } |