diff --git a/context.go b/context.go index a475c37..0f0119e 100644 --- a/context.go +++ b/context.go @@ -120,17 +120,6 @@ type cTx struct { tsr bool } -// Reset resets the [Context] to its initial state, attaching the provided [ResponseWriter] and [http.Request]. -func (c *cTx) Reset(w ResponseWriter, r *http.Request) { - c.req = r - c.w = w - c.tsr = false - c.cachedQuery = nil - c.route = nil - c.scope = RouteHandler - *c.params = (*c.params)[:0] -} - // Rehydrate updates the current [Context] to serve the provided [Route], bypassing the need for a full tree lookup. // It succeeds only if the [http.Request]'s URL path strictly matches the given [Route]. If successful, the internal state // of the context is updated, allowing the context to serve the route directly, regardless of whether the route @@ -169,13 +158,13 @@ func (c *cTx) Rehydrate(route *Route) bool { // reset resets the [Context] to its initial state, attaching the provided [http.ResponseWriter] and [http.Request]. // Caution: always pass the original [http.ResponseWriter] to this method, not the [ResponseWriter] itself, to -// avoid wrapping the [ResponseWriter] within itself. Use wisely! +// avoid wrapping the [ResponseWriter] within itself. Use wisely! Note that ServeHTTP is managing the reset of +// c.route and c.tsr. func (c *cTx) reset(w http.ResponseWriter, r *http.Request) { c.rec.reset(w) c.req = r c.w = &c.rec c.cachedQuery = nil - c.route = nil c.scope = RouteHandler *c.params = (*c.params)[:0] } @@ -188,6 +177,17 @@ func (c *cTx) resetNil() { *c.params = (*c.params)[:0] } +// resetWithWriter resets the [Context] to its initial state, attaching the provided [ResponseWriter] and [http.Request]. +func (c *cTx) resetWithWriter(w ResponseWriter, r *http.Request) { + c.req = r + c.w = w + c.tsr = false + c.cachedQuery = nil + c.route = nil + c.scope = RouteHandler + *c.params = (*c.params)[:0] +} + // Request returns the [http.Request]. func (c *cTx) Request() *http.Request { return c.req diff --git a/fox.go b/fox.go index d9339da..57845dd 100644 --- a/fox.go +++ b/fox.go @@ -373,6 +373,7 @@ func (fox *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { if n.route.redirectTrailingSlash && target == CleanPath(target) { // Reset params as it may have recorded wildcard segment (the context may still be used in a middleware) *c.params = (*c.params)[:0] + c.route = nil c.tsr = false c.scope = RedirectHandler fox.tsrRedirect(c) @@ -384,6 +385,7 @@ func (fox *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Reset params as it may have recorded wildcard segment (the context may still be used in no route, no method and // automatic option handler or middleware) *c.params = (*c.params)[:0] + c.route = nil c.tsr = false NoMethodFallback: diff --git a/tree.go b/tree.go index 79ef736..f185b85 100644 --- a/tree.go +++ b/tree.go @@ -191,7 +191,7 @@ func (t *Tree) Lookup(w ResponseWriter, r *http.Request) (route *Route, cc Conte } c := t.ctx.Get().(*cTx) - c.Reset(w, r) + c.resetWithWriter(w, r) target := r.URL.Path if len(r.URL.RawPath) > 0 {