* fix: don't drop handlers that collide with a Mount()/Route() pattern
Mount() (and Route(), which is sugar for it) registers a synthetic
"stub" handler on its exact mount pattern to connect it to the
subrouter. node.routes() used that stub as a signal to hide the whole
node from Routes()/Walk() — but if a real handler was also registered
on that same pattern (e.g. r.Get("/bar", h) next to
r.Route("/bar", ...)), it got hidden too, even though the route is
perfectly routable.
routes() now compares each handler against the stub by identity
(sameHandler, using reflection since http.HandlerFunc isn't ==
comparable) and only hides the stub itself, keeping any real handler
that shares the node. The "*" slot is only filtered on leaf nodes,
since Walk() also reads Handlers["*"] to detect a *ChainHandler and
propagate With() middleware into a subrouter — that entry has to
survive even when it's the same value as the stub.
Fixes#830.
Also adds a regression test for #750 (middlewares registered on a
Group not reaching handlers on a Route() mounted inside it), which
turns out to already be fixed on master — this just locks it in.
* Trim comments in routes() to just the why
* Rename sameHandler to equalHandlers; narrow the reflect switch to Func
Chan, Pointer, and UnsafePointer are already == comparable in Go —
only Func, Map, and Slice aren't, and Map/Slice identity was never a
real case here (the stub is always http.HandlerFunc or *ChainHandler).
* Explain why Routes() reports /api/* and not /api/{id} in TestRoutesHidesMountStub
* Trim comment in TestRoutesHidesMountStub
Go 1.21 brings us the builtin min func and slices.Contains.
Go 1.22 brings us range over int and fixed for loop scoping.
We were also able to drop the build tags from the path value code and
inline the three-line function directly. This should still work on
tinygo as they claim to support Go 1.24.
name old time/op new time/op
Mux/route:/sup/123/foo/this-8 1.24µs ± 0% 1.99µs ± 0%
name old alloc/op new alloc/op
Mux/route:/sup/123/foo/this-8 456B ± 0% 448B ± 0%
name old allocs/op new allocs/op
Mux/route:/sup/123/foo/this-8 4.00 ± 0% 3.00 ± 0%
Add Match(..) bool to chi.Routes, and add middleware.GetHead to automatically route missing head requests to GET handlers
* Put chi.Routes pointer on the route Context instead of having to pass to the middleware
* Rename FindHandler to Find, and add breaking test when searching subtrees
* Rename middleware to GetHead, and update Mux#Find method to search subroutes
* Update README, router at ~1000 LOC
* Redefine Routes#Find(..) http.Handler to Routes#Match(..) bool
* Update benchmarks
* Slightly adjust GetHead middleware to make it clearer