Skip to content

Commit 2eecc0a

Browse files
committed
net/http: add Request.Pattern
1 parent ba9c445 commit 2eecc0a

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/net/http/request.go

+4
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ type Request struct {
320320
// redirects.
321321
Response *Response
322322

323+
// Pattern is the [ServeMux] pattern that matched the request.
324+
// It is empty if the request was not matched against a pattern.
325+
Pattern string
326+
323327
// ctx is either the client or server context. It should only
324328
// be modified via copying the whole Request using Clone or WithContext.
325329
// It is unexported to prevent people from using Context wrong

src/net/http/request_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ func TestPathValueNoMatch(t *testing.T) {
15271527
}
15281528
}
15291529

1530-
func TestPathValue(t *testing.T) {
1530+
func TestPathValueAndPattern(t *testing.T) {
15311531
for _, test := range []struct {
15321532
pattern string
15331533
url string
@@ -1568,6 +1568,9 @@ func TestPathValue(t *testing.T) {
15681568
t.Errorf("%q, %q: got %q, want %q", test.pattern, name, got, want)
15691569
}
15701570
}
1571+
if r.Pattern != test.pattern {
1572+
t.Errorf("pattern: got %s, want %s", r.Pattern, test.pattern)
1573+
}
15711574
})
15721575
server := httptest.NewServer(mux)
15731576
defer server.Close()

src/net/http/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2682,7 +2682,7 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
26822682
if use121 {
26832683
h, _ = mux.mux121.findHandler(r)
26842684
} else {
2685-
h, _, r.pat, r.matches = mux.findHandler(r)
2685+
h, r.Pattern, r.pat, r.matches = mux.findHandler(r)
26862686
}
26872687
h.ServeHTTP(w, r)
26882688
}

0 commit comments

Comments
 (0)