From f3fb34236b1cd2d8cf04b42572b711af66d54e7b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 6 Oct 2016 17:09:21 -0600 Subject: [PATCH] use httpolyglot --- package.json | 1 + serve.js | 36 ++++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index c94d28a..d14126f 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "homepage": "https://github.com/Daplie/serve-https#readme", "dependencies": { "finalhandler": "^0.4.0", + "httpolyglot": "^0.1.1", "ipify": "^1.1.0", "livereload": "^0.4.0", "localhost.daplie.com-certificates": "^1.2.0", diff --git a/serve.js b/serve.js index 0cfe08c..9d0e256 100755 --- a/serve.js +++ b/serve.js @@ -2,10 +2,12 @@ 'use strict'; var PromiseA = global.Promise; // require('bluebird'); -var https = require('https'); +var https = require('httpolyglot'); var http = require('http'); var fs = require('fs'); var path = require('path'); +var httpPort = 80; +var httpsPort = 443; var portFallback = 8443; var insecurePortFallback = 4080; @@ -76,7 +78,7 @@ function createServer(port, pubdir, content, opts) { server2.watch(pubdir); - if ('false' !== opts.insecurePort) { + if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) { return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve); } else { resolve(); @@ -90,6 +92,17 @@ function createServer(port, pubdir, content, opts) { } server.on('request', function (req, res) { + if (!req.socket.encrypted) { + res.statusCode = 301; + res.setHeader( + 'Location' + , 'https://' + (req.headers.host || 'localhost') + + (httpsPort === opts.port ? '' : ':' + opts.port) + ); + res.end(); + return; + } + if ('function' === typeof app) { app(req, res); return; @@ -110,7 +123,7 @@ function run() { var defaultServername = 'localhost.daplie.com'; var minimist = require('minimist'); var argv = minimist(process.argv.slice(2)); - var port = argv.p || argv.port || argv._[0] || 443; + var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort; var livereload = argv.livereload; var pubdir = path.resolve(argv.d || argv._[1] || process.cwd()); var content = argv.c; @@ -201,7 +214,10 @@ function run() { if (argv.i || argv['insecure-port']) { opts.manualInsecurePort = true; } - opts.insecurePort = argv.i || argv['insecure-port'] || 80; + opts.insecurePort = parseInt(argv.i || argv['insecure-port'], 10) + || argv.i || argv['insecure-port'] + || httpPort + ; opts.livereload = livereload; if (argv['express-app']) { @@ -218,7 +234,7 @@ function run() { msg = 'Serving ' + pubdir + ' at '; httpsUrl = 'https://' + opts.servername; p = opts.port; - if (443 !== p) { + if (httpsPort !== p) { httpsUrl += ':' + p; } console.info(''); @@ -228,7 +244,7 @@ function run() { // Insecure Port p = ''; - if (80 !== p) { + if (httpPort !== p) { p = ':' + opts.insecurePort; } msg = '\thttp://' + opts.servername + p + ' (redirecting to https)'; @@ -264,14 +280,14 @@ function run() { opts.matchingIps.forEach(function (ip) { if ('IPv4' === ip.family) { httpsUrl = 'https://' + ip.address; - if (443 !== opts.port) { + if (httpsPort !== opts.port) { httpsUrl += ':' + opts.port; } console.info('\t' + httpsUrl); } else { httpsUrl = 'https://[' + ip.address + ']'; - if (443 !== opts.port) { + if (httpsPort !== opts.port) { httpsUrl += ':' + opts.port; } console.info('\t' + httpsUrl); @@ -287,13 +303,13 @@ function run() { console.info(iname + ':'); httpsUrl = 'https://' + iface.ipv4[0].address; - if (443 !== opts.port) { + if (httpsPort !== opts.port) { httpsUrl += ':' + opts.port; } console.info('\t' + httpsUrl); httpsUrl = 'https://[' + iface.ipv6[0].address + ']'; - if (443 !== opts.port) { + if (httpsPort !== opts.port) { httpsUrl += ':' + opts.port; } if (iface.ipv6.length) {