aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/server.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2023-09-13 16:58:24 -0400
committerJonathan Amsterdam <jba@google.com>2023-09-14 00:00:28 +0000
commit495830acd6976c8a2b39dd4aa4fdc105ad72de52 (patch)
treea44f8bcbd8d75e67e2cd3ce5c607a6cbacde1616 /src/net/http/server.go
parentfccd0b9b70255099691deca5dc1d577efcfc889b (diff)
downloadgo-495830acd6976c8a2b39dd4aa4fdc105ad72de52.tar.xz
net/http: implement path value methods on Request
Add Request.PathValue and Request.SetPathValue, and the fields on Request required to support them. Populate those fields in ServeMux.ServeHTTP. Updates #61410. Change-Id: Ic88cb865b0d865a30d3b35ece8e0382c58ef67d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/528355 Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/net/http/server.go')
-rw-r--r--src/net/http/server.go24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/net/http/server.go b/src/net/http/server.go
index 74362a69ad..a229169197 100644
--- a/src/net/http/server.go
+++ b/src/net/http/server.go
@@ -2410,14 +2410,15 @@ func stripHostPort(h string) string {
// If there is no registered handler that applies to the request,
// Handler returns a “page not found” handler and an empty pattern.
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
- return mux.findHandler(r)
+ h, p, _, _ := mux.findHandler(r)
+ return h, p
}
// findHandler finds a handler for a request.
// If there is a matching handler, it returns it and the pattern that matched.
// Otherwise it returns a Redirect or NotFound handler with the path that would match
// after the redirect.
-func (mux *ServeMux) findHandler(r *Request) (h Handler, patStr string) {
+func (mux *ServeMux) findHandler(r *Request) (h Handler, patStr string, _ *pattern, matches []string) {
var n *routingNode
// TODO(jba): use escaped path. This is an independent change that is also part
// of proposal https://go.dev/issue/61410.
@@ -2430,11 +2431,11 @@ func (mux *ServeMux) findHandler(r *Request) (h Handler, patStr string) {
// but the path canonicalization does not.
_, _, u := mux.matchOrRedirect(r.URL.Host, r.Method, path, r.URL)
if u != nil {
- return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
+ return RedirectHandler(u.String(), StatusMovedPermanently), u.Path, nil, nil
}
// Redo the match, this time with r.Host instead of r.URL.Host.
// Pass a nil URL to skip the trailing-slash redirect logic.
- n, _, _ = mux.matchOrRedirect(r.Host, r.Method, path, nil)
+ n, matches, _ = mux.matchOrRedirect(r.Host, r.Method, path, nil)
} else {
// All other requests have any port stripped and path cleaned
// before passing to mux.handler.
@@ -2444,9 +2445,9 @@ func (mux *ServeMux) findHandler(r *Request) (h Handler, patStr string) {
// If the given path is /tree and its handler is not registered,
// redirect for /tree/.
var u *url.URL
- n, _, u = mux.matchOrRedirect(host, r.Method, path, r.URL)
+ n, matches, u = mux.matchOrRedirect(host, r.Method, path, r.URL)
if u != nil {
- return RedirectHandler(u.String(), StatusMovedPermanently), u.Path
+ return RedirectHandler(u.String(), StatusMovedPermanently), u.Path, nil, nil
}
if path != r.URL.Path {
// Redirect to cleaned path.
@@ -2455,14 +2456,14 @@ func (mux *ServeMux) findHandler(r *Request) (h Handler, patStr string) {
patStr = n.pattern.String()
}
u := &url.URL{Path: path, RawQuery: r.URL.RawQuery}
- return RedirectHandler(u.String(), StatusMovedPermanently), patStr
+ return RedirectHandler(u.String(), StatusMovedPermanently), patStr, nil, nil
}
}
if n == nil {
// TODO(jba): support 405 (MethodNotAllowed) by checking for patterns with different methods.
- return NotFoundHandler(), ""
+ return NotFoundHandler(), "", nil, nil
}
- return n.handler, n.pattern.String()
+ return n.handler, n.pattern.String(), n.pattern, matches
}
// matchOrRedirect looks up a node in the tree that matches the host, method and path.
@@ -2551,8 +2552,9 @@ func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
w.WriteHeader(StatusBadRequest)
return
}
- h, _ := mux.findHandler(r)
- // TODO(jba); save matches in Request.
+ h, _, pat, matches := mux.findHandler(r)
+ r.pat = pat
+ r.matches = matches
h.ServeHTTP(w, r)
}