]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add Pattern field in Request to return matched pattern info
authorChen.Zhidong <njutczd@gmail.com>
Thu, 16 May 2024 16:27:58 +0000 (16:27 +0000)
committerJonathan Amsterdam <jba@google.com>
Thu, 16 May 2024 18:42:34 +0000 (18:42 +0000)
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>

api/next/66405.txt [new file with mode: 0644]
doc/next/6-stdlib/99-minor/net/http/66405.md [new file with mode: 0644]
src/net/http/request.go
src/net/http/request_test.go
src/net/http/server.go

diff --git a/api/next/66405.txt b/api/next/66405.txt
new file mode 100644 (file)
index 0000000..0b39494
--- /dev/null
@@ -0,0 +1 @@
+pkg net/http, type Request struct, Pattern string #66405
diff --git a/doc/next/6-stdlib/99-minor/net/http/66405.md b/doc/next/6-stdlib/99-minor/net/http/66405.md
new file mode 100644 (file)
index 0000000..c827b4b
--- /dev/null
@@ -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.
index bdd18adf3f3a9d0c53ccb211e1ee149fdb6e4704..f208b95c462d854e0b3b9707d33012d4e581d693 100644 (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
index a7deba46e316075888648529d5e67ca1aeb67ceb..9b6eb6e1a8bc99364bc28e1dd1f93a53db6bdac5 100644 (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()
index b76c869567185b77652816b34f963816bb6a43dc..9786a68129246bc915c312e881aa5083163634e5 100644 (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)
 }