上文说到,由url匹配到app是由journey完成的,那么journey是怎么完成的呢?
首先回到 routes.rb里,get,post 方法最终是会调用match方法的,match方法会调用map_match -> decomposed_match -> ....无数层,最终会调用 journey的routes 的 add_route 方法,生成多个的 Journey::Route 的对象。Journey::Router
ActionDispatch::Routing::RouteSet 作为最后一层rack middleware,在call方法中调用 Journey::Router::Utils.normalize_path将url标准化以使rourney::route可以辨别
def self.normalize_path(path)
path ||= ""
encoding = path.encoding
path = "/#{path}".dup
path.squeeze!("/".freeze)
path.sub!(%r{/+\Z}, "".freeze)
path.gsub!(/(%[a-f0-9]{2})/) { $1.upcase }
path = "/".dup if path == "".freeze
path.force_encoding(encoding)
path
end
route在app方法中循环调用route.app 的call方法,,依次找到匹配url的app并调用,之后是Routing::RouteSet::Dispatcher.new调用相对应的controller的dispatch方法
def call env
env['PATH_INFO'] = Utils.normalize_path env['PATH_INFO']
find_routes(env).each do |match, parameters, route|
script_name, path_info, set_params = env.values_at('SCRIPT_NAME',
'PATH_INFO',
@params_key)
unless route.path.anchored
env['SCRIPT_NAME'] = (script_name.to_s + match.to_s).chomp('/')
env['PATH_INFO'] = match.post_match
end
env[@params_key] = (set_params || {}).merge parameters
status, headers, body = route.app.call(env)
if 'pass' == headers['X-Cascade']
env['SCRIPT_NAME'] = script_name
env['PATH_INFO'] = path_info
env[@params_key] = set_params
next
end
return [status, headers, body]
end
return [404, {'X-Cascade' => 'pass'}, ['Not Found']]
end
至此,完成路由
网友评论