Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created January 20, 2012 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrkn/1646048 to your computer and use it in GitHub Desktop.
Save mrkn/1646048 to your computer and use it in GitHub Desktop.
diff --git a/lib/cgi/util.rb b/lib/cgi/util.rb
index 2bb3b0d..f08a23c 100644
--- a/lib/cgi/util.rb
+++ b/lib/cgi/util.rb
@@ -4,9 +4,15 @@ class CGI
# url_encoded_string = CGI::escape("'Stop!' said Fred")
# # => "%27Stop%21%27+said+Fred"
def CGI::escape(string)
+ unless string.valid_encoding?
+ encoding = string.encoding
+ string = string.dup.force_encoding('ASCII-8BIT')
+ end
string.gsub(/([^ a-zA-Z0-9_.-]+)/) do
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
- end.tr(' ', '+')
+ end.tr(' ', '+').tap do |result|
+ result.force_encoding(encoding) if encoding
+ end
end
# URL-decode a string with encoding(optional).
diff --git a/test/cgi/test_cgi_util.rb b/test/cgi/test_cgi_util.rb
index 71e8bea..f34703e 100644
--- a/test/cgi/test_cgi_util.rb
+++ b/test/cgi/test_cgi_util.rb
@@ -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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment