Skip to content

net/http: ServeMux.Handler does not populate named path wildcards #69623

Closed
@chressie

Description

@chressie

Go version

go version devel go1.24-b17a55d095 Tue Sep 24 23:20:50 2024 +0000 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/chressie/.cache/go-build'
GOENV='/home/chressie/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/chressie/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/chressie/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/chressie/src/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/chressie/src/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.24-b17a55d095 Tue Sep 24 23:20:50 2024 +0000'
GODEBUG=''
GOTELEMETRY='on'
GOTELEMETRYDIR='/home/chressie/.config/go/telemetry'
GCCGO='/usr/bin/gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/chressie/src/go/src/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build549655017=/tmp/go-build -gno-record-gcc-switches'

What did you do?

The http.ServeMux.Handler method currently discards patterns and matches that findHandler returns.

This behavior makes it impossible to use named path wildcards in http.Handler implementations that want to use an http.ServeMux as the underlying request multiplexer.

The following program shows a common pattern that demonstrates this:

package main

import (
	"fmt"
	"net"
	"net/http"
	"time"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/{foo}", func(w http.ResponseWriter, r *http.Request) {
		fmt.Printf("handler1: {foo}==%q\n", r.PathValue("foo"))
	})
	mux.HandleFunc("/x/{bar}", func(w http.ResponseWriter, r *http.Request) {
		fmt.Printf("handler2: {bar}==%q\n", r.PathValue("bar"))
	})

	// Typical middleware that routes all requests to mux.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		h, _ := mux.Handler(r)
		h.ServeHTTP(w, r)
	})

	ln, _ := net.Listen("tcp", ":0")
	go (&http.Server{Handler: http.DefaultServeMux, Addr: ln.Addr().String()}).Serve(ln)
	http.Get("http://" + ln.Addr().String() + "/wildcard-foo")
	http.Get("http://" + ln.Addr().String() + "/x/wildcard-bar")
}

What did you see happen?

Executing above program yields

% go run t.go
handler1: {foo}==""
handler2: {bar}==""

The wildcards were not populated.

What did you expect to see?

I'd expect to see the wildcards populated.

% go run t.go
handler1: {foo}=="wildcard-foo"
handler2: {bar}=="wildcard-bar"

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions