2012-12-19 15:54:47 -05:00
|
|
|
require 'action_dispatch/journey/router/utils'
|
|
|
|
require 'action_dispatch/journey/router/strexp'
|
|
|
|
require 'action_dispatch/journey/routes'
|
|
|
|
require 'action_dispatch/journey/formatter'
|
|
|
|
|
|
|
|
before = $-w
|
|
|
|
$-w = false
|
|
|
|
require 'action_dispatch/journey/parser'
|
|
|
|
$-w = before
|
|
|
|
|
|
|
|
require 'action_dispatch/journey/route'
|
|
|
|
require 'action_dispatch/journey/path/pattern'
|
|
|
|
|
|
|
|
module ActionDispatch
|
2012-12-19 19:24:25 -05:00
|
|
|
module Journey # :nodoc:
|
|
|
|
class Router # :nodoc:
|
|
|
|
class RoutingError < ::StandardError # :nodoc:
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2012-12-19 19:24:25 -05:00
|
|
|
# :nodoc:
|
2012-12-19 15:54:47 -05:00
|
|
|
VERSION = '2.0.0'
|
|
|
|
|
|
|
|
attr_accessor :routes
|
|
|
|
|
2014-05-23 13:57:15 -04:00
|
|
|
def initialize(routes)
|
|
|
|
@routes = routes
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2014-05-23 13:25:26 -04:00
|
|
|
def serve(req)
|
2014-05-23 13:16:03 -04:00
|
|
|
find_routes(req).each do |match, parameters, route|
|
2014-05-23 12:56:33 -04:00
|
|
|
set_params = req.path_parameters
|
|
|
|
path_info = req.path_info
|
|
|
|
script_name = req.script_name
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
unless route.path.anchored
|
2014-05-23 12:56:33 -04:00
|
|
|
req.script_name = (script_name.to_s + match.to_s).chomp('/')
|
|
|
|
req.path_info = match.post_match
|
2014-06-12 02:15:29 -04:00
|
|
|
req.path_info = "/" + req.path_info unless req.path_info.start_with? "/"
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2014-05-22 18:57:24 -04:00
|
|
|
req.path_parameters = set_params.merge parameters
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2014-05-25 17:26:48 -04:00
|
|
|
status, headers, body = route.app.serve(req)
|
2012-12-19 15:54:47 -05:00
|
|
|
|
|
|
|
if 'pass' == headers['X-Cascade']
|
2014-05-23 12:56:33 -04:00
|
|
|
req.script_name = script_name
|
|
|
|
req.path_info = path_info
|
2014-05-22 18:57:24 -04:00
|
|
|
req.path_parameters = set_params
|
2012-12-19 15:54:47 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
return [status, headers, body]
|
|
|
|
end
|
|
|
|
|
|
|
|
return [404, {'X-Cascade' => 'pass'}, ['Not Found']]
|
|
|
|
end
|
|
|
|
|
2014-05-23 13:52:58 -04:00
|
|
|
def recognize(rails_req)
|
2014-05-23 13:16:03 -04:00
|
|
|
find_routes(rails_req).each do |match, parameters, route|
|
2012-12-19 15:54:47 -05:00
|
|
|
unless route.path.anchored
|
2014-05-23 13:17:57 -04:00
|
|
|
rails_req.script_name = match.to_s
|
|
|
|
rails_req.path_info = match.post_match.sub(/^([^\/])/, '/\1')
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
2014-05-21 19:12:49 -04:00
|
|
|
yield(route, parameters)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def visualizer
|
|
|
|
tt = GTG::Builder.new(ast).transition_table
|
|
|
|
groups = partitioned_routes.first.map(&:ast).group_by { |a| a.to_s }
|
|
|
|
asts = groups.values.map { |v| v.first }
|
2012-12-20 15:42:39 -05:00
|
|
|
tt.visualizer(asts)
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def partitioned_routes
|
|
|
|
routes.partitioned_routes
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def ast
|
|
|
|
routes.ast
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def simulator
|
|
|
|
routes.simulator
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def custom_routes
|
|
|
|
partitioned_routes.last
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
def filter_routes(path)
|
|
|
|
return [] unless ast
|
2014-04-01 18:55:48 -04:00
|
|
|
simulator.memos(path) { [] }
|
2012-12-20 15:42:39 -05:00
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2014-05-23 13:16:03 -04:00
|
|
|
def find_routes req
|
2012-12-20 15:42:39 -05:00
|
|
|
routes = filter_routes(req.path_info).concat custom_routes.find_all { |r|
|
|
|
|
r.path.match(req.path_info)
|
|
|
|
}
|
2014-05-25 16:48:30 -04:00
|
|
|
|
2014-07-04 00:32:14 -04:00
|
|
|
routes =
|
|
|
|
if req.request_method == "HEAD"
|
|
|
|
match_head_routes(routes, req)
|
|
|
|
else
|
|
|
|
match_routes(routes, req)
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2014-06-20 10:16:11 -04:00
|
|
|
routes.sort_by!(&:precedence)
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2012-12-20 15:42:39 -05:00
|
|
|
routes.map! { |r|
|
|
|
|
match_data = r.path.match(req.path_info)
|
2014-05-21 19:33:06 -04:00
|
|
|
path_parameters = r.defaults.dup
|
2014-05-21 19:21:33 -04:00
|
|
|
match_data.names.zip(match_data.captures) { |name,val|
|
|
|
|
path_parameters[name.to_sym] = Utils.unescape_uri(val) if val
|
|
|
|
}
|
2014-05-21 19:33:06 -04:00
|
|
|
[match_data, path_parameters, r]
|
2012-12-20 15:42:39 -05:00
|
|
|
}
|
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
|
2014-07-04 00:32:14 -04:00
|
|
|
def match_head_routes(routes, req)
|
|
|
|
head_routes = match_routes(routes, req)
|
|
|
|
|
|
|
|
if head_routes.empty?
|
|
|
|
begin
|
|
|
|
req.request_method = "GET"
|
|
|
|
match_routes(routes, req)
|
|
|
|
ensure
|
|
|
|
req.request_method = "HEAD"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
head_routes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_routes(routes, req)
|
|
|
|
routes.select { |r| r.matches?(req) }
|
2012-12-20 15:42:39 -05:00
|
|
|
end
|
2012-12-19 15:54:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|