Skip to content

Commit

Permalink
minor refactor of branch
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Vulaj <avulaj@redhat.com>
Signed-off-by: Jakob Ackermann <das7pad@outlook.com>
  • Loading branch information
das7pad and AlexVulaj committed Dec 7, 2023
1 parent 48baa28 commit e136241
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var handler http.Handler
if r.Match(req, &match) {
handler = match.Handler
if handler == nil {
// The default handlers do not make use of the context values.
} else if r.omitRouteFromContext {
// Only populate the match vars (if any) into the context.
req = requestWithVars(req, match.Vars)
} else {
req = requestWithRouteAndVars(req, match.Route, match.Vars)
if handler != nil {
// Populate context for custom handlers
if r.omitRouteFromContext {
// Only populate the match vars (if any) into the context.
req = requestWithVars(req, match.Vars)
} else {
req = requestWithRouteAndVars(req, match.Route, match.Vars)
}
}
}

Expand Down Expand Up @@ -263,7 +264,9 @@ func (r *Router) SkipClean(value bool) *Router {
}

// OmitRouteFromContext defines the behavior of omitting the Route from the
// http.Request context.
//
// http.Request context.
//
// CurrentRoute will yield nil with this option.
func (r *Router) OmitRouteFromContext(value bool) *Router {
r.omitRouteFromContext = value
Expand Down Expand Up @@ -467,7 +470,8 @@ func requestWithVars(r *http.Request, vars map[string]string) *http.Request {

// requestWithRouteAndVars adds the matched route and vars to the request ctx.
// It saves extra allocations in cloning the request once and skipping the
// population of empty vars, which in turn mux.Vars can handle gracefully.
//
// population of empty vars, which in turn mux.Vars can handle gracefully.
func requestWithRouteAndVars(r *http.Request, route *Route, vars map[string]string) *http.Request {
ctx := context.WithValue(r.Context(), routeKey, route)
if len(vars) > 0 {
Expand Down

0 comments on commit e136241

Please sign in to comment.