aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-24 01:33:18 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-24 01:33:18 +0530
commitefd0bd3b7390ebb8526b981169025f2860f6a113 (patch)
tree1cb3bcd8f9534f64e8a764af1010ce65303b42b0 /actionpack/lib/action_dispatch
parentf4571e3617ccd9cc9e5ee9f7431066bd80395e22 (diff)
parent8ff2fb6f3aa6140f5a8bd018d5919a8a1e707cda (diff)
downloadrails-efd0bd3b7390ebb8526b981169025f2860f6a113.tar.gz
rails-efd0bd3b7390ebb8526b981169025f2860f6a113.tar.bz2
rails-efd0bd3b7390ebb8526b981169025f2860f6a113.zip
Merge remote branch 'mainstream/master'
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r--actionpack/lib/action_dispatch/http/filter_parameters.rb98
-rw-r--r--actionpack/lib/action_dispatch/http/parameters.rb3
-rwxr-xr-xactionpack/lib/action_dispatch/http/request.rb1
-rw-r--r--actionpack/lib/action_dispatch/middleware/notifications.rb24
-rw-r--r--actionpack/lib/action_dispatch/middleware/params_parser.rb6
-rw-r--r--actionpack/lib/action_dispatch/middleware/stack.rb4
-rw-r--r--actionpack/lib/action_dispatch/railtie.rb3
-rw-r--r--actionpack/lib/action_dispatch/railties/subscriber.rb17
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb36
-rw-r--r--actionpack/lib/action_dispatch/routing/route.rb6
-rw-r--r--actionpack/lib/action_dispatch/testing/integration.rb42
11 files changed, 151 insertions, 89 deletions
diff --git a/actionpack/lib/action_dispatch/http/filter_parameters.rb b/actionpack/lib/action_dispatch/http/filter_parameters.rb
new file mode 100644
index 0000000000..1958e1668d
--- /dev/null
+++ b/actionpack/lib/action_dispatch/http/filter_parameters.rb
@@ -0,0 +1,98 @@
+require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/hash/keys'
+
+module ActionDispatch
+ module Http
+ # Allows you to specify sensitive parameters which will be replaced from
+ # the request log by looking in all subhashes of the param hash for keys
+ # to filter. If a block is given, each key and value of the parameter
+ # hash and all subhashes is passed to it, the value or key can be replaced
+ # using String#replace or similar method.
+ #
+ # Examples:
+ #
+ # env["action_dispatch.parameter_filter"] = [:password]
+ # => replaces the value to all keys matching /password/i with "[FILTERED]"
+ #
+ # env["action_dispatch.parameter_filter"] = [:foo, "bar"]
+ # => replaces the value to all keys matching /foo|bar/i with "[FILTERED]"
+ #
+ # env["action_dispatch.parameter_filter"] = lambda do |k,v|
+ # v.reverse! if k =~ /secret/i
+ # end
+ # => reverses the value to all keys matching /secret/i
+ #
+ module FilterParameters
+ extend ActiveSupport::Concern
+
+ # Return a hash of parameters with all sensitive data replaced.
+ def filtered_parameters
+ @filtered_parameters ||= process_parameter_filter(parameters)
+ end
+ alias :fitered_params :filtered_parameters
+
+ # Return a hash of request.env with all sensitive data replaced.
+ def filtered_env
+ filtered_env = @env.dup
+ filtered_env.each do |key, value|
+ if (key =~ /RAW_POST_DATA/i)
+ filtered_env[key] = '[FILTERED]'
+ elsif value.is_a?(Hash)
+ filtered_env[key] = process_parameter_filter(value)
+ end
+ end
+ filtered_env
+ end
+
+ protected
+
+ def compile_parameter_filter #:nodoc:
+ strings, regexps, blocks = [], [], []
+
+ Array(@env["action_dispatch.parameter_filter"]).each do |item|
+ case item
+ when NilClass
+ when Proc
+ blocks << item
+ when Regexp
+ regexps << item
+ else
+ strings << item.to_s
+ end
+ end
+
+ regexps << Regexp.new(strings.join('|'), true) unless strings.empty?
+ [regexps, blocks]
+ end
+
+ def filtering_parameters? #:nodoc:
+ @env["action_dispatch.parameter_filter"].present?
+ end
+
+ def process_parameter_filter(original_params) #:nodoc:
+ return original_params.dup unless filtering_parameters?
+
+ filtered_params = {}
+ regexps, blocks = compile_parameter_filter
+
+ original_params.each do |key, value|
+ if regexps.find { |r| key =~ r }
+ value = '[FILTERED]'
+ elsif value.is_a?(Hash)
+ value = process_parameter_filter(value)
+ elsif value.is_a?(Array)
+ value = value.map { |i| process_parameter_filter(i) }
+ elsif blocks.present?
+ key = key.dup
+ value = value.dup if value.duplicable?
+ blocks.each { |b| b.call(key, value) }
+ end
+
+ filtered_params[key] = value
+ end
+
+ filtered_params
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/http/parameters.rb b/actionpack/lib/action_dispatch/http/parameters.rb
index 97546d5f93..40b40ea94e 100644
--- a/actionpack/lib/action_dispatch/http/parameters.rb
+++ b/actionpack/lib/action_dispatch/http/parameters.rb
@@ -29,9 +29,8 @@ module ActionDispatch
def path_parameters
@env["action_dispatch.request.path_parameters"] ||= {}
end
-
- private
+ private
# Convert nested Hashs to HashWithIndifferentAccess
def normalize_parameters(value)
case value
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb
index 187ce7c15d..7a17023ed2 100755
--- a/actionpack/lib/action_dispatch/http/request.rb
+++ b/actionpack/lib/action_dispatch/http/request.rb
@@ -11,6 +11,7 @@ module ActionDispatch
include ActionDispatch::Http::Cache::Request
include ActionDispatch::Http::MimeNegotiation
include ActionDispatch::Http::Parameters
+ include ActionDispatch::Http::FilterParameters
include ActionDispatch::Http::Upload
include ActionDispatch::Http::URL
diff --git a/actionpack/lib/action_dispatch/middleware/notifications.rb b/actionpack/lib/action_dispatch/middleware/notifications.rb
deleted file mode 100644
index 01d2cbb435..0000000000
--- a/actionpack/lib/action_dispatch/middleware/notifications.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-module ActionDispatch
- # Provide notifications in the middleware stack. Notice that for the before_dispatch
- # and after_dispatch notifications, we just send the original env, so we don't pile
- # up large env hashes in the queue. However, in exception cases, the whole env hash
- # is actually useful, so we send it all.
- class Notifications
- def initialize(app)
- @app = app
- end
-
- def call(stack_env)
- env = stack_env.dup
- ActiveSupport::Notifications.instrument("action_dispatch.before_dispatch", :env => env)
-
- ActiveSupport::Notifications.instrument!("action_dispatch.after_dispatch", :env => env) do
- @app.call(stack_env)
- end
- rescue Exception => exception
- ActiveSupport::Notifications.instrument('action_dispatch.exception',
- :env => stack_env, :exception => exception)
- raise exception
- end
- end
-end \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb
index 534390d4aa..522982e202 100644
--- a/actionpack/lib/action_dispatch/middleware/params_parser.rb
+++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb
@@ -35,14 +35,14 @@ module ActionDispatch
when Proc
strategy.call(request.raw_post)
when :xml_simple, :xml_node
- request.body.size == 0 ? {} : Hash.from_xml(request.body).with_indifferent_access
+ request.body.size == 0 ? {} : Hash.from_xml(request.raw_post).with_indifferent_access
when :yaml
- YAML.load(request.body)
+ YAML.load(request.raw_post)
when :json
if request.body.size == 0
{}
else
- data = ActiveSupport::JSON.decode(request.body)
+ data = ActiveSupport::JSON.decode(request.raw_post)
data = {:_json => data} unless data.is_a?(Hash)
data.with_indifferent_access
end
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index 24be4fee55..0dc1d70e37 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -60,9 +60,7 @@ module ActionDispatch
end
def inspect
- str = klass.to_s
- args.each { |arg| str += ", #{build_args.inspect}" }
- str
+ klass.to_s
end
def build(app)
diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb
index 18978bfb39..e4bd143e78 100644
--- a/actionpack/lib/action_dispatch/railtie.rb
+++ b/actionpack/lib/action_dispatch/railtie.rb
@@ -5,9 +5,6 @@ module ActionDispatch
class Railtie < Rails::Railtie
plugin_name :action_dispatch
- require "action_dispatch/railties/subscriber"
- subscriber ActionDispatch::Railties::Subscriber.new
-
# Prepare dispatcher callbacks and run 'prepare' callbacks
initializer "action_dispatch.prepare_dispatcher" do |app|
# TODO: This used to say unless defined?(Dispatcher). Find out why and fix.
diff --git a/actionpack/lib/action_dispatch/railties/subscriber.rb b/actionpack/lib/action_dispatch/railties/subscriber.rb
deleted file mode 100644
index c08a844c6a..0000000000
--- a/actionpack/lib/action_dispatch/railties/subscriber.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-module ActionDispatch
- module Railties
- class Subscriber < Rails::Subscriber
- def before_dispatch(event)
- request = Request.new(event.payload[:env])
- path = request.request_uri.inspect rescue "unknown"
-
- info "\n\nProcessing #{path} to #{request.formats.join(', ')} " <<
- "(for #{request.remote_ip} at #{event.time.to_s(:db)}) [#{request.method.to_s.upcase}]"
- end
-
- def logger
- ActionController::Base.logger
- end
- end
- end
-end \ No newline at end of file
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 811c287355..fcbb70749f 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -375,6 +375,15 @@ module ActionDispatch
end
end
+ def action_type(action)
+ case action
+ when :index, :create
+ :collection
+ when :show, :update, :destroy
+ :member
+ end
+ end
+
def name
options[:as] || plural
end
@@ -391,6 +400,15 @@ module ActionDispatch
plural
end
+ def name_for_action(action)
+ case action_type(action)
+ when :collection
+ collection_name
+ when :member
+ member_name
+ end
+ end
+
def id_segment
":#{singular}_id"
end
@@ -405,6 +423,13 @@ module ActionDispatch
super
end
+ def action_type(action)
+ case action
+ when :show, :create, :update, :destroy
+ :member
+ end
+ end
+
def name
options[:as] || singular
end
@@ -428,7 +453,7 @@ module ActionDispatch
with_scope_level(:resource, resource) do
yield if block_given?
- get :show, :as => resource.member_name if resource.actions.include?(:show)
+ get :show if resource.actions.include?(:show)
post :create if resource.actions.include?(:create)
put :update if resource.actions.include?(:update)
delete :destroy if resource.actions.include?(:destroy)
@@ -454,14 +479,14 @@ module ActionDispatch
yield if block_given?
with_scope_level(:collection) do
- get :index, :as => resource.collection_name if resource.actions.include?(:index)
+ get :index if resource.actions.include?(:index)
post :create if resource.actions.include?(:create)
get :new, :as => resource.singular if resource.actions.include?(:new)
end
with_scope_level(:member) do
scope(':id') do
- get :show, :as => resource.member_name if resource.actions.include?(:show)
+ get :show if resource.actions.include?(:show)
put :update if resource.actions.include?(:update)
delete :destroy if resource.actions.include?(:destroy)
get :edit, :as => resource.singular if resource.actions.include?(:edit)
@@ -525,7 +550,10 @@ module ActionDispatch
begin
old_path = @scope[:path]
@scope[:path] = "#{@scope[:path]}(.:format)"
- return match(options.reverse_merge(:to => action))
+ return match(options.reverse_merge(
+ :to => action,
+ :as => parent_resource.name_for_action(action)
+ ))
ensure
@scope[:path] = old_path
end
diff --git a/actionpack/lib/action_dispatch/routing/route.rb b/actionpack/lib/action_dispatch/routing/route.rb
index f1431e7a37..e6e44d3546 100644
--- a/actionpack/lib/action_dispatch/routing/route.rb
+++ b/actionpack/lib/action_dispatch/routing/route.rb
@@ -44,6 +44,12 @@ module ActionDispatch
def to_a
[@app, @conditions, @defaults, @name]
end
+
+ def to_s
+ @to_s ||= begin
+ "%-6s %-40s %s" % [(verb || :any).to_s.upcase, path, requirements.inspect]
+ end
+ end
end
end
end
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index 4ec47d146c..2093bb3a0e 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -240,9 +240,9 @@ module ActionDispatch
path = location.query ? "#{location.path}?#{location.query}" : location.path
end
- [ControllerCapture, ActionController::Testing].each do |mod|
- unless ActionController::Base < mod
- ActionController::Base.class_eval { include mod }
+ unless ActionController::Base < ActionController::Testing
+ ActionController::Base.class_eval do
+ include ActionController::Testing
end
end
@@ -259,7 +259,9 @@ module ActionDispatch
"HTTP_HOST" => host,
"REMOTE_ADDR" => remote_addr,
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
- "HTTP_ACCEPT" => accept
+ "HTTP_ACCEPT" => accept,
+
+ "action_dispatch.show_exceptions" => false
}
(rack_environment || {}).each do |key, value|
@@ -267,16 +269,15 @@ module ActionDispatch
end
session = Rack::Test::Session.new(@mock_session)
-
- @controller = ActionController::Base.capture_instantiation do
- session.request(path, env)
- end
+ session.request(path, env)
@request_count += 1
@request = ActionDispatch::Request.new(session.last_request.env)
@response = ActionDispatch::TestResponse.from_response(@mock_session.last_response)
@html_document = nil
+ @controller = session.last_request.env['action_controller.instance']
+
return response.status
end
@@ -294,31 +295,6 @@ module ActionDispatch
end
end
- # A module used to extend ActionController::Base, so that integration tests
- # can capture the controller used to satisfy a request.
- module ControllerCapture #:nodoc:
- extend ActiveSupport::Concern
-
- included do
- alias_method_chain :initialize, :capture
- end
-
- def initialize_with_capture(*args)
- initialize_without_capture
- self.class.last_instantiation ||= self
- end
-
- module ClassMethods #:nodoc:
- mattr_accessor :last_instantiation
-
- def capture_instantiation
- self.last_instantiation = nil
- yield
- return last_instantiation
- end
- end
- end
-
module Runner
def app
@app