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

* lib/uri/common.rb (decode_www_form): don't ignore leading '?'.

[ruby-dev:40938]

* lib/uri/common.rb (decode_www_form): check whether argument is
  valid application/x-www-form-urlencoded data.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27270 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2010-04-09 11:58:20 +00:00
parent 1f2def7dd8
commit 8a3c3b9c95
3 changed files with 21 additions and 4 deletions

View file

@ -1,3 +1,11 @@
Fri Apr 9 20:54:10 2010 NARUSE, Yui <naruse@ruby-lang.org>
* lib/uri/common.rb (decode_www_form): don't ignore leading '?'.
[ruby-dev:40938]
* lib/uri/common.rb (decode_www_form): check whether argument is
valid application/x-www-form-urlencoded data.
Fri Apr 9 20:29:13 2010 Yusuke Endoh <mame@tsg.ne.jp> Fri Apr 9 20:29:13 2010 Yusuke Endoh <mame@tsg.ne.jp>
* dir.c (push_glob): clear up the previous commit (RB_GC_GUARD can * dir.c (push_glob): clear up the previous commit (RB_GC_GUARD can

View file

@ -805,6 +805,9 @@ module URI
str str
end end
# :nodoc:
WFKV_ = '(?:%\h\h|[^%#=;&])'
# Decode URL-encoded form data from given +str+. # Decode URL-encoded form data from given +str+.
# #
# This decodes application/x-www-form-urlencoded data # This decodes application/x-www-form-urlencoded data
@ -826,11 +829,11 @@ module URI
# #
# See URI.decode_www_form_component, URI.encode_www_form # See URI.decode_www_form_component, URI.encode_www_form
def self.decode_www_form(str, enc=Encoding::UTF_8) def self.decode_www_form(str, enc=Encoding::UTF_8)
ary = [] unless /\A#{WFKV_}*=#{WFKV_}*(?:[;&]#{WFKV_}*=#{WFKV_}*)*\z/o =~ str
unless /\A\??(?<query>[^=;&]*=[^;&]*(?:[;&][^=;&]*=[^;&]*)*)\z/ =~ str
raise ArgumentError, "invalid data of application/x-www-form-urlencoded (#{str})" raise ArgumentError, "invalid data of application/x-www-form-urlencoded (#{str})"
end end
query.scan(/([^=;&]+)=([^;&]*)/) do ary = []
$&.scan(/([^=;&]+)=([^;&]*)/) do
ary << [decode_www_form_component($1, enc), decode_www_form_component($2, enc)] ary << [decode_www_form_component($1, enc), decode_www_form_component($2, enc)]
end end
ary ary

View file

@ -86,7 +86,13 @@ class TestCommon < Test::Unit::TestCase
def test_decode_www_form def test_decode_www_form
assert_equal([%w[a 1], %w[a 2]], URI.decode_www_form("a=1&a=2")) assert_equal([%w[a 1], %w[a 2]], URI.decode_www_form("a=1&a=2"))
assert_equal([%w[a 1], ["\u3042", "\u6F22"]], assert_equal([%w[a 1], ["\u3042", "\u6F22"]],
URI.decode_www_form("a=1&%E3%81%82=%E6%BC%A2")) URI.decode_www_form("a=1;%E3%81%82=%E6%BC%A2"))
assert_equal([%w[?a 1], %w[a 2]], URI.decode_www_form("?a=1&a=2"))
assert_raise(ArgumentError){URI.decode_www_form("%=1")}
assert_raise(ArgumentError){URI.decode_www_form("a=%")}
assert_raise(ArgumentError){URI.decode_www_form("a=1&%=2")}
assert_raise(ArgumentError){URI.decode_www_form("a=1&b=%")}
assert_raise(ArgumentError){URI.decode_www_form("a&b")}
end end
end end