Skip to content

Commit 6a08e80

Browse files
nicholashusingopherbot
authored andcommitted
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 golang#74422 Change-Id: I3cc5b4d195ab0591a9139225b632cbe17f4290db Reviewed-on: https://go-review.googlesource.com/c/go/+/699915 Reviewed-by: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 8bcda6c commit 6a08e80

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/net/http/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,9 +2759,12 @@ func (mux *ServeMux) matchOrRedirect(host, method, path string, u *url.URL) (_ *
27592759
defer mux.mu.RUnlock()
27602760

27612761
n, matches := mux.tree.match(host, method, path)
2762-
// If we have an exact match, or we were asked not to try trailing-slash redirection,
2763-
// or the URL already has a trailing slash, then we're done.
2764-
if !exactMatch(n, path) && u != nil && !strings.HasSuffix(path, "/") {
2762+
// We can terminate here if any of the following is true:
2763+
// - We have an exact match already.
2764+
// - We were asked not to try trailing slash redirection.
2765+
// - The URL already has a trailing slash.
2766+
// - The URL is an empty string.
2767+
if !exactMatch(n, path) && u != nil && !strings.HasSuffix(path, "/") && path != "" {
27652768
// If there is an exact match with a trailing slash, then redirect.
27662769
path += "/"
27672770
n2, _ := mux.tree.match(host, method, path)

src/net/http/server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func TestFindHandler(t *testing.T) {
9797
{"GET", "/foo/x", "&http.handler{i:2}"},
9898
{"GET", "/bar/x", "&http.handler{i:4}"},
9999
{"GET", "/bar", `&http.redirectHandler{url:"/bar/", code:301}`},
100+
{"CONNECT", "", "(http.HandlerFunc)(.*)"},
100101
{"CONNECT", "/", "&http.handler{i:1}"},
101102
{"CONNECT", "//", "&http.handler{i:1}"},
102103
{"CONNECT", "//foo", "&http.handler{i:5}"},
@@ -112,7 +113,7 @@ func TestFindHandler(t *testing.T) {
112113
r.URL = &url.URL{Path: test.path}
113114
gotH, _, _, _ := mux.findHandler(&r)
114115
got := fmt.Sprintf("%#v", gotH)
115-
if got != test.wantHandler {
116+
if !regexp.MustCompile(test.wantHandler).MatchString(got) {
116117
t.Errorf("%s %q: got %q, want %q", test.method, test.path, got, test.wantHandler)
117118
}
118119
}

0 commit comments

Comments
 (0)