From 229c9ed89658cabd74764b7f5a2509bee33d364c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 24 May 2014 18:53:30 -0700 Subject: Always construct route objects with Constraint objects --- actionpack/lib/action_dispatch/routing/inspector.rb | 6 +++--- actionpack/lib/action_dispatch/routing/mapper.rb | 8 +++----- actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 2135b280da..4be49516bf 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -14,10 +14,10 @@ module ActionDispatch def rack_app(app = self.app) @rack_app ||= begin + app = app.app class_name = app.class.name.to_s - if class_name == "ActionDispatch::Routing::Mapper::Constraints" - app.app - elsif ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/ + + if ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/ app end end diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index b33c5e0dfd..c5e093dae5 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -216,10 +216,12 @@ module ActionDispatch end def app + endpoint = to.respond_to?(:call) ? to : dispatcher + if blocks.any? Constraints.new(endpoint, blocks, @set.request_class) else - endpoint + Constraints.new(endpoint, blocks, @set.request_class) end end @@ -306,10 +308,6 @@ module ActionDispatch Journey::Router::Strexp.compile(path, requirements, SEPARATORS) end - def endpoint - to.respond_to?(:call) ? to : dispatcher - end - def dispatcher Routing::RouteSet::Dispatcher.new(defaults) end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 924455bce2..f36453020f 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -704,7 +704,7 @@ module ActionDispatch old_params = req.path_parameters req.path_parameters = old_params.merge params dispatcher = route.app - if dispatcher.is_a?(Mapper::Constraints) && dispatcher.matches?(env) + if dispatcher.matches?(env) dispatcher = dispatcher.app end -- cgit v1.2.3 From 633589c1405990bde9ebd8cdde096f58c7e376bb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 24 May 2014 19:03:12 -0700 Subject: push is_a?(Dispatcher) check in to one place --- actionpack/lib/action_dispatch/journey/route.rb | 10 +--------- actionpack/lib/action_dispatch/routing/mapper.rb | 7 +++++++ actionpack/lib/action_dispatch/routing/route_set.rb | 8 +++----- 3 files changed, 11 insertions(+), 14 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/journey/route.rb b/actionpack/lib/action_dispatch/journey/route.rb index 1ba91d548e..9f0a3af902 100644 --- a/actionpack/lib/action_dispatch/journey/route.rb +++ b/actionpack/lib/action_dispatch/journey/route.rb @@ -16,14 +16,6 @@ module ActionDispatch @app = app @path = path - # Unwrap any constraints so we can see what's inside for route generation. - # This allows the formatter to skip over any mounted applications or redirects - # that shouldn't be matched when using a url_for without a route name. - if app.is_a?(Routing::Mapper::Constraints) - app = app.app - end - @dispatcher = app.is_a?(Routing::RouteSet::Dispatcher) - @constraints = constraints @defaults = defaults @required_defaults = nil @@ -99,7 +91,7 @@ module ActionDispatch end def dispatcher? - @dispatcher + @app.dispatcher? end def matches?(request) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index c5e093dae5..c7f6ff620e 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -28,9 +28,16 @@ module ActionDispatch app = app.app end + # Unwrap any constraints so we can see what's inside for route generation. + # This allows the formatter to skip over any mounted applications or redirects + # that shouldn't be matched when using a url_for without a route name. + @dispatcher = app.is_a?(Routing::RouteSet::Dispatcher) + @app, @constraints, @request = app, constraints, request end + def dispatcher?; @dispatcher; end + def matches?(env) req = @request.new(env) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index f36453020f..5839db87f9 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -703,12 +703,10 @@ module ActionDispatch end old_params = req.path_parameters req.path_parameters = old_params.merge params - dispatcher = route.app - if dispatcher.matches?(env) - dispatcher = dispatcher.app - end + app = route.app + if app.matches?(env) && app.dispatcher? + dispatcher = app.app - if dispatcher.is_a?(Dispatcher) if dispatcher.controller(params, false) dispatcher.prepare_params!(params) return params -- cgit v1.2.3 From b6ec5e2c14e30bbb8a8dc434da36fc976440f2ca Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 24 May 2014 19:07:26 -0700 Subject: eliminate dispatcher is_a checks --- actionpack/lib/action_dispatch/routing/mapper.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index c7f6ff620e..a6f17b9459 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -18,7 +18,7 @@ module ActionDispatch class Constraints #:nodoc: attr_reader :app, :constraints - def initialize(app, constraints, request) + def initialize(app, constraints, request, dispatcher_p) # Unwrap Constraints objects. I don't actually think it's possible # to pass a Constraints object to this constructor, but there were # multiple places that kept testing children of this object. I @@ -28,10 +28,7 @@ module ActionDispatch app = app.app end - # Unwrap any constraints so we can see what's inside for route generation. - # This allows the formatter to skip over any mounted applications or redirects - # that shouldn't be matched when using a url_for without a route name. - @dispatcher = app.is_a?(Routing::RouteSet::Dispatcher) + @dispatcher = dispatcher_p @app, @constraints, @request = app, constraints, request end @@ -223,12 +220,21 @@ module ActionDispatch end def app - endpoint = to.respond_to?(:call) ? to : dispatcher + # Unwrap any constraints so we can see what's inside for route generation. + # This allows the formatter to skip over any mounted applications or redirects + # that shouldn't be matched when using a url_for without a route name. + if to.respond_to?(:call) + dispatcher_p = false + endpoint = to + else + dispatcher_p = true + endpoint = dispatcher + end if blocks.any? - Constraints.new(endpoint, blocks, @set.request_class) + Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p) else - Constraints.new(endpoint, blocks, @set.request_class) + Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p) end end -- cgit v1.2.3 From 8a51ec01581625876904bb785f66aac9788e043d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 25 May 2014 13:43:14 -0700 Subject: Constraints#app should never return another Constraints object, so switch to if statement --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index a6f17b9459..9656109ed3 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -23,7 +23,7 @@ module ActionDispatch # to pass a Constraints object to this constructor, but there were # multiple places that kept testing children of this object. I # *think* they were just being defensive, but I have no idea. - while app.is_a?(self.class) + if app.is_a?(self.class) constraints += app.constraints app = app.app end -- cgit v1.2.3 From c1bc70e229be479b99bc2100e7a26059a6c107eb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 25 May 2014 14:04:08 -0700 Subject: one fewer is_a check --- actionpack/lib/action_dispatch/routing/inspector.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 4be49516bf..ff8b730249 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -14,11 +14,10 @@ module ActionDispatch def rack_app(app = self.app) @rack_app ||= begin - app = app.app - class_name = app.class.name.to_s + endpoint = app.app - if ActionDispatch::Routing::Redirect === app || class_name !~ /^ActionDispatch::Routing/ - app + if ActionDispatch::Routing::Redirect === endpoint || !app.dispatcher? + endpoint end end end -- cgit v1.2.3 From cff0d15e4b77d4a3f884f6f1c351a2d1660c8ccc Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 25 May 2014 14:06:20 -0700 Subject: nothing is passed to `rack_app` anymore, so rm the params --- actionpack/lib/action_dispatch/routing/inspector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index ff8b730249..273f5b9306 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -12,7 +12,7 @@ module ActionDispatch requirements.except(:controller, :action) end - def rack_app(app = self.app) + def rack_app @rack_app ||= begin endpoint = app.app -- cgit v1.2.3 From 62c013d7b18d726d0ce10c3e7a1208d5e0e7fadf Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 25 May 2014 14:11:34 -0700 Subject: pass a request to `matches?` so we can avoid creating excess requests --- actionpack/lib/action_dispatch/routing/mapper.rb | 11 +++++------ actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 9656109ed3..702d998447 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -35,19 +35,18 @@ module ActionDispatch def dispatcher?; @dispatcher; end - def matches?(env) - req = @request.new(env) - + def matches?(req) @constraints.all? do |constraint| (constraint.respond_to?(:matches?) && constraint.matches?(req)) || (constraint.respond_to?(:call) && constraint.call(*constraint_args(constraint, req))) end - ensure - req.reset_parameters end def call(env) - matches?(env) ? @app.call(env) : [ 404, {'X-Cascade' => 'pass'}, [] ] + req = @request.new(env) + matches?(req) ? @app.call(env) : [ 404, {'X-Cascade' => 'pass'}, [] ] + ensure + req.reset_parameters end private diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 5839db87f9..7a565635f5 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -704,7 +704,7 @@ module ActionDispatch old_params = req.path_parameters req.path_parameters = old_params.merge params app = route.app - if app.matches?(env) && app.dispatcher? + if app.matches?(req) && app.dispatcher? dispatcher = app.app if dispatcher.controller(params, false) -- cgit v1.2.3 From b18f22d15c28fc5fe634928d59148c932bba4696 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 25 May 2014 14:26:48 -0700 Subject: pass the request object to the application --- actionpack/lib/action_dispatch/journey/router.rb | 2 +- actionpack/lib/action_dispatch/routing/mapper.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/journey/router.rb b/actionpack/lib/action_dispatch/journey/router.rb index 2ead6a4eb3..8ec223afc3 100644 --- a/actionpack/lib/action_dispatch/journey/router.rb +++ b/actionpack/lib/action_dispatch/journey/router.rb @@ -39,7 +39,7 @@ module ActionDispatch req.path_parameters = set_params.merge parameters - status, headers, body = route.app.call(req.env) + status, headers, body = route.app.serve(req) if 'pass' == headers['X-Cascade'] req.script_name = script_name diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 702d998447..08a5e7c637 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -42,9 +42,8 @@ module ActionDispatch end end - def call(env) - req = @request.new(env) - matches?(req) ? @app.call(env) : [ 404, {'X-Cascade' => 'pass'}, [] ] + def serve(req) + matches?(req) ? @app.call(req.env) : [ 404, {'X-Cascade' => 'pass'}, [] ] ensure req.reset_parameters end -- cgit v1.2.3 From 605ab030a9a8fd8ce77ee20ab246875f958c551f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 26 May 2014 13:20:43 -0700 Subject: push is_a check up to where the Constraints object is allocated --- actionpack/lib/action_dispatch/routing/inspector.rb | 2 +- actionpack/lib/action_dispatch/routing/mapper.rb | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 273f5b9306..dc03a7370e 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -16,7 +16,7 @@ module ActionDispatch @rack_app ||= begin endpoint = app.app - if ActionDispatch::Routing::Redirect === endpoint || !app.dispatcher? + if app.redirect? || !app.dispatcher? endpoint end end diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 08a5e7c637..86357ded4f 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -18,7 +18,7 @@ module ActionDispatch class Constraints #:nodoc: attr_reader :app, :constraints - def initialize(app, constraints, request, dispatcher_p) + def initialize(app, constraints, request, dispatcher_p, redirect_p) # Unwrap Constraints objects. I don't actually think it's possible # to pass a Constraints object to this constructor, but there were # multiple places that kept testing children of this object. I @@ -28,12 +28,14 @@ module ActionDispatch app = app.app end - @dispatcher = dispatcher_p + @dispatcher = dispatcher_p + @redirect = redirect_p @app, @constraints, @request = app, constraints, request end def dispatcher?; @dispatcher; end + def redirect?; @redirect; end def matches?(req) @constraints.all? do |constraint| @@ -218,21 +220,24 @@ module ActionDispatch end def app + dispatcher_p = false + redirect = false + # Unwrap any constraints so we can see what's inside for route generation. # This allows the formatter to skip over any mounted applications or redirects # that shouldn't be matched when using a url_for without a route name. if to.respond_to?(:call) - dispatcher_p = false endpoint = to + redirect = Redirect === endpoint else dispatcher_p = true endpoint = dispatcher end if blocks.any? - Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p) + Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p, redirect) else - Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p) + Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p, redirect) end end -- cgit v1.2.3 From 8a826f5d6391926d7cdc267cc0069a399b7324d8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 26 May 2014 13:24:03 -0700 Subject: a redirect is not a dispatcher by definition, so eliminate test --- actionpack/lib/action_dispatch/routing/inspector.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index dc03a7370e..122bd5a7ca 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -16,7 +16,7 @@ module ActionDispatch @rack_app ||= begin endpoint = app.app - if app.redirect? || !app.dispatcher? + unless app.dispatcher? endpoint end end -- cgit v1.2.3 From d1012b669e33342156e3109d82e7eec8c3d457a9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 26 May 2014 15:43:14 -0700 Subject: we do not need to cache rack_app --- actionpack/lib/action_dispatch/routing/inspector.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 122bd5a7ca..ea3b2f419d 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -5,7 +5,7 @@ module ActionDispatch module Routing class RouteWrapper < SimpleDelegator def endpoint - rack_app ? rack_app.inspect : "#{controller}##{action}" + app.dispatcher? ? "#{controller}##{action}" : rack_app.inspect end def constraints @@ -13,13 +13,7 @@ module ActionDispatch end def rack_app - @rack_app ||= begin - endpoint = app.app - - unless app.dispatcher? - endpoint - end - end + app.app end def verb @@ -72,7 +66,7 @@ module ActionDispatch end def engine? - rack_app && rack_app.respond_to?(:routes) + rack_app.respond_to?(:routes) end end -- cgit v1.2.3 From 402c2af55053c2f29319091ad21fd6fa6b90ee89 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 May 2014 12:10:24 -0700 Subject: give all endpoints a superclass --- actionpack/lib/action_dispatch/routing/endpoint.rb | 10 ++++++++ actionpack/lib/action_dispatch/routing/mapper.rb | 30 ++++++++++------------ .../lib/action_dispatch/routing/redirection.rb | 9 +++++-- .../lib/action_dispatch/routing/route_set.rb | 13 +++++++--- 4 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 actionpack/lib/action_dispatch/routing/endpoint.rb (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/endpoint.rb b/actionpack/lib/action_dispatch/routing/endpoint.rb new file mode 100644 index 0000000000..88aa13c3e8 --- /dev/null +++ b/actionpack/lib/action_dispatch/routing/endpoint.rb @@ -0,0 +1,10 @@ +module ActionDispatch + module Routing + class Endpoint # :nodoc: + def dispatcher?; false; end + def redirect?; false; end + def matches?(req); true; end + def app; self; end + end + end +end diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 86357ded4f..0c3581db32 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -6,6 +6,7 @@ require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/module/remove_method' require 'active_support/inflector' require 'action_dispatch/routing/redirection' +require 'action_dispatch/routing/endpoint' module ActionDispatch module Routing @@ -15,10 +16,10 @@ module ActionDispatch :controller, :action, :path_names, :constraints, :shallow, :blocks, :defaults, :options] - class Constraints #:nodoc: + class Constraints < Endpoint #:nodoc: attr_reader :app, :constraints - def initialize(app, constraints, request, dispatcher_p, redirect_p) + def initialize(app, constraints, request, dispatcher_p) # Unwrap Constraints objects. I don't actually think it's possible # to pass a Constraints object to this constructor, but there were # multiple places that kept testing children of this object. I @@ -29,13 +30,11 @@ module ActionDispatch end @dispatcher = dispatcher_p - @redirect = redirect_p @app, @constraints, @request = app, constraints, request end def dispatcher?; @dispatcher; end - def redirect?; @redirect; end def matches?(req) @constraints.all? do |constraint| @@ -220,24 +219,21 @@ module ActionDispatch end def app - dispatcher_p = false - redirect = false - # Unwrap any constraints so we can see what's inside for route generation. # This allows the formatter to skip over any mounted applications or redirects # that shouldn't be matched when using a url_for without a route name. if to.respond_to?(:call) - endpoint = to - redirect = Redirect === endpoint - else - dispatcher_p = true - endpoint = dispatcher - end - - if blocks.any? - Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p, redirect) + if Redirect === to + to + else + Constraints.new(to, blocks, @set.request_class, false) + end else - Constraints.new(endpoint, blocks, @set.request_class, dispatcher_p, redirect) + if blocks.any? + Constraints.new(dispatcher, blocks, @set.request_class, true) + else + dispatcher + end end end diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index f8ed0cbe6a..c9639bca84 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -3,10 +3,11 @@ require 'active_support/core_ext/uri' require 'active_support/core_ext/array/extract_options' require 'rack/utils' require 'action_controller/metal/exceptions' +require 'action_dispatch/routing/endpoint' module ActionDispatch module Routing - class Redirect # :nodoc: + class Redirect < Endpoint # :nodoc: attr_reader :status, :block def initialize(status, block) @@ -14,9 +15,13 @@ module ActionDispatch @block = block end + def redirect?; true; end + def call(env) - req = Request.new(env) + serve Request.new env + end + def serve(req) # If any of the path parameters has an invalid encoding then # raise since it's likely to trigger errors further on. req.path_parameters.each do |key, value| diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 7a565635f5..942a32087c 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -8,6 +8,7 @@ require 'active_support/core_ext/module/remove_method' require 'active_support/core_ext/array/extract_options' require 'action_controller/metal/exceptions' require 'action_dispatch/http/request' +require 'action_dispatch/routing/endpoint' module ActionDispatch module Routing @@ -20,14 +21,20 @@ module ActionDispatch PARAMETERS_KEY = 'action_dispatch.request.path_parameters' - class Dispatcher #:nodoc: + class Dispatcher < Routing::Endpoint #:nodoc: def initialize(defaults) @defaults = defaults @controller_class_names = ThreadSafe::Cache.new end + def dispatcher?; true; end + def call(env) - params = env[PARAMETERS_KEY] + serve Request.new env + end + + def serve(req) + params = req.path_parameters # If any of the path parameters has an invalid encoding then # raise since it's likely to trigger errors further on. @@ -46,7 +53,7 @@ module ActionDispatch return [404, {'X-Cascade' => 'pass'}, []] end - dispatch(controller, params[:action], env) + dispatch(controller, params[:action], req.env) end def prepare_params!(params) -- cgit v1.2.3 From 7fe14432d875fdf52fe95044a38f110582227bd3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 May 2014 13:44:58 -0700 Subject: constraints class does not need the request class anymore --- actionpack/lib/action_dispatch/routing/mapper.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 0c3581db32..cabaa26e44 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -19,7 +19,7 @@ module ActionDispatch class Constraints < Endpoint #:nodoc: attr_reader :app, :constraints - def initialize(app, constraints, request, dispatcher_p) + def initialize(app, constraints, dispatcher_p) # Unwrap Constraints objects. I don't actually think it's possible # to pass a Constraints object to this constructor, but there were # multiple places that kept testing children of this object. I @@ -31,7 +31,7 @@ module ActionDispatch @dispatcher = dispatcher_p - @app, @constraints, @request = app, constraints, request + @app, @constraints, = app, constraints end def dispatcher?; @dispatcher; end @@ -226,11 +226,11 @@ module ActionDispatch if Redirect === to to else - Constraints.new(to, blocks, @set.request_class, false) + Constraints.new(to, blocks, false) end else if blocks.any? - Constraints.new(dispatcher, blocks, @set.request_class, true) + Constraints.new(dispatcher, blocks, true) else dispatcher end -- cgit v1.2.3 From 9ad01d0adee69d7613214ad4104d419096877c07 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 May 2014 13:51:58 -0700 Subject: call `serve` with the request on dispatchers --- actionpack/lib/action_dispatch/routing/mapper.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index cabaa26e44..70c3b18e15 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -44,7 +44,13 @@ module ActionDispatch end def serve(req) - matches?(req) ? @app.call(req.env) : [ 404, {'X-Cascade' => 'pass'}, [] ] + return [ 404, {'X-Cascade' => 'pass'}, [] ] unless matches?(req) + + if dispatcher? + @app.serve req + else + @app.call req.env + end ensure req.reset_parameters end @@ -219,15 +225,10 @@ module ActionDispatch end def app - # Unwrap any constraints so we can see what's inside for route generation. - # This allows the formatter to skip over any mounted applications or redirects - # that shouldn't be matched when using a url_for without a route name. + return to if Redirect === to + if to.respond_to?(:call) - if Redirect === to - to - else - Constraints.new(to, blocks, false) - end + Constraints.new(to, blocks, false) else if blocks.any? Constraints.new(dispatcher, blocks, true) -- cgit v1.2.3 From 97a52283f89ae45f967f665e8fc8fd991abb967f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 May 2014 13:54:59 -0700 Subject: dispatcher doesn't need `call` anymore --- actionpack/lib/action_dispatch/routing/route_set.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 942a32087c..64a9d1cecd 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -29,10 +29,6 @@ module ActionDispatch def dispatcher?; true; end - def call(env) - serve Request.new env - end - def serve(req) params = req.path_parameters -- cgit v1.2.3 From 4797c4caca52b415bc2233254e2503a8e3752c15 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 May 2014 14:01:30 -0700 Subject: move path_parameter encoding check to the request object --- actionpack/lib/action_dispatch/http/request.rb | 11 +++++++++++ actionpack/lib/action_dispatch/routing/redirection.rb | 9 +-------- actionpack/lib/action_dispatch/routing/route_set.rb | 11 +---------- 3 files changed, 13 insertions(+), 18 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index cdb3e44b3a..dfe258e463 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -53,6 +53,17 @@ module ActionDispatch @uuid = nil end + def check_path_parameters! + # If any of the path parameters has an invalid encoding then + # raise since it's likely to trigger errors further on. + path_parameters.each do |key, value| + next unless value.respond_to?(:valid_encoding?) + unless value.valid_encoding? + raise ActionController::BadRequest, "Invalid parameter: #{key} => #{value}" + end + end + end + def key?(key) @env.key?(key) end diff --git a/actionpack/lib/action_dispatch/routing/redirection.rb b/actionpack/lib/action_dispatch/routing/redirection.rb index c9639bca84..b1b39a5496 100644 --- a/actionpack/lib/action_dispatch/routing/redirection.rb +++ b/actionpack/lib/action_dispatch/routing/redirection.rb @@ -22,14 +22,7 @@ module ActionDispatch end def serve(req) - # If any of the path parameters has an invalid encoding then - # raise since it's likely to trigger errors further on. - req.path_parameters.each do |key, value| - unless value.valid_encoding? - raise ActionController::BadRequest, "Invalid parameter: #{key} => #{value}" - end - end - + req.check_path_parameters! uri = URI.parse(path(req.path_parameters, req)) unless uri.host diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 64a9d1cecd..8777a9cd71 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -30,18 +30,9 @@ module ActionDispatch def dispatcher?; true; end def serve(req) + req.check_path_parameters! params = req.path_parameters - # If any of the path parameters has an invalid encoding then - # raise since it's likely to trigger errors further on. - params.each do |key, value| - next unless value.respond_to?(:valid_encoding?) - - unless value.valid_encoding? - raise ActionController::BadRequest, "Invalid parameter: #{key} => #{value}" - end - end - prepare_params!(params) # Just raise undefined constant errors if a controller was specified as default. -- cgit v1.2.3 From 406b1b64649f48bdd724826a05c06fb78f5378ea Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 27 May 2014 14:24:30 -0700 Subject: rm reset_parameters because we automatically do it from 9ca4839a --- actionpack/lib/action_dispatch/http/parameters.rb | 4 ---- actionpack/lib/action_dispatch/routing/mapper.rb | 2 -- 2 files changed, 6 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/http/parameters.rb b/actionpack/lib/action_dispatch/http/parameters.rb index 5b22cd1fcd..ff555f93a8 100644 --- a/actionpack/lib/action_dispatch/http/parameters.rb +++ b/actionpack/lib/action_dispatch/http/parameters.rb @@ -43,10 +43,6 @@ module ActionDispatch @env[Routing::RouteSet::PARAMETERS_KEY] ||= {} end - def reset_parameters #:nodoc: - @env.delete("action_dispatch.request.parameters") - end - private # Convert nested Hash to HashWithIndifferentAccess diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 70c3b18e15..84a08770f5 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -51,8 +51,6 @@ module ActionDispatch else @app.call req.env end - ensure - req.reset_parameters end private -- cgit v1.2.3