Skip to content

Commit

Permalink
Fixed that hashes was not working properly when passed by GET to ligh…
Browse files Browse the repository at this point in the history
…ttpd #849 [Nicholas Seckar]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1677 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jul 4, 2005
1 parent 9e4f5f3 commit 128e352
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Fixed that hashes was not working properly when passed by GET to lighttpd #849 [Nicholas Seckar]

* Fixed assert_template nil will be true when no template was rendered #1565 [maceywj@telus.net] * Fixed assert_template nil will be true when no template was rendered #1565 [maceywj@telus.net]


* Added :prompt option to FormOptions#select (and the users of it, like FormOptions#select_country etc) to create "Please select" style descriptors #1181 [Michael Schuerig] * Added :prompt option to FormOptions#select (and the users of it, like FormOptions#select_country etc) to create "Please select" style descriptors #1181 [Michael Schuerig]
Expand Down
34 changes: 25 additions & 9 deletions actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
Expand Up @@ -17,20 +17,19 @@ def CGIMethods.parse_query_parameters(query_string)
k = CGI.unescape(k) unless k.nil? k = CGI.unescape(k) unless k.nil?
v = CGI.unescape(v) unless v.nil? v = CGI.unescape(v) unless v.nil?


if k =~ /(.*)\[\]$/ keys = split_key(k)
if parsed_params.has_key? $1 last_key = keys.pop
parsed_params[$1] << v last_key = keys.pop if (use_array = last_key.empty?)
else parent = keys.inject(parsed_params) {|h, k| h[k] ||= {}}
parsed_params[$1] = [v]
end if use_array then (parent[last_key] ||= []) << v
else else parent[last_key] = v
parsed_params[k] = v.nil? ? nil : v
end end
} }


return parsed_params return parsed_params
end end

# Returns the request (POST/GET) parameters in a parsed form where pairs such as "customer[address][street]" / # Returns the request (POST/GET) parameters in a parsed form where pairs such as "customer[address][street]" /
# "Somewhere cool!" are translated into a full hash hierarchy, like # "Somewhere cool!" are translated into a full hash hierarchy, like
# { "customer" => { "address" => { "street" => "Somewhere cool!" } } } # { "customer" => { "address" => { "street" => "Somewhere cool!" } } }
Expand Down Expand Up @@ -62,6 +61,23 @@ def self.parse_formatted_request_parameters(format, raw_post_data)
end end


private private

# Splits the given key into several pieces. Example keys are 'name', 'person[name]',
# 'person[name][first]', and 'people[]'. In each instance, an Array instance is returned.
# 'person[name][first]' produces ['person', 'name', 'first']; 'people[]' produces ['people', '']
def CGIMethods.split_key(key)
if /^([^\[]+)((?:\[[^\]]*\])+)$/ =~ key
keys = [$1]

keys.concat($2[1..-2].split(']['))
keys << '' if key[-2..-1] == '[]' # Have to add it since split will drop empty strings

return keys
else
return [key]
end
end

def CGIMethods.get_typed_value(value) def CGIMethods.get_typed_value(value)
if value.respond_to?(:content_type) && !value.content_type.empty? if value.respond_to?(:content_type) && !value.content_type.empty?
# Uploaded file # Uploaded file
Expand Down
15 changes: 12 additions & 3 deletions actionpack/test/controller/cgi_test.rb
Expand Up @@ -32,7 +32,16 @@ def test_query_string
CGIMethods.parse_query_parameters(@query_string) CGIMethods.parse_query_parameters(@query_string)
) )
end end


def test_deep_query_string
assert_equal({'x' => {'y' => {'z' => '10'}}}, CGIMethods.parse_query_parameters('x[y][z]=10'))
end

def test_deep_query_string_with_array
assert_equal({'x' => {'y' => {'z' => ['10']}}}, CGIMethods.parse_query_parameters('x[y][z][]=10'))
assert_equal({'x' => {'y' => {'z' => ['10', '5']}}}, CGIMethods.parse_query_parameters('x[y][z][]=10&x[y][z][]=5'))
end

def test_query_string_with_nil def test_query_string_with_nil
assert_equal( assert_equal(
{ "action" => "create_customer", "full_name" => nil}, { "action" => "create_customer", "full_name" => nil},
Expand All @@ -41,10 +50,10 @@ def test_query_string_with_nil
end end


def test_query_string_with_array def test_query_string_with_array
assert_equal( assert_equal(
{ "action" => "create_customer", "selected" => ["1", "2", "3"]}, { "action" => "create_customer", "selected" => ["1", "2", "3"]},
CGIMethods.parse_query_parameters(@query_string_with_array) CGIMethods.parse_query_parameters(@query_string_with_array)
) )
end end


def test_query_string_with_amps def test_query_string_with_amps
Expand Down

0 comments on commit 128e352

Please sign in to comment.