|
@@ -23,6 +23,7 @@ import (
|
23
|
23
|
gourl "net/url"
|
24
|
24
|
"os"
|
25
|
25
|
"os/exec"
|
|
26
|
+ "strconv"
|
26
|
27
|
"strings"
|
27
|
28
|
"time"
|
28
|
29
|
|
|
@@ -81,6 +82,18 @@ type webArgs struct {
|
81
|
82
|
}
|
82
|
83
|
|
83
|
84
|
func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) error {
|
|
85
|
+ host, portStr, err := net.SplitHostPort(hostport)
|
|
86
|
+ if err != nil {
|
|
87
|
+ return fmt.Errorf("could not split http address: %v", err)
|
|
88
|
+ }
|
|
89
|
+ port, err := strconv.Atoi(portStr)
|
|
90
|
+ if err != nil {
|
|
91
|
+ return fmt.Errorf("invalid port number: %v", err)
|
|
92
|
+ }
|
|
93
|
+ if host == "" {
|
|
94
|
+ host = "localhost"
|
|
95
|
+ }
|
|
96
|
+
|
84
|
97
|
interactiveMode = true
|
85
|
98
|
ui := makeWebInterface(p, o)
|
86
|
99
|
for n, c := range pprofCommands {
|
|
@@ -93,47 +106,51 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) e
|
93
|
106
|
ui.help["graph"] = "Display profile as a directed graph"
|
94
|
107
|
ui.help["reset"] = "Show the entire profile"
|
95
|
108
|
|
96
|
|
- ln, url, isLocal, err := newListenerAndURL(hostport)
|
97
|
|
- if err != nil {
|
98
|
|
- return err
|
|
109
|
+ server := o.HTTPServer
|
|
110
|
+ if server == nil {
|
|
111
|
+ server = defaultWebServer
|
99
|
112
|
}
|
100
|
|
-
|
101
|
|
- // authorization wrapper
|
102
|
|
- wrap := o.HTTPWrapper
|
103
|
|
- if wrap == nil {
|
104
|
|
- if isLocal {
|
105
|
|
- // Only allow requests from local host.
|
106
|
|
- wrap = checkLocalHost
|
107
|
|
- } else {
|
108
|
|
- wrap = func(h http.Handler) http.Handler { return h }
|
109
|
|
- }
|
|
113
|
+ args := &plugin.HTTPServerArgs{
|
|
114
|
+ Hostport: net.JoinHostPort(host, portStr),
|
|
115
|
+ Host: host,
|
|
116
|
+ Port: port,
|
|
117
|
+ Handlers: map[string]http.Handler{
|
|
118
|
+ "/": http.HandlerFunc(ui.dot),
|
|
119
|
+ "/top": http.HandlerFunc(ui.top),
|
|
120
|
+ "/disasm": http.HandlerFunc(ui.disasm),
|
|
121
|
+ "/source": http.HandlerFunc(ui.source),
|
|
122
|
+ "/peek": http.HandlerFunc(ui.peek),
|
|
123
|
+ },
|
110
|
124
|
}
|
111
|
125
|
|
112
|
|
- mux := http.NewServeMux()
|
113
|
|
- mux.Handle("/", wrap(http.HandlerFunc(ui.dot)))
|
114
|
|
- mux.Handle("/top", wrap(http.HandlerFunc(ui.top)))
|
115
|
|
- mux.Handle("/disasm", wrap(http.HandlerFunc(ui.disasm)))
|
116
|
|
- mux.Handle("/source", wrap(http.HandlerFunc(ui.source)))
|
117
|
|
- mux.Handle("/peek", wrap(http.HandlerFunc(ui.peek)))
|
118
|
|
-
|
119
|
|
- s := &http.Server{Handler: mux}
|
120
|
|
- go openBrowser(url, o)
|
121
|
|
- return s.Serve(ln)
|
|
126
|
+ go openBrowser("http://"+args.Hostport, o)
|
|
127
|
+ return server(args)
|
122
|
128
|
}
|
123
|
129
|
|
124
|
|
-func newListenerAndURL(hostport string) (ln net.Listener, url string, isLocal bool, err error) {
|
125
|
|
- host, _, err := net.SplitHostPort(hostport)
|
|
130
|
+func defaultWebServer(args *plugin.HTTPServerArgs) error {
|
|
131
|
+ ln, err := net.Listen("tcp", args.Hostport)
|
126
|
132
|
if err != nil {
|
127
|
|
- return nil, "", false, err
|
128
|
|
- }
|
129
|
|
- if host == "" {
|
130
|
|
- host = "localhost"
|
131
|
|
- }
|
132
|
|
- if ln, err = net.Listen("tcp", hostport); err != nil {
|
133
|
|
- return nil, "", false, err
|
|
133
|
+ return err
|
134
|
134
|
}
|
135
|
|
- url = fmt.Sprint("http://", net.JoinHostPort(host, fmt.Sprint(ln.Addr().(*net.TCPAddr).Port)))
|
136
|
|
- return ln, url, isLocalhost(host), nil
|
|
135
|
+ isLocal := isLocalhost(args.Host)
|
|
136
|
+ handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
137
|
+ if isLocal {
|
|
138
|
+ // Only allow local clients
|
|
139
|
+ host, _, err := net.SplitHostPort(req.RemoteAddr)
|
|
140
|
+ if err != nil || !isLocalhost(host) {
|
|
141
|
+ http.Error(w, "permission denied", http.StatusForbidden)
|
|
142
|
+ return
|
|
143
|
+ }
|
|
144
|
+ }
|
|
145
|
+ h := args.Handlers[req.URL.Path]
|
|
146
|
+ if h == nil {
|
|
147
|
+ // Fall back to default behavior
|
|
148
|
+ h = http.DefaultServeMux
|
|
149
|
+ }
|
|
150
|
+ h.ServeHTTP(w, req)
|
|
151
|
+ })
|
|
152
|
+ s := &http.Server{Handler: handler}
|
|
153
|
+ return s.Serve(ln)
|
137
|
154
|
}
|
138
|
155
|
|
139
|
156
|
func isLocalhost(host string) bool {
|
|
@@ -179,17 +196,6 @@ func openBrowser(url string, o *plugin.Options) {
|
179
|
196
|
o.UI.PrintErr(u.String())
|
180
|
197
|
}
|
181
|
198
|
|
182
|
|
-func checkLocalHost(h http.Handler) http.Handler {
|
183
|
|
- return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
184
|
|
- host, _, err := net.SplitHostPort(req.RemoteAddr)
|
185
|
|
- if err != nil || !isLocalhost(host) {
|
186
|
|
- http.Error(w, "permission denied", http.StatusForbidden)
|
187
|
|
- return
|
188
|
|
- }
|
189
|
|
- h.ServeHTTP(w, req)
|
190
|
|
- })
|
191
|
|
-}
|
192
|
|
-
|
193
|
199
|
func varsFromURL(u *gourl.URL) variables {
|
194
|
200
|
vars := pprofVariables.makeCopy()
|
195
|
201
|
vars["focus"].value = u.Query().Get("f")
|
|
@@ -241,12 +247,6 @@ func (ui *webInterface) render(w http.ResponseWriter, baseURL, tmpl string,
|
241
|
247
|
|
242
|
248
|
// dot generates a web page containing an svg diagram.
|
243
|
249
|
func (ui *webInterface) dot(w http.ResponseWriter, req *http.Request) {
|
244
|
|
- // Disable prefix matching behavior of net/http.
|
245
|
|
- if req.URL.Path != "/" {
|
246
|
|
- http.NotFound(w, req)
|
247
|
|
- return
|
248
|
|
- }
|
249
|
|
-
|
250
|
250
|
rpt, errList := ui.makeReport(w, req, []string{"svg"})
|
251
|
251
|
if rpt == nil {
|
252
|
252
|
return // error already reported
|