net/http: skip redirecting in ServeMux when URL path for CONNECT is empty

In 1.21 ServeMux, we had a special-case to skip redirection when a given
path is empty for CONNECT requests:
https://go.googlesource.com/go/+/refs/tags/go1.24.4/src/net/http/servemux121.go#205.

This special case seems to not have been carried over to 1.22 ServeMux.
This causes needless redirection, which this CL fixes.

Fixes #74422

Change-Id: I3cc5b4d195ab0591a9139225b632cbe17f4290db
Reviewed-on: https://go-review.googlesource.com/c/go/+/699915
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Nicholas Husin 2025-08-29 10:34:10 -04:00 committed by Gopher Robot
parent 8bcda6c79d
commit 6a08e80399
2 changed files with 8 additions and 4 deletions

View file

@ -2759,9 +2759,12 @@ func (mux *ServeMux) matchOrRedirect(host, method, path string, u *url.URL) (_ *
defer mux.mu.RUnlock() defer mux.mu.RUnlock()
n, matches := mux.tree.match(host, method, path) n, matches := mux.tree.match(host, method, path)
// If we have an exact match, or we were asked not to try trailing-slash redirection, // We can terminate here if any of the following is true:
// or the URL already has a trailing slash, then we're done. // - We have an exact match already.
if !exactMatch(n, path) && u != nil && !strings.HasSuffix(path, "/") { // - We were asked not to try trailing slash redirection.
// - The URL already has a trailing slash.
// - The URL is an empty string.
if !exactMatch(n, path) && u != nil && !strings.HasSuffix(path, "/") && path != "" {
// If there is an exact match with a trailing slash, then redirect. // If there is an exact match with a trailing slash, then redirect.
path += "/" path += "/"
n2, _ := mux.tree.match(host, method, path) n2, _ := mux.tree.match(host, method, path)

View file

@ -97,6 +97,7 @@ func TestFindHandler(t *testing.T) {
{"GET", "/foo/x", "&http.handler{i:2}"}, {"GET", "/foo/x", "&http.handler{i:2}"},
{"GET", "/bar/x", "&http.handler{i:4}"}, {"GET", "/bar/x", "&http.handler{i:4}"},
{"GET", "/bar", `&http.redirectHandler{url:"/bar/", code:301}`}, {"GET", "/bar", `&http.redirectHandler{url:"/bar/", code:301}`},
{"CONNECT", "", "(http.HandlerFunc)(.*)"},
{"CONNECT", "/", "&http.handler{i:1}"}, {"CONNECT", "/", "&http.handler{i:1}"},
{"CONNECT", "//", "&http.handler{i:1}"}, {"CONNECT", "//", "&http.handler{i:1}"},
{"CONNECT", "//foo", "&http.handler{i:5}"}, {"CONNECT", "//foo", "&http.handler{i:5}"},
@ -112,7 +113,7 @@ func TestFindHandler(t *testing.T) {
r.URL = &url.URL{Path: test.path} r.URL = &url.URL{Path: test.path}
gotH, _, _, _ := mux.findHandler(&r) gotH, _, _, _ := mux.findHandler(&r)
got := fmt.Sprintf("%#v", gotH) got := fmt.Sprintf("%#v", gotH)
if got != test.wantHandler { if !regexp.MustCompile(test.wantHandler).MatchString(got) {
t.Errorf("%s %q: got %q, want %q", test.method, test.path, got, test.wantHandler) t.Errorf("%s %q: got %q, want %q", test.method, test.path, got, test.wantHandler)
} }
} }