We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8bcda6c commit 6a08e80Copy full SHA for 6a08e80
src/net/http/server.go
@@ -2759,9 +2759,12 @@ func (mux *ServeMux) matchOrRedirect(host, method, path string, u *url.URL) (_ *
2759
defer mux.mu.RUnlock()
2760
2761
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, "/") {
+ // We can terminate here if any of the following is true:
+ // - We have an exact match already.
+ // - 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 != "" {
2768
// If there is an exact match with a trailing slash, then redirect.
2769
path += "/"
2770
n2, _ := mux.tree.match(host, method, path)
src/net/http/server_test.go
@@ -97,6 +97,7 @@ func TestFindHandler(t *testing.T) {
97
{"GET", "/foo/x", "&http.handler{i:2}"},
98
{"GET", "/bar/x", "&http.handler{i:4}"},
99
{"GET", "/bar", `&http.redirectHandler{url:"/bar/", code:301}`},
100
+ {"CONNECT", "", "(http.HandlerFunc)(.*)"},
101
{"CONNECT", "/", "&http.handler{i:1}"},
102
{"CONNECT", "//", "&http.handler{i:1}"},
103
{"CONNECT", "//foo", "&http.handler{i:5}"},
@@ -112,7 +113,7 @@ func TestFindHandler(t *testing.T) {
112
113
r.URL = &url.URL{Path: test.path}
114
gotH, _, _, _ := mux.findHandler(&r)
115
got := fmt.Sprintf("%#v", gotH)
- if got != test.wantHandler {
116
+ if !regexp.MustCompile(test.wantHandler).MatchString(got) {
117
t.Errorf("%s %q: got %q, want %q", test.method, test.path, got, test.wantHandler)
118
}
119
0 commit comments