Skip to content
Newer
Older
100644 80 lines (70 sloc) 2.47 KB
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
1 require 'active_support/core_ext/hash/keys'
961aa70 @spastorino Some require indifferent_access added
spastorino authored
2 require 'active_support/core_ext/hash/indifferent_access'
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
3
4 module ActionDispatch
5 module Http
6 module Parameters
6a3d469 @tenderlove initialize instance variables
tenderlove authored
7 def initialize(env)
8 super
9 @symbolized_path_params = nil
10 end
11
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
12 # Returns both GET and POST \parameters in a single hash.
13 def parameters
25215d7 @wycats Fix several known web encoding issues:
wycats authored
14 @env["action_dispatch.request.parameters"] ||= begin
bc254cc @sheerun Prevent raising EOFError on multipart GET request.
sheerun authored
15 params = begin
16 request_parameters.merge(query_parameters)
17 rescue EOFError
18 query_parameters.dup
19 end
25215d7 @wycats Fix several known web encoding issues:
wycats authored
20 params.merge!(path_parameters)
b307210 @teohm UTF-8 encode all keys and values in nested params hash.
teohm authored
21 params.with_indifferent_access
25215d7 @wycats Fix several known web encoding issues:
wycats authored
22 end
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
23 end
24 alias :params :parameters
25
26 def path_parameters=(parameters) #:nodoc:
11f6179 @pixeltrix Reset symbolized path parameters when a test request is recycled [#5437
pixeltrix authored
27 @symbolized_path_params = nil
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
28 @env.delete("action_dispatch.request.parameters")
29 @env["action_dispatch.request.path_parameters"] = parameters
30 end
31
32 # The same as <tt>path_parameters</tt> with explicitly symbolized keys.
33 def symbolized_path_parameters
11f6179 @pixeltrix Reset symbolized path parameters when a test request is recycled [#5437
pixeltrix authored
34 @symbolized_path_params ||= path_parameters.symbolize_keys
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
35 end
36
37 # Returns a hash with the \parameters used to form the \path of the request.
38 # Returned hash keys are strings:
39 #
40 # {'action' => 'my_action', 'controller' => 'my_controller'}
41 #
42 # See <tt>symbolized_path_parameters</tt> for symbolized keys.
43 def path_parameters
44 @env["action_dispatch.request.path_parameters"] ||= {}
45 end
46
5603050 @pixeltrix Reset the request parameters after a constraints check
pixeltrix authored
47 def reset_parameters #:nodoc:
48 @env.delete("action_dispatch.request.parameters")
49 end
50
87bcf1a @josh Request#filter_parameters and filter_env
josh authored
51 private
25215d7 @wycats Fix several known web encoding issues:
wycats authored
52
b307210 @teohm UTF-8 encode all keys and values in nested params hash.
teohm authored
53 # Convert nested Hash to HashWithIndifferentAccess
54 # and UTF-8 encode both keys and values in nested Hash.
55 #
25215d7 @wycats Fix several known web encoding issues:
wycats authored
56 # TODO: Validate that the characters are UTF-8. If they aren't,
57 # you'll get a weird error down the road, but our form handling
58 # should really prevent that from happening
b307210 @teohm UTF-8 encode all keys and values in nested params hash.
teohm authored
59 def normalize_encode_params(params)
25215d7 @wycats Fix several known web encoding issues:
wycats authored
60 if params.is_a?(String)
5f30b54 @amatsuda Use Encoding::UTF_8 constant :do_not_litter:
amatsuda authored
61 return params.force_encoding(Encoding::UTF_8).encode!
25215d7 @wycats Fix several known web encoding issues:
wycats authored
62 elsif !params.is_a?(Hash)
63 return params
64 end
65
b307210 @teohm UTF-8 encode all keys and values in nested params hash.
teohm authored
66 new_hash = {}
998d03f @gsamokovarov Refactor ActionDispatch::Http::Parameters#normalize_encode_params
gsamokovarov authored
67 params.each do |key, val|
68 new_key = key.is_a?(String) ? key.dup.force_encoding(Encoding::UTF_8).encode! : key
69 new_hash[new_key] = if val.is_a?(Array)
70 val.map! { |el| normalize_encode_params(el) }
71 else
72 normalize_encode_params(val)
73 end
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
74 end
b307210 @teohm UTF-8 encode all keys and values in nested params hash.
teohm authored
75 new_hash.with_indifferent_access
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored
76 end
77 end
78 end
961aa70 @spastorino Some require indifferent_access added
spastorino authored
79 end
Something went wrong with that request. Please try again.