aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChen.Zhidong <njutczd@gmail.com>2024-05-16 16:27:58 +0000
committerJonathan Amsterdam <jba@google.com>2024-05-16 18:42:34 +0000
commita523152ea1df8d39d923ed90d19662896eff0607 (patch)
treea962a3f08a8394371615995b982b42f10efb9b9c /src
parent06b45781603ec6998012ac63b2c0bda32600f682 (diff)
downloadgo-a523152ea1df8d39d923ed90d19662896eff0607.tar.gz
go-a523152ea1df8d39d923ed90d19662896eff0607.zip
net/http: add Pattern field in Request to return matched pattern info
Fixes #66405 Change-Id: Icd80944b6ca081aa7addd4fb85d2b3c29b6c9542 GitHub-Last-Rev: c6e32742c4b733230c82627571b423de45997c24 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>
Diffstat (limited to 'src')
-rw-r--r--src/net/http/request.go4
-rw-r--r--src/net/http/request_test.go5
-rw-r--r--src/net/http/server.go2
3 files changed, 9 insertions, 2 deletions
diff --git a/src/net/http/request.go b/src/net/http/request.go
index bdd18adf3f..f208b95c46 100644
--- a/src/net/http/request.go
+++ b/src/net/http/request.go
@@ -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
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index a7deba46e3..9b6eb6e1a8 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -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()
diff --git a/src/net/http/server.go b/src/net/http/server.go
index b76c869567..9786a68129 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -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)
}