mirror of
https://github.com/golang/go.git
synced 2025-05-18 13:54:40 +00:00
net/http/pprof: avoid panic with user-defined "GET /" route
With the new routing style in go 1.22, declaring http.Handle("GET /", h) generates a conflict with route "/debug/pprof/" and the others declared in the net/http/pprof package. You get an error such as: panic: pattern "GET /" (registered at .../pprof.go:94): GET / matches fewer methods than /debug/pprof/, but has a more general path pattern This patch prevents that error. Adding GET is correct because no other method makes sense with the /debug/pprof routes. However, a tool using any method other than GET will break. We preserve the traditional behaviour when GODEBUG=httpmuxgo121=1 is specified. Updates #65723 Change-Id: I49c21f5f3e802ad7538062d824354b2e4d8a800e GitHub-Last-Rev: 35e4012663f454fee8f00a321c43592ce4754feb GitHub-Pull-Request: golang/go#65791 Reviewed-on: https://go-review.googlesource.com/c/go/+/565176 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
parent
0584574797
commit
9fcffc5359
@ -8,6 +8,7 @@
|
|||||||
// The package is typically only imported for the side effect of
|
// The package is typically only imported for the side effect of
|
||||||
// registering its HTTP handlers.
|
// registering its HTTP handlers.
|
||||||
// The handled paths all begin with /debug/pprof/.
|
// The handled paths all begin with /debug/pprof/.
|
||||||
|
// As of Go 1.22, all the paths must be requested with GET.
|
||||||
//
|
//
|
||||||
// To use pprof, link this package into your program:
|
// To use pprof, link this package into your program:
|
||||||
//
|
//
|
||||||
@ -75,6 +76,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
|
"internal/godebug"
|
||||||
"internal/profile"
|
"internal/profile"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -91,11 +93,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
http.HandleFunc("/debug/pprof/", Index)
|
prefix := ""
|
||||||
http.HandleFunc("/debug/pprof/cmdline", Cmdline)
|
if godebug.New("httpmuxgo121").Value() != "1" {
|
||||||
http.HandleFunc("/debug/pprof/profile", Profile)
|
prefix = "GET "
|
||||||
http.HandleFunc("/debug/pprof/symbol", Symbol)
|
}
|
||||||
http.HandleFunc("/debug/pprof/trace", Trace)
|
http.HandleFunc(prefix+"/debug/pprof/", Index)
|
||||||
|
http.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline)
|
||||||
|
http.HandleFunc(prefix+"/debug/pprof/profile", Profile)
|
||||||
|
http.HandleFunc(prefix+"/debug/pprof/symbol", Symbol)
|
||||||
|
http.HandleFunc(prefix+"/debug/pprof/trace", Trace)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cmdline responds with the running program's
|
// Cmdline responds with the running program's
|
||||||
|
Loading…
x
Reference in New Issue
Block a user