Skip to content

Commit ac8c2d9

Browse files
committed
net/http: add Pattern field and set value with matched pattern string
1 parent e68048d commit ac8c2d9

File tree

3 files changed

+7
-61
lines changed

3 files changed

+7
-61
lines changed

src/net/http/request.go

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

323+
// Pattern is saving the pattern string of the matching handler.
324+
// This field is also available for third-party routers.
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
@@ -1561,39 +1565,3 @@ func (r *Request) requiresHTTP1() bool {
15611565
return hasToken(r.Header.Get("Connection"), "upgrade") &&
15621566
ascii.EqualFold(r.Header.Get("Upgrade"), "websocket")
15631567
}
1564-
1565-
// SetPattern parses a string into a Pattern and set in Request.
1566-
// The string's syntax is
1567-
//
1568-
// [METHOD] [HOST]/[PATH]
1569-
//
1570-
// where:
1571-
// - METHOD is an HTTP method
1572-
// - HOST is a hostname
1573-
// - PATH consists of slash-separated segments, where each segment is either
1574-
// a literal or a wildcard of the form "{name}", "{name...}", or "{$}".
1575-
//
1576-
// METHOD, HOST and PATH are all optional; that is, the string can be "/".
1577-
// If METHOD is present, it must be followed by at least one space or tab.
1578-
// Wildcard names must be valid Go identifiers.
1579-
// The "{$}" and "{name...}" wildcard must occur at the end of PATH.
1580-
// PATH may end with a '/'.
1581-
// Wildcard names in a path must be distinct.
1582-
func (r *Request) SetPattern(s string) error {
1583-
pat, err := parsePattern(s)
1584-
if err != nil {
1585-
return err
1586-
}
1587-
r.pat = pat
1588-
return nil
1589-
}
1590-
1591-
// Pattern returns the pattern that matched this request, if the request
1592-
// resulted from matching a pattern registered on a ServeMux.
1593-
// Otherwise, it returns the empty string.
1594-
func (r *Request) Pattern() string {
1595-
if r.pat == nil {
1596-
return ""
1597-
}
1598-
return r.pat.str
1599-
}

src/net/http/request_test.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -1568,8 +1568,8 @@ func TestPathValueAndPattern(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)
1571+
if r.Pattern != test.pattern {
1572+
t.Errorf("pattern: got %s, want %s", r.Pattern, test.pattern)
15731573
}
15741574
})
15751575
server := httptest.NewServer(mux)
@@ -1655,25 +1655,3 @@ func TestStatus(t *testing.T) {
16551655
}
16561656
}
16571657
}
1658-
1659-
func TestSetPattern(t *testing.T) {
1660-
patterns := []string{
1661-
"GET /",
1662-
"HEAD /test",
1663-
"/",
1664-
"/foo/bar",
1665-
"GET /foo/{b...}",
1666-
"GET /{f}",
1667-
}
1668-
1669-
req := new(Request)
1670-
for _, pattern := range patterns {
1671-
err := req.SetPattern(pattern)
1672-
if err != nil {
1673-
t.Fatal(err)
1674-
}
1675-
if req.Pattern() != pattern {
1676-
t.Errorf("pattern doesn't match, got %s, want %s", req.Pattern(), pattern)
1677-
}
1678-
}
1679-
}

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)