Commit graph

86 commits

Author SHA1 Message Date
James Raspass
77074b34b9
Use slices.Backward where appropriate (#1165) 2026-08-31 11:15:36 +02:00
Jonathan Hall
9b6ddcddb9
Don't duplicate methods in Allow: header for 405 responses (#1029) 2026-08-20 11:16:25 +02:00
Vojtech Vitek (golang.cz)
29164f023b
fix: don't drop handlers that collide with a Mount()/Route() pattern (#1148)
* 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
2026-08-20 10:55:43 +02:00
Vojtech Vitek (golang.cz)
caf87e6e1b
feat(mux): support http QUERY method ietf rfc10008 (#1132)
* feat(mux): support http QUERY method ietf rfc10008

* refactor(mux): unexport the temporary QUERY method constant

---------

Co-authored-by: PJ O'Leary <peej@oleary.com>
2026-07-05 20:13:24 +02:00
James Raspass
be60b2ec57
Simplify chi.walk with slices.Concat (#1053)
Should perform better too as the initial allocation will be sized to fit
both slices unlike before.
2026-02-19 14:12:24 +01:00
Lukas Jenicek
903cff2596
Propagate inline middlewares across mounted subrouters (#1049)
* add tests to cover broken scenarios

* fix missing middlewares in Walk function
2026-02-12 12:31:51 +01:00
James Raspass
142fada90f
Use strings.ReplaceAll where applicable (#1046)
We already use it in the codebase, it's been around since 1.12, and is
considered best practice since it avoids hardcoding a magic number.
2026-02-08 11:48:22 +01:00
Ehden Sinai
9fb4a15daa
update reverseMethodMap in RegisterMethod (#1022)
Fixes #1021.
2025-10-08 18:00:50 +02:00
James Raspass
a52c582b53
Bump minimum Go and use new features (#1017)
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.
2025-09-17 10:51:46 +02:00
James Raspass
9b9fb55def
Replace methodTypString func with reverseMethodMap (#1018)
This code predates the introduction of the reverseMethodMap.
2025-08-26 19:56:01 +02:00
James Raspass
877e876205
Use strings.Cut in a few places (#971)
Now that we're 1.20+ instead of 1.14+ we can use 1.18's strings.Cut in a
few places to simplify code.
2025-02-04 16:13:25 +01:00
Ewen Quimerc'h
4b14b832d5
Adds the Allow header on 405 response (#776) 2023-07-13 11:36:22 -04:00
Peter Kieltyka
34d54d6d59
methodTyp as uint to fix go 1.14 compatibility (#599) 2021-03-07 07:33:48 -05:00
C H
bed8648a22
optimize sructs with memory alignment / padding (#596) 2021-03-07 07:09:40 -05:00
C H
fb48a47641
Memory alignment on structs (#589) 2021-02-28 16:49:55 -05:00
Eduardo Sachser
423901dfd5
Fixing RegisterMethod method value generation (#573)
* Fix: RegisterMethod registering first method with same value as mTRACE

* Len of methods must not be greater than IntSize - 2
2021-02-05 20:28:56 -05:00
Masaya Watanabe
681ea79f56
Fix: validate empty path param (#567)
Thanks @matsuyoshi30
2020-12-24 12:28:05 -05:00
alresvor
3950456604
remove unnecessary type conversions (#523)
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%
2020-06-26 10:06:54 -04:00
Casey Hadden
5704d7ee98
fix: handle methodnotallowed with path variables (#512) 2020-05-22 09:51:03 -04:00
Joakim Hamrén
99ad97ff6f
Route recursive-search regexp patterns (#506) 2020-04-16 08:10:37 -04:00
Peter Kieltyka
b612eb4a06 Comments 2020-02-19 22:26:21 -05:00
Peter Kieltyka
17fb1065d2 walk method will hide /*/ internal detail when generating route list 2020-01-10 19:23:42 -05:00
Peter Kieltyka
3658d98ee4 fix regexp routing to include default value when param is not matched 2020-01-08 21:55:02 -05:00
Christian Muehlhaeuser
ece538c84e Simplify code
- Removed unnecessary conversions
- 0 is the default capacity, no need to specify it
2019-08-06 21:13:43 -04:00
Christian Muehlhaeuser
b83ac995c3 Simplify code
- We can access the pointer members directly
- Easier to read concatenation
2019-08-06 21:12:21 -04:00
tkivisik
120ad4a206 documented Handlers map key 2019-08-06 21:09:28 -04:00
Peter Kieltyka
85905ae4dd fixes a regression for wildcard param checking 2019-02-24 11:35:16 -05:00
Peter Kieltyka
cd12532e98 fixes #333, which adds additional check to ensure wildcard is used at end of a url pattern 2019-01-09 09:32:43 -05:00
Peter Kieltyka
0ebf7795c5 fix travis for go 1.7 and go 1.8 2018-10-24 06:12:33 -04:00
Peter Kieltyka
be3aea5558 revert methodMap change - brings back support for go 1.7 and 1.8 2018-10-24 06:06:09 -04:00
Rodney
daa22f628c Exported HTTP method names to avoid manual entry (#353)
* Using package http's already defined Method constants instead of redefining them
2018-10-23 11:52:23 -04:00
Peter Kieltyka
575dd3a137 Fix #277 by setting param values for empty segments 2017-11-20 07:10:20 -08:00
Peter Kieltyka
1cbc3a2b40 Fix lint warnings 2017-11-08 17:29:18 -05:00
Peter Kieltyka
d806569952 New chi.RegisterMethod(method) to add support for custom HTTP methods (#271) 2017-10-10 17:28:23 -04:00
Domen Ipavec
624e6e12e9 Fix: regex matching only part of parameter (#258)
* Add start and end to param regex

* Added test for regex match whole param
2017-09-07 09:24:55 -07:00
Peter Kieltyka
d132b31857 middleware.GetHead (#248)
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
2017-08-31 21:51:08 -04:00
Peter Kieltyka
ba73ba0623 FindRoute now returns specific method handler along with endpoints map 2017-08-22 13:58:43 -04:00
Nic Pottier
df0b91fada Add support for using {} in regex paths (#245) 2017-08-17 12:09:07 -04:00
Peter Kieltyka
25354a53cc go vet and golint the project, release 3.1.5 2017-08-02 17:27:05 -04:00
Vojtech Vitek
524a020446 Implement router tree Walker, an analogy to filepath.Walk (#222)
* Implement router tree Walker, an analogy to filepath.Walk

func Walk(r Routes, walkFn WalkFunc) error
type WalkFunc func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error

* Move Walker to tree.go
2017-07-25 13:24:32 -04:00
Peter Kieltyka
7e6b856011 Flatten Context.URLParams data structure 2017-07-24 16:38:22 -04:00
Peter Kieltyka
a40fe4fc12 Add test case for mux mounts 2017-07-21 10:22:23 -04:00
Peter Kieltyka
49e7138cab organize routing patterns as endpoints with pattern, handler, paramKeys in the leaf nodes 2017-07-09 19:17:12 -04:00
Peter Kieltyka
25201e8a88 Export access to Context.URLParams 2017-07-08 18:12:46 -04:00
Peter Kieltyka
45f7a16ad6 Continue searching for route on empty param value case 2017-07-08 16:45:31 -04:00
Peter Kieltyka
3a7936b243 short-circuit and return no matching route for empty param values (#224) 2017-07-08 14:31:51 -04:00
Niklas Janlert
4cbb5e2f2a Avoid matching parameters across path segments (#216)
Fixes #215
2017-06-28 10:30:38 -04:00
Peter Kieltyka
34b58319a8 Add support for LINK/UNLINK http methods, via Method() 2017-06-21 11:37:04 -04:00
Peter Kieltyka
02e6bbdfd1 v3.0.0 release 2017-06-21 10:43:59 -04:00
Peter Kieltyka
8f717baab4 v3 beta 1 2017-06-21 10:43:59 -04:00