1
0
mirror of https://github.com/golang/go synced 2024-11-25 00:07:56 -07:00

net/http: add Pattern field in Request to return matched pattern info

Fixes #66405

Change-Id: Icd80944b6ca081aa7addd4fb85d2b3c29b6c9542
GitHub-Last-Rev: c6e32742c4
GitHub-Pull-Request: golang/go#66618
Reviewed-on: https://go-review.googlesource.com/c/go/+/574997
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Chen.Zhidong 2024-05-16 16:27:58 +00:00 committed by Jonathan Amsterdam
parent 06b4578160
commit a523152ea1
5 changed files with 13 additions and 2 deletions

1
api/next/66405.txt Normal file
View File

@ -0,0 +1 @@
pkg net/http, type Request struct, Pattern string #66405

View File

@ -0,0 +1,3 @@
For inbound requests, the new [Request.Pattern] field contains the [ServeMux]
pattern (if any) that matched the request. This field is not set when
`GODEBUG=httpmuxgo121=1` is set.

View File

@ -320,6 +320,10 @@ type Request struct {
// redirects.
Response *Response
// Pattern is the [ServeMux] pattern that matched the request.
// It is empty if the request was not matched against a pattern.
Pattern string
// ctx is either the client or server context. It should only
// be modified via copying the whole Request using Clone or WithContext.
// It is unexported to prevent people from using Context wrong

View File

@ -1527,7 +1527,7 @@ func TestPathValueNoMatch(t *testing.T) {
}
}
func TestPathValue(t *testing.T) {
func TestPathValueAndPattern(t *testing.T) {
for _, test := range []struct {
pattern string
url string
@ -1576,6 +1576,9 @@ func TestPathValue(t *testing.T) {
t.Errorf("%q, %q: got %q, want %q", test.pattern, name, got, want)
}
}
if r.Pattern != test.pattern {
t.Errorf("pattern: got %s, want %s", r.Pattern, test.pattern)
}
})
server := httptest.NewServer(mux)
defer server.Close()

View File

@ -2703,7 +2703,7 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
if use121 {
h, _ = mux.mux121.findHandler(r)
} else {
h, _, r.pat, r.matches = mux.findHandler(r)
h, r.Pattern, r.pat, r.matches = mux.findHandler(r)
}
h.ServeHTTP(w, r)
}