[前][次][番号順一覧][スレッド一覧]

ruby-changes:15013

From: naruse <ko1@a...>
Date: Fri, 12 Mar 2010 18:01:04 +0900 (JST)
Subject: [ruby-changes:15013] Ruby:r26888 (trunk): * lib/uri/common.rb (URI.encode_www_form): new method to

naruse	2010-03-12 18:00:48 +0900 (Fri, 12 Mar 2010)

  New Revision: 26888

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26888

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/lib/uri/common.rb
    trunk/test/uri/test_common.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26887)
+++ ChangeLog	(revision 26888)
@@ -1,3 +1,12 @@
+Fri Mar 12 17:14:12 2010  NARUSE, Yui  <naruse@r...>
+
+	* 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@r...>
 
 	* lib/webrick/httpservlet/cgihandler.rb
Index: lib/uri/common.rb
===================================================================
--- lib/uri/common.rb	(revision 26887)
+++ lib/uri/common.rb	(revision 26888)
@@ -716,6 +716,92 @@
     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
Index: test/uri/test_common.rb
===================================================================
--- test/uri/test_common.rb	(revision 26887)
+++ test/uri/test_common.rb	(revision 26888)
@@ -49,6 +49,31 @@
     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
 
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]