From d79c937199cacacbcfdf6b3fd284d9fa20bd2b47 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 18 Dec 2014 11:55:53 -0800 Subject: [PATCH] use the parser type to escape the query string --- lib/uri/generic.rb | 2 +- lib/uri/rfc2396_parser.rb | 6 ++++++ lib/uri/rfc3986_parser.rb | 6 ++++++ test/uri/test_rfc2396.rb | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/uri/test_rfc2396.rb diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index c0b94a8..ae95893 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -851,7 +851,7 @@ module URI v.encode!(Encoding::UTF_8) rescue nil v.delete!("\t\r\n") v.force_encoding(Encoding::ASCII_8BIT) - v.gsub!(/(?!%\h\h|[!$-&(-;=?-Z_a-~])./n.freeze){'%%%02X'.freeze % $&.ord} + v = parser.escape_query v v.force_encoding(Encoding::US_ASCII) @query = v end diff --git a/lib/uri/rfc2396_parser.rb b/lib/uri/rfc2396_parser.rb index c192f65..bab41f6 100644 --- a/lib/uri/rfc2396_parser.rb +++ b/lib/uri/rfc2396_parser.rb @@ -336,6 +336,12 @@ module URI @@to_s.bind(self).call end + # Escapes query strings for RFC2396 uris + def escape_query(v) # :nodoc: + v.gsub!(/(?!%\h\h|[!$-&(-;=?-_a-~])./n){'%%%02X' % $&.ord} + v + end + private # Constructs the default Hash of patterns diff --git a/lib/uri/rfc3986_parser.rb b/lib/uri/rfc3986_parser.rb index 946f374..e94451e 100644 --- a/lib/uri/rfc3986_parser.rb +++ b/lib/uri/rfc3986_parser.rb @@ -92,6 +92,12 @@ module URI } end + # Escapes query strings for RFC2396 uris + def escape_query(v) # :nodoc: + v.gsub!(/(?!%\h\h|[!$-&(-;=?-Z_a-~])./n.freeze){'%%%02X'.freeze % $&.ord} + v + end + private def convert_to_uri(uri) diff --git a/test/uri/test_rfc2396.rb b/test/uri/test_rfc2396.rb new file mode 100644 index 0000000..8e8a7fc --- /dev/null +++ b/test/uri/test_rfc2396.rb @@ -0,0 +1,14 @@ +require 'test/unit' +require 'uri' + +module URI + +class TestRFC2396 < Test::Unit::TestCase + def test_query_on_parse + parser = URI::RFC2396_Parser.new + uri = parser.parse "http://www.example.com/parse?location[]=1" + assert_equal 'location[]=1', uri.query + end +end + +end -- 1.9.3 (Apple Git-50)