From eb32ef2ea164cebb572f9a6e2ae4f7f8940f00ef Mon Sep 17 00:00:00 2001 From: HouzuoGuo Date: Sun, 26 Nov 2017 17:13:17 +0100 Subject: [PATCH] teach http server to listen on port 80 when launched as "insecure http server" without tls --- daemon/httpd/httpd.go | 12 ++++++------ daemon/httpd/httpd_test.go | 2 +- launcher/config_test.go | 2 +- main.go | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/daemon/httpd/httpd.go b/daemon/httpd/httpd.go index 3d8d7e7d..9df9ef27 100644 --- a/daemon/httpd/httpd.go +++ b/daemon/httpd/httpd.go @@ -185,20 +185,20 @@ func (daemon *Daemon) Initialise() error { StartAndBlockNoTLS starts HTTP daemon and serve unencrypted connections. Blocks caller until StopNoTLS function is called. You may call this function only after having called Initialise()! */ -func (daemon *Daemon) StartAndBlockNoTLS() error { +func (daemon *Daemon) StartAndBlockNoTLS(fallbackPort int) error { /* In order to determine the listener's port: - On ElasticBeanstalk (and several other scenarios), listen on port number specified in environment PORT value. - - If TLS listener has not yet started, listen on the port specified by user configuration (prone to race condition). - - In this way, caller of the function can determine whether to listen for TLS or ordinary traffic, one or the other. + - If TLS configuration does not exist, listen on the port specified by user configuration. + - If TLS configuration exists, then listen on the fallback port. + Not very elegant, but it should help to launch HTTP daemon in TLS only, TLS + HTTP, and HTTP only scenarios. */ var port int if envPort := strings.TrimSpace(os.Getenv("PORT")); envPort == "" { - if daemon.serverWithTLS == nil { + if daemon.TLSCertPath == "" { port = daemon.Port } else { - return fmt.Errorf("httpd.StartAndBlockNoTLS: was about to listen on port %d but TLS listener is already using it", daemon.Port) + port = fallbackPort } } else { iPort, err := strconv.Atoi(envPort) diff --git a/daemon/httpd/httpd_test.go b/daemon/httpd/httpd_test.go index 01d309b8..d96966f8 100644 --- a/daemon/httpd/httpd_test.go +++ b/daemon/httpd/httpd_test.go @@ -89,7 +89,7 @@ func TestHTTPD_StartAndBlock(t *testing.T) { // HTTP daemon is expected to start in two seconds var stoppedNormally bool go func() { - if err := daemon.StartAndBlockNoTLS(); err != nil { + if err := daemon.StartAndBlockNoTLS(0); err != nil { t.Fatal(err) } stoppedNormally = true diff --git a/launcher/config_test.go b/launcher/config_test.go index 8025f6e9..8f88aa6a 100644 --- a/launcher/config_test.go +++ b/launcher/config_test.go @@ -242,7 +242,7 @@ func TestConfig(t *testing.T) { httpDaemon := config.GetHTTPD() // HTTP daemon is expected to start in two seconds go func() { - if err := httpDaemon.StartAndBlockNoTLS(); err != nil { + if err := httpDaemon.StartAndBlockNoTLS(0); err != nil { t.Fatal(err) } }() diff --git a/main.go b/main.go index f48c5b07..6854970f 100644 --- a/main.go +++ b/main.go @@ -265,7 +265,7 @@ func main() { }() case launcher.InsecureHTTPDName: go func() { - daemonErrs <- config.GetHTTPD().StartAndBlockNoTLS() + daemonErrs <- config.GetHTTPD().StartAndBlockNoTLS(80) }() case launcher.MaintenanceName: go func() {