Skip to content

Commit a523152

Browse files
sillydongjba
authored andcommitted
net/http: add Pattern field in Request to return matched pattern info
Fixes #66405 Change-Id: Icd80944b6ca081aa7addd4fb85d2b3c29b6c9542 GitHub-Last-Rev: c6e3274 GitHub-Pull-Request: #66618 Reviewed-on: https://go-review.googlesource.com/c/go/+/574997 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 06b4578 commit a523152

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

api/next/66405.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg net/http, type Request struct, Pattern string #66405
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
For inbound requests, the new [Request.Pattern] field contains the [ServeMux]
2+
pattern (if any) that matched the request. This field is not set when
3+
`GODEBUG=httpmuxgo121=1` is set.

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
@@ -1576,6 +1576,9 @@ func TestPathValue(t *testing.T) {
15761576
t.Errorf("%q, %q: got %q, want %q", test.pattern, name, got, want)
15771577
}
15781578
}
1579+
if r.Pattern != test.pattern {
1580+
t.Errorf("pattern: got %s, want %s", r.Pattern, test.pattern)
1581+
}
15791582
})
15801583
server := httptest.NewServer(mux)
15811584
defer server.Close()

src/net/http/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2703,7 +2703,7 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
27032703
if use121 {
27042704
h, _ = mux.mux121.findHandler(r)
27052705
} else {
2706-
h, _, r.pat, r.matches = mux.findHandler(r)
2706+
h, r.Pattern, r.pat, r.matches = mux.findHandler(r)
27072707
}
27082708
h.ServeHTTP(w, r)
27092709
}

0 commit comments

Comments
 (0)