Skip to content

Commit

Permalink
net/http: fix ServeMux pattern registration
Browse files Browse the repository at this point in the history
When the httpmuxgo121 GODEBUG setting was active, we were registering
patterns in the old and the new way. Fix to register only in the old
way.

Change-Id: Ibc1fd41e7f4d162ee5bc34575df409e1db5657cd
Reviewed-on: https://go-review.googlesource.com/c/go/+/533095
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Olena Synenka <olenasynenka@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
  • Loading branch information
jba authored and pull[bot] committed Mar 6, 2024
1 parent e84c007 commit 2162738
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2686,8 +2686,9 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
func (mux *ServeMux) Handle(pattern string, handler Handler) {
if use121 {
mux.mux121.handle(pattern, handler)
} else {
mux.register(pattern, handler)
}
mux.register(pattern, handler)
}

// HandleFunc registers the handler function for the given pattern.
Expand All @@ -2696,26 +2697,29 @@ func (mux *ServeMux) Handle(pattern string, handler Handler) {
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if use121 {
mux.mux121.handleFunc(pattern, handler)
} else {
mux.register(pattern, HandlerFunc(handler))
}
mux.register(pattern, HandlerFunc(handler))
}

// Handle registers the handler for the given pattern in [DefaultServeMux].
// The documentation for [ServeMux] explains how patterns are matched.
func Handle(pattern string, handler Handler) {
if use121 {
DefaultServeMux.mux121.handle(pattern, handler)
} else {
DefaultServeMux.register(pattern, handler)
}
DefaultServeMux.register(pattern, handler)
}

// HandleFunc registers the handler function for the given pattern in [DefaultServeMux].
// The documentation for [ServeMux] explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if use121 {
DefaultServeMux.mux121.handleFunc(pattern, handler)
} else {
DefaultServeMux.register(pattern, HandlerFunc(handler))
}
DefaultServeMux.register(pattern, HandlerFunc(handler))
}

func (mux *ServeMux) register(pattern string, handler Handler) {
Expand Down

0 comments on commit 2162738

Please sign in to comment.