]> Cypherpunks repositories - gostls13.git/commitdiff
expvar: avoid conflict with user-defined "GET /" route.
authorMatteo Vaccari <matteo.vaccari@gmail.com>
Thu, 22 Feb 2024 16:11:20 +0000 (16:11 +0000)
committerJonathan Amsterdam <jba@google.com>
Mon, 26 Feb 2024 15:31:33 +0000 (15:31 +0000)
With the new routing style in go 1.22, declaring

    http.Handle("GET /", h)

generates a conflict with route "/debug/vars" declared in the expvar
package. You get an error such as:

panic: pattern "GET /" (registered at ...) conflicts with pattern
"/debug/vars" (registered at ...expvar.go:384): GET / matches fewer
methods than /debug/vars, but has a more general path pattern

This patch prevents that error.  Adding GET is correct because no other
method makes sense with /debug/vars.

We preserve the traditional behaviour when GODEBUG=httpmuxgo121=1 is
specified.

Fixes #65723

Change-Id: Id2b963ebad41a1ebdcceb73baf3436d59aac73a0
GitHub-Last-Rev: 9c2b9f74a7c8a3c756e9948814231b8f4e60b216
GitHub-Pull-Request: golang/go#65745
Reviewed-on: https://go-review.googlesource.com/c/go/+/564735
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
src/expvar/expvar.go

index 954d63d17f52b10ec1de8a6cb227c5993b981b26..5e9034e5d7a976462dab7861763c1a460dca3000 100644 (file)
@@ -4,7 +4,8 @@
 
 // Package expvar provides a standardized interface to public variables, such
 // as operation counters in servers. It exposes these variables via HTTP at
-// /debug/vars in JSON format.
+// /debug/vars in JSON format. As of Go 1.22, the /debug/vars request must
+// use GET.
 //
 // Operations to set or modify these public variables are atomic.
 //
@@ -23,6 +24,7 @@ package expvar
 
 import (
        "encoding/json"
+       "internal/godebug"
        "log"
        "math"
        "net/http"
@@ -381,7 +383,11 @@ func memstats() any {
 }
 
 func init() {
-       http.HandleFunc("/debug/vars", expvarHandler)
+       if godebug.New("httpmuxgo121").Value() == "1" {
+               http.HandleFunc("/debug/vars", expvarHandler)
+       } else {
+               http.HandleFunc("GET /debug/vars", expvarHandler)
+       }
        Publish("cmdline", Func(cmdline))
        Publish("memstats", Func(memstats))
 }