* lib/cgi/util.rb (CGI.escape): support a string with invalid byte

sequence.

* test/cgi/test_cgi_util.rb
  (test_cgi_escape_with_invalid_byte_sequence): test for the above
  change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34346 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2012-01-20 11:48:52 +00:00
Родитель 587135e994
Коммит ce8d368b1e
3 изменённых файлов: 18 добавлений и 2 удалений

Просмотреть файл

@ -1,3 +1,12 @@
Fri Jan 20 20:47:37 2012 Kenta Murata <mrkn@cookpad.com>
* lib/cgi/util.rb (CGI.escape): support a string with invalid byte
sequence.
* test/cgi/test_cgi_util.rb
(test_cgi_escape_with_invalid_byte_sequence): test for the above
change.
Fri Jan 20 17:37:37 2012 NARUSE, Yui <naruse@ruby-lang.org>
* vm.c (vm_exec): remove workaround for LLVM because r34278 fixes it.

Просмотреть файл

@ -4,9 +4,10 @@ class CGI
# url_encoded_string = CGI::escape("'Stop!' said Fred")
# # => "%27Stop%21%27+said+Fred"
def CGI::escape(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/) do
encoding = string.encoding
string.dup.force_encoding('ASCII-8BIT').gsub(/([^ a-zA-Z0-9_.-]+)/) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
end.tr(' ', '+')
end.tr(' ', '+').force_encoding(encoding)
end
# URL-decode a string with encoding(optional).

Просмотреть файл

@ -24,6 +24,12 @@ class CGIUtilTest < Test::Unit::TestCase
assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'.ascii_only?, CGI::escape(@str1).ascii_only?) if defined?(::Encoding)
end
def test_cgi_escape_with_invalid_byte_sequence
assert_nothing_raised(ArgumentError) do
assert_equal('%C0%3C%3C', CGI::escape("\xC0<<".force_encoding("UTF-8")))
end
end
def test_cgi_unescape
assert_equal(@str1, CGI::unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'))
assert_equal(@str1.encoding, CGI::unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93').encoding) if defined?(::Encoding)