forcessl update
authorDarren <darren@darrenwhitlen.com>
Sat, 1 Oct 2011 19:47:33 +0000 (20:47 +0100)
committerDarren <darren@darrenwhitlen.com>
Sat, 1 Oct 2011 19:47:33 +0000 (20:47 +0100)
node/kiwi_modules/forcessl.js

index 1b4d46d1b3586f894631ae2becc5583e7ee06423..82e8cf223a2db869c3484cad550118cfabba13b3 100644 (file)
@@ -7,23 +7,33 @@ var kiwi = require('../kiwi.js');
 
 
 exports.onhttp = function (ev) {
+       var host, port = null, i;
+
        // TODO: request.socket.pair seems to only be set in a SSL req, is this
        // the best way to check for this?
        if (typeof ev.request.socket.pair === 'undefined') {
            host = ev.request.headers.host;
-           //port = 443;
-           
+
+           // Remove the port if one is set
            if (host.search(/:/)) {
-               //port = parseInt(host.substring(host.search(/:/) + 1), 10);
                host = host.substring(0, host.search(/:/));
            }
-           if (kiwi.config.ports[0].number != 443) {
-               for (i in kiwi.config.ports) {
-                       if (kiwi.config.ports[i].secure) {
-                               host += ':' + kiwi.config.ports[0].number.toString();
-                               break;
-                       }
-               }
+
+       for (i in kiwi.config.ports) {
+               if (kiwi.config.ports[i].secure) {
+                       port = kiwi.config.ports[0].number;
+                       break;
+               }
+       }
+
+           // If we didn't find an SSL listener, don't redirect
+           if (port == null) {
+               return ev;
+           }
+
+           // No need to specify port 443 since it's the standard
+           if (port !== 443) {
+               host += ':' + port.toString();
            }
 
            console.log('https://' + host + ev.request.url);