aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-12 09:00:48 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-03-12 09:00:48 +0000
commit8212cdf176a5ac5e6693d4a0284437a38d5922b4 (patch)
tree10725287f53fe32bbc290f4dee7b591c258f80da
parentd7cc39658cd7b92b9bac8e5131cae4d79577e82c (diff)
downloadruby-8212cdf176a5ac5e6693d4a0284437a38d5922b4.tar.gz
* lib/uri/common.rb (URI.encode_www_form): new method to
generate URL-encoded form data. [ruby-dev:39246] * lib/uri/common.rb (URI.encode_www_component, URI.decode_www_component): new method for encode/decode a name/value of HTML form. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--lib/uri/common.rb86
-rw-r--r--test/uri/test_common.rb25
3 files changed, 120 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 4004c0c5e4..da6b5c552b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Fri Mar 12 17:14:12 2010 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * lib/uri/common.rb (URI.encode_www_form): new method to
+ generate URL-encoded form data. [ruby-dev:39246]
+
+ * lib/uri/common.rb (URI.encode_www_component,
+ URI.decode_www_component): new method for encode/decode
+ a name/value of HTML form.
+
Fri Mar 12 17:36:35 2010 NARUSE, Yui <naruse@ruby-lang.org>
* lib/webrick/httpservlet/cgihandler.rb
diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index 8958c79047..19ca44d7bc 100644
--- a/lib/uri/common.rb
+++ b/lib/uri/common.rb
@@ -716,6 +716,92 @@ module URI
DEFAULT_PARSER.make_regexp(schemes)
end
+ # :nodoc:
+ TBLENCWWWCOMP_ = {}
+
+ # :nodoc:
+ TBLDECWWWCOMP_ = {}
+
+ # Encode given +str+ to URL-encoded form data.
+ #
+ # This doesn't convert *, -, ., 0-9, A-Z, _, a-z,
+ # does convert SP to +, and convert others to %XX.
+ #
+ # This refers http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
+ #
+ # See URI.decode_www_component(str), URI.encode_www_form(enum)
+ def self.encode_www_component(str)
+ if TBLENCWWWCOMP_.empty?
+ 256.times do |i|
+ case i
+ when 0x20
+ TBLENCWWWCOMP_[' '] = '+'
+ when 0x2A, 0x2D, 0x2E, 0x30..0x39, 0x41..0x5A, 0x5F, 0x61..0x7A
+ else
+ TBLENCWWWCOMP_[i.chr] = '%%%X' % i
+ end
+ end
+ TBLENCWWWCOMP_.freeze
+ end
+ str = str.dup.force_encoding(Encoding::ASCII_8BIT)
+ str.gsub!(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_)
+ str
+ end
+
+ # Decode given +str+ of URL-encoded form data.
+ #
+ # This decods + to SP.
+ #
+ # See URI.encode_www_component(str)
+ def self.decode_www_component(str)
+ if TBLDECWWWCOMP_.empty?
+ 256.times do |i|
+ case i
+ when 0x20
+ TBLDECWWWCOMP_['+'] = ' '
+ else
+ h, l = i>>4, i&15
+ TBLDECWWWCOMP_['%%%X%X' % [h, l]] = i.chr
+ TBLDECWWWCOMP_['%%%x%X' % [h, l]] = i.chr
+ TBLDECWWWCOMP_['%%%X%x' % [h, l]] = i.chr
+ TBLDECWWWCOMP_['%%%x%x' % [h, l]] = i.chr
+ end
+ end
+ TBLDECWWWCOMP_.freeze
+ end
+ str.gsub(/\+|%\h\h/, TBLDECWWWCOMP_)
+ end
+
+ # Generate URL-encoded form data from given +enum+.
+ #
+ # This generates application/x-www-form-urlencoded data defined in HTML5
+ # from given an Enumerable object.
+ #
+ # This internally uses URI.encode_www_component(str).
+ #
+ # This doesn't convert encodings of give items, so convert them before call
+ # this method if you want to send data as other than original encoding or
+ # mixed encoding data.
+ #
+ # This doesn't treat files. When you send a file, use multipart/form-data.
+ #
+ # This refers http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
+ #
+ # See URI.encode_www_component(str)
+ def self.encode_www_form(enum)
+ str = nil
+ enum.each do |k,v|
+ if str
+ str << '&'
+ else
+ str = ''.force_encoding(Encoding::US_ASCII)
+ end
+ str << encode_www_component(k)
+ str << '='
+ str << encode_www_component(v)
+ end
+ str
+ end
end
module Kernel
diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb
index dfc4acd8d3..9a5fd6751f 100644
--- a/test/uri/test_common.rb
+++ b/test/uri/test_common.rb
@@ -49,6 +49,31 @@ class TestCommon < Test::Unit::TestCase
assert_equal(expected, Kernel::URI("http://www.ruby-lang.org/"))
assert_raise(NoMethodError) { Object.new.URI("http://www.ruby-lang.org/") }
end
+
+ def test_encode_www_component
+ assert_equal("+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
+ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E",
+ URI.encode_www_component(" !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~"))
+ end
+
+ def test_decode_www_component
+ assert_equal(" !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~",
+ URI.decode_www_component(
+ "+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \
+ "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E"))
+ end
+
+ def test_encode_www_form
+ assert_equal("a=1", URI.encode_www_form("a" => "1"))
+ assert_equal("a=1", URI.encode_www_form(a: 1))
+ assert_equal("a=1", URI.encode_www_form([["a", "1"]]))
+ assert_equal("a=1", URI.encode_www_form([[:a, 1]]))
+ expected = "a=1&%E3%81%82=%E6%BC%A2"
+ assert_equal(expected, URI.encode_www_form("a" => "1", "\u3042" => "\u6F22"))
+ assert_equal(expected, URI.encode_www_form(a: 1, :"\u3042" => "\u6F22"))
+ assert_equal(expected, URI.encode_www_form([["a", "1"], ["\u3042", "\u6F22"]]))
+ assert_equal(expected, URI.encode_www_form([[:a, 1], [:"\u3042", "\u6F22"]]))
+ end
end