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

Store compiled parameter filters so we don't have to compile them in each request.

This commit is contained in:
José Valim 2010-02-21 14:29:40 +01:00
parent 87a011df6f
commit 55ae903c3f

View file

@ -25,9 +25,16 @@ module ActionDispatch
module FilterParameters module FilterParameters
extend ActiveSupport::Concern extend ActiveSupport::Concern
mattr_reader :compiled_parameter_filter_for
@@compiled_parameter_filter_for = {}
# Return a hash of parameters with all sensitive data replaced. # Return a hash of parameters with all sensitive data replaced.
def filtered_parameters def filtered_parameters
@filtered_parameters ||= process_parameter_filter(parameters) @filtered_parameters ||= if filtering_parameters?
process_parameter_filter(parameters)
else
parameters.dup
end
end end
alias :fitered_params :filtered_parameters alias :fitered_params :filtered_parameters
@ -46,10 +53,18 @@ module ActionDispatch
protected protected
def compile_parameter_filter #:nodoc: def filtering_parameters? #:nodoc:
@env["action_dispatch.parameter_filter"].present?
end
def process_parameter_filter(params) #:nodoc:
compiled_parameter_filter_for(@env["action_dispatch.parameter_filter"]).call(params)
end
def compile_parameter_filter(filters) #:nodoc:
strings, regexps, blocks = [], [], [] strings, regexps, blocks = [], [], []
Array(@env["action_dispatch.parameter_filter"]).each do |item| filters.each do |item|
case item case item
when NilClass when NilClass
when Proc when Proc
@ -65,34 +80,34 @@ module ActionDispatch
[regexps, blocks] [regexps, blocks]
end end
def filtering_parameters? #:nodoc: def compiled_parameter_filter_for(filters) #:nodoc:
@env["action_dispatch.parameter_filter"].present? @@compiled_parameter_filter_for[filters] ||= begin
end regexps, blocks = compile_parameter_filter(filters)
def process_parameter_filter(original_params) #:nodoc: lambda do |original_params|
return original_params.dup unless filtering_parameters? filtered_params = {}
filtered_params = {} original_params.each do |key, value|
regexps, blocks = compile_parameter_filter 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 { |v| v.is_a?(Hash) ? process_parameter_filter(v) : v }
elsif blocks.present?
key = key.dup
value = value.dup if value.duplicable?
blocks.each { |b| b.call(key, value) }
end
original_params.each do |key, value| filtered_params[key] = value
if regexps.find { |r| key =~ r } end
value = '[FILTERED]'
elsif value.is_a?(Hash) filtered_params
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 end
filtered_params[key] = value
end end
filtered_params
end end
end end
end end
end end