1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #38910 from vinistock/reduce_loops_in_head_route_matching

Reduce loops in head route matching
This commit is contained in:
Ryuta Kamizono 2020-04-10 10:05:00 +09:00 committed by GitHub
commit c1ccc6a0d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -110,11 +110,10 @@ module ActionDispatch
r.path.match?(req.path_info) r.path.match?(req.path_info)
} }
routes =
if req.head? if req.head?
match_head_routes(routes, req) routes = match_head_routes(routes, req)
else else
match_routes(routes, req) routes.select! { |r| r.matches?(req) }
end end
routes.sort_by!(&:precedence) routes.sort_by!(&:precedence)
@ -131,23 +130,16 @@ module ActionDispatch
end end
def match_head_routes(routes, req) def match_head_routes(routes, req)
verb_specific_routes = routes.select(&:requires_matching_verb?) head_routes = routes.select { |r| r.requires_matching_verb? && r.matches?(req) }
head_routes = match_routes(verb_specific_routes, req) return head_routes unless head_routes.empty?
if head_routes.empty?
begin begin
req.request_method = "GET" req.request_method = "GET"
match_routes(routes, req) routes.select! { |r| r.matches?(req) }
routes
ensure ensure
req.request_method = "HEAD" req.request_method = "HEAD"
end end
else
head_routes
end
end
def match_routes(routes, req)
routes.select { |r| r.matches?(req) }
end end
end end
end end