|
@@ -83,18 +83,10 @@ type webArgs struct {
|
83
|
83
|
}
|
84
|
84
|
|
85
|
85
|
func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, wantBrowser bool) error {
|
86
|
|
- host, portStr, err := net.SplitHostPort(hostport)
|
87
|
|
- if err != nil {
|
88
|
|
- return fmt.Errorf("could not split http address: %v", err)
|
89
|
|
- }
|
90
|
|
- port, err := strconv.Atoi(portStr)
|
|
86
|
+ host, port, err := getHostAndPort(hostport)
|
91
|
87
|
if err != nil {
|
92
|
|
- return fmt.Errorf("invalid port number: %v", err)
|
93
|
|
- }
|
94
|
|
- if host == "" {
|
95
|
|
- host = "localhost"
|
|
88
|
+ return err
|
96
|
89
|
}
|
97
|
|
-
|
98
|
90
|
interactiveMode = true
|
99
|
91
|
ui := makeWebInterface(p, o)
|
100
|
92
|
for n, c := range pprofCommands {
|
|
@@ -112,7 +104,7 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
|
112
|
104
|
server = defaultWebServer
|
113
|
105
|
}
|
114
|
106
|
args := &plugin.HTTPServerArgs{
|
115
|
|
- Hostport: net.JoinHostPort(host, portStr),
|
|
107
|
+ Hostport: net.JoinHostPort(host, strconv.Itoa(port)),
|
116
|
108
|
Host: host,
|
117
|
109
|
Port: port,
|
118
|
110
|
Handlers: map[string]http.Handler{
|
|
@@ -131,6 +123,33 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, w
|
131
|
123
|
return server(args)
|
132
|
124
|
}
|
133
|
125
|
|
|
126
|
+func getHostAndPort(hostport string) (string, int, error) {
|
|
127
|
+ host, portStr, err := net.SplitHostPort(hostport)
|
|
128
|
+ if err != nil {
|
|
129
|
+ return "", 0, fmt.Errorf("could not split http address: %v", err)
|
|
130
|
+ }
|
|
131
|
+ if host == "" {
|
|
132
|
+ host = "localhost"
|
|
133
|
+ }
|
|
134
|
+ var port int
|
|
135
|
+ if portStr == "" {
|
|
136
|
+ ln, err := net.Listen("tcp", net.JoinHostPort(host, "0"))
|
|
137
|
+ if err != nil {
|
|
138
|
+ return "", 0, fmt.Errorf("could not generate random port: %v", err)
|
|
139
|
+ }
|
|
140
|
+ port = ln.Addr().(*net.TCPAddr).Port
|
|
141
|
+ err = ln.Close()
|
|
142
|
+ if err != nil {
|
|
143
|
+ return "", 0, fmt.Errorf("could not generate random port: %v", err)
|
|
144
|
+ }
|
|
145
|
+ } else {
|
|
146
|
+ port, err = strconv.Atoi(portStr)
|
|
147
|
+ if err != nil {
|
|
148
|
+ return "", 0, fmt.Errorf("invalid port number: %v", err)
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+ return host, port, nil
|
|
152
|
+}
|
134
|
153
|
func defaultWebServer(args *plugin.HTTPServerArgs) error {
|
135
|
154
|
ln, err := net.Listen("tcp", args.Hostport)
|
136
|
155
|
if err != nil {
|