Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,23 @@ func (c *Context) getQueries() url.Values {
return c.cachedQueries
}

// onlyRequestContext wraps *Context so matchers can only reach the [RequestContext] interface and cannot
// misuse the full Context API.
type onlyRequestContext struct {
c *Context
}

func (s onlyRequestContext) Request() *http.Request { return s.c.Request() }
func (s onlyRequestContext) RemoteIP() *net.IPAddr { return s.c.RemoteIP() }
func (s onlyRequestContext) ClientIP() (*net.IPAddr, error) { return s.c.ClientIP() }
func (s onlyRequestContext) Method() string { return s.c.Method() }
func (s onlyRequestContext) Path() string { return s.c.Path() }
func (s onlyRequestContext) EscapedPath() string { return s.c.EscapedPath() }
func (s onlyRequestContext) Host() string { return s.c.Host() }
func (s onlyRequestContext) QueryParams() url.Values { return s.c.QueryParams() }
func (s onlyRequestContext) QueryParam(name string) string { return s.c.QueryParam(name) }
func (s onlyRequestContext) Header(key string) string { return s.c.Header(key) }

// WrapF is an adapter for wrapping [http.HandlerFunc] and returns a [HandlerFunc] function.
// The route parameters are being accessed by the wrapped handler through the context.
func WrapF(f http.HandlerFunc) HandlerFunc {
Expand Down
7 changes: 4 additions & 3 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r *Route) String() string {

// match reports whether the request satisfies this route's method constraint (if any)
// and all attached matchers.
func (r *Route) match(method string, c RequestContext) bool {
func (r *Route) match(method string, c *Context) bool {
// Fast path: routes with exactly one method and no matchers cache that
// method in methodFast.
if r.methodFast == method {
Expand All @@ -149,7 +149,7 @@ func (r *Route) match(method string, c RequestContext) bool {
// inlinable.
//
//go:noinline
func (r *Route) matchSlow(method string, c RequestContext) bool {
func (r *Route) matchSlow(method string, c *Context) bool {
methods := r.methods
switch len(methods) {
case 0:
Expand All @@ -165,8 +165,9 @@ func (r *Route) matchSlow(method string, c RequestContext) bool {
return false
}
}
sealed := onlyRequestContext{c}
for _, m := range r.matchers {
if !m.Match(c) {
if !m.Match(sealed) {
return false
}
}
Expand Down
Loading