Verified Commit ceccfab1 authored by Aral Balkan's avatar Aral Balkan
Browse files

Remove built-in http-server; replace with @ind.ie/https-server

parent 09430d3b
var getPort = require('get-port') var getPort = require('get-port')
var isElectronProject = require('./is-electron-project') var isElectronProject = require('./is-electron-project')
var http = require('./http-server') var httpsServer = require('@ind.ie/https-server')
var bankai = require('../http') var bankai = require('../http')
var createTui = require('./ui') var createTui = require('./ui')
var createLogUi = require('./ui-basic') var createLogUi = require('./ui-basic')
...@@ -19,13 +19,15 @@ function start (entry, opts) { ...@@ -19,13 +19,15 @@ function start (entry, opts) {
if (err) throw err if (err) throw err
opts.electron = bool opts.electron = bool
var server = http.createServer(function (req, res) { var server = httpsServer.createServer({}, function (req, res) {
if (req.type === 'OPTIONS') return cors(req, res) if (req.type === 'OPTIONS') return cors(req, res)
handler(req, res, function () { handler(req, res, function () {
res.statusCode = 404 res.statusCode = 404
return res.end('No route found for ' + req.url) return res.end('No route found for ' + req.url)
}) })
}) })
server.keepAliveTimeout = 0
server.timeout = 0
render() render()
......
var http = require('http')
var pump = require('pump')
var net = require('net')
var getPort = require('get-port')
var httpsServer = require('@ind.ie/https-server')
exports.createServer = createDevServer
function createDevServer (connectionHandler) {
var httpPort, httpsPort
var createSecureServer
try {
createSecureServer = httpsServer.createSecureServer
} catch (e) {
createSecureServer = httpsServer.createServer
}
return {
listen: listen
}
function listen (port, onlisten) {
net.createServer(tcpConnection).listen(port, onNetListen)
function onNetListen () {
getPort({port: 443}).then(function (port) {
httpPort = port
var httpServer = http.createServer(httpConnection)
.listen(port, onHttpListen)
httpServer.keepAliveTimeout = 0
httpServer.timeout = 0
})
.catch(function (err) {
throw err
})
}
function onHttpListen () {
getPort({port: 4443}).then(function (port) {
httpsPort = port
var serverOpts = { allowHTTP1: true }
var http2Server = createSecureServer(serverOpts, connectionHandler)
http2Server.keepAliveTimeout = 0
http2Server.timeout = 0
http2Server.listen(httpsPort, function () {
if (onlisten) onlisten()
})
})
.catch(function (err) {
throw err
})
}
}
function tcpConnection (conn) {
conn.once('data', function (buf) {
// A TLS handshake record starts with byte 22.
var address = buf[0] === 22 ? httpsPort : httpPort
var proxy = net.createConnection(address, function () {
proxy.write(buf)
pump(conn, proxy, conn, function (err) {
if (err) return false // TODO: log error to the logger part
})
})
})
}
function httpConnection (req, res) {
var host = req.headers['host']
var location = 'https://' + host + req.url
var agent = req.headers['user-agent']
// We don't want to force an HTTPS connection if we are already
// encrypted or we are being forwarded through a proxy that may be
// taking care of it.
var encrypted = req.headers['x-forwarded-proto'] || req.connection.encrypted
// We want to force HTTPS connections, but using curl(1) or wget(1) from
// the command line can be convenient to quickly check output.
if (/^(curl|wget)/i.test(agent) || encrypted) {
return connectionHandler(req, res)
} else {
res.writeHead(301, { location: location })
res.end(`Redirecting to ${location}`)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment