summaryrefslogtreecommitdiff
path: root/lib/cgi/util.rb
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-01-20 11:48:52 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-01-20 11:48:52 +0000
commitce8d368b1ea4d6b97b7493074a0c041f59d5a5c1 (patch)
tree43c87ba9600df0542a24defd54484e9708a9bc54 /lib/cgi/util.rb
parent587135e9941306efee72346b0d00c82d8e8574e0 (diff)
* 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
Diffstat (limited to 'lib/cgi/util.rb')
-rw-r--r--lib/cgi/util.rb5
1 files changed, 3 insertions, 2 deletions
diff --git a/lib/cgi/util.rb b/lib/cgi/util.rb
index 2bb3b0da784..b877c1bae74 100644
--- a/lib/cgi/util.rb
+++ b/lib/cgi/util.rb
@@ -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).