The net/http package's Server type has ListenAndServe and
ListenAndServeTLS methods that create a TCP listener and pass it into
Serve, which is an infinite loop. I wanted to be able to inspect the
created listener before it gets passed to Serve; specifically, I wanted
to find out the listener's address. The attached patch factors out
Listen and ListenTLS methods so you can inspect the listener before
calling Serve.
A separate Serve method already exists and allows you to pass in your
own listener. For plain HTTP it's no big deal to create your own
listener because ListenAndServe essentially just calls net.Listen.
https://code.google.com/p/go/source/browse/src/pkg/net/http/server.go?r=1849f83423ca#1684
However the HTTPS case is more complicated. It creates a slice for
certificates, load the certificate and key files, and sets up the
tls.Config NextProtos. The TLS setup exists only in ListenAndServeTLS;
there's no way to get the http package to set up a listener without it
also calling Serve. I can easily copy and paste the TLS setup (which is
what I am doing as a workaround), but that means my code will not notice
future TLS setup changes that happen in ListenAndServeTLS.
https://code.google.com/p/go/source/browse/src/pkg/net/http/server.go?r=1849f83423ca#1822
In summary, these methods already exist:
ListenAndServe
ListenAndServeTLS
Serve
The patch adds these two additional methods:
Listen
ListenTLS
My use case is running an HTTPS-based pluggable transport for Tor. Tor
tells the pluggable transport subprocess what address to listen on, and
the subprocess is supposed to report back with the actual address of the
listener. Tor's default behavior is to send "
0.0.0.0:0" as the listening
address, meaning the subprocess should listen on an ephemeral port.
There's no way to find out what port the ":0" will become without
actually opening the listener, and currently the http package doesn't
provide a way to open a listener and do something with it before it is
passed to Serve.
David Fifield