Skip to content

Commit

Permalink
Use random port when no port specified with -http (#316)
Browse files Browse the repository at this point in the history
* use random port when no port specified

* remove debugging print statement
  • Loading branch information
nolanmar511 authored Feb 7, 2018
1 parent a8f279b commit 7a927a9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
41 changes: 30 additions & 11 deletions internal/driver/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,10 @@ type webArgs struct {
}

func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, wantBrowser bool) error {
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
return fmt.Errorf("could not split http address: %v", err)
}
port, err := strconv.Atoi(portStr)
host, port, err := getHostAndPort(hostport)
if err != nil {
return fmt.Errorf("invalid port number: %v", err)
}
if host == "" {
host = "localhost"
return err
}

interactiveMode = true
ui := makeWebInterface(p, o)
for n, c := range pprofCommands {
Expand All @@ -112,7 +104,7 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
server = defaultWebServer
}
args := &plugin.HTTPServerArgs{
Hostport: net.JoinHostPort(host, portStr),
Hostport: net.JoinHostPort(host, strconv.Itoa(port)),
Host: host,
Port: port,
Handlers: map[string]http.Handler{
Expand All @@ -131,6 +123,33 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
return server(args)
}

func getHostAndPort(hostport string) (string, int, error) {
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
return "", 0, fmt.Errorf("could not split http address: %v", err)
}
if host == "" {
host = "localhost"
}
var port int
if portStr == "" {
ln, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
if err != nil {
return "", 0, fmt.Errorf("could not generate random port: %v", err)
}
port = ln.Addr().(*net.TCPAddr).Port
err = ln.Close()
if err != nil {
return "", 0, fmt.Errorf("could not generate random port: %v", err)
}
} else {
port, err = strconv.Atoi(portStr)
if err != nil {
return "", 0, fmt.Errorf("invalid port number: %v", err)
}
}
return host, port, nil
}
func defaultWebServer(args *plugin.HTTPServerArgs) error {
ln, err := net.Listen("tcp", args.Hostport)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/driver/webui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,41 @@ func makeFakeProfile() *profile.Profile {
}
}

func TestGetHostAndPort(t *testing.T) {
if runtime.GOOS == "nacl" {
t.Skip("test assumes tcp available")
}

type testCase struct {
hostport string
wantHost string
wantPort int
wantRandomPort bool
}

testCases := []testCase{
{":", "localhost", 0, true},
{":4681", "localhost", 4681, false},
{"localhost:4681", "localhost", 4681, false},
}
for _, tc := range testCases {
host, port, err := getHostAndPort(tc.hostport)
if err != nil {
t.Errorf("could not get host and port for %q: %v", tc.hostport, err)
}
if got, want := host, tc.wantHost; got != want {
t.Errorf("for %s, got host %s, want %s", tc.hostport, got, want)
continue
}
if !tc.wantRandomPort {
if got, want := port, tc.wantPort; got != want {
t.Errorf("for %s, got port %d, want %d", tc.hostport, got, want)
continue
}
}
}
}

func TestIsLocalHost(t *testing.T) {
for _, s := range []string{"localhost:10000", "[::1]:10000", "127.0.0.1:10000"} {
host, _, err := net.SplitHostPort(s)
Expand Down

0 comments on commit 7a927a9

Please sign in to comment.