Skip to content

Commit

Permalink
Reduce loops in head route matching
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Apr 9, 2020
1 parent 8544c9c commit d7ac607
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions actionpack/lib/action_dispatch/journey/router.rb
Expand Up @@ -110,12 +110,11 @@ def find_routes(req)
r.path.match?(req.path_info)
}

routes =
if req.head?
match_head_routes(routes, req)
else
match_routes(routes, req)
end
if req.head?
routes = match_head_routes(routes, req)
else
routes.select! { |r| r.matches?(req) }
end

routes.sort_by!(&:precedence)

Expand All @@ -131,24 +130,17 @@ def find_routes(req)
end

def match_head_routes(routes, req)
verb_specific_routes = routes.select(&:requires_matching_verb?)
head_routes = match_routes(verb_specific_routes, req)

if head_routes.empty?
begin
req.request_method = "GET"
match_routes(routes, req)
ensure
req.request_method = "HEAD"
end
else
head_routes
head_routes = routes.select { |r| r.requires_matching_verb? && r.matches?(req) }
return head_routes unless head_routes.empty?

begin
req.request_method = "GET"
routes.select! { |r| r.matches?(req) }
routes
ensure
req.request_method = "HEAD"
end
end

def match_routes(routes, req)
routes.select { |r| r.matches?(req) }
end
end
end
end

0 comments on commit d7ac607

Please sign in to comment.