MCPcopy Create free account
hub / github.com/google/pprof / defaultWebServer

Function defaultWebServer

internal/driver/webui.go:185–217  ·  view source on GitHub ↗
(args *plugin.HTTPServerArgs)

Source from the content-addressed store, hash-verified

183 return host, port, nil
184}
185func defaultWebServer(args *plugin.HTTPServerArgs) error {
186 ln, err := net.Listen("tcp", args.Hostport)
187 if err != nil {
188 return err
189 }
190 isLocal := isLocalhost(args.Host)
191 handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
192 if isLocal {
193 // Only allow local clients
194 host, _, err := net.SplitHostPort(req.RemoteAddr)
195 if err != nil || !isLocalhost(host) {
196 http.Error(w, "permission denied", http.StatusForbidden)
197 return
198 }
199 }
200 h := args.Handlers[req.URL.Path]
201 if h == nil {
202 // Fall back to default behavior
203 h = http.DefaultServeMux
204 }
205 h.ServeHTTP(w, req)
206 })
207
208 // We serve the ui at /ui/ and redirect there from the root. This is done
209 // to surface any problems with serving the ui at a non-root early. See:
210 //
211 // https://github.com/google/pprof/pull/348
212 mux := http.NewServeMux()
213 mux.Handle("/ui/", http.StripPrefix("/ui", handler))
214 mux.Handle("/", redirectWithQuery("/ui", http.StatusTemporaryRedirect))
215 s := &http.Server{Handler: mux}
216 return s.Serve(ln)
217}
218
219// redirectWithQuery responds with a given redirect code, preserving query
220// parameters in the redirect URL. It does not convert relative paths to

Callers

nothing calls this directly

Calls 2

isLocalhostFunction · 0.85
redirectWithQueryFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…