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

ruby-changes:34460

From: naruse <ko1@a...>
Date: Tue, 24 Jun 2014 11:58:59 +0900 (JST)
Subject: [ruby-changes:34460] naruse:r46541 (trunk): Revert "* lib/uri/mailto.rb: support RFC6068."

naruse	2014-06-24 11:58:45 +0900 (Tue, 24 Jun 2014)

  New Revision: 46541

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

  Log:
    Revert "* lib/uri/mailto.rb: support RFC6068."
    
    to fix mailto URI syntax

  Modified files:
    trunk/ChangeLog
    trunk/lib/uri/mailto.rb
    trunk/test/uri/test_mailto.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46540)
+++ ChangeLog	(revision 46541)
@@ -34,10 +34,6 @@ Tue Jun 24 05:40:41 2014  KOSAKI Motohir https://github.com/ruby/ruby/blob/trunk/ChangeLog#L34
 	  Patch by Shinichiro Hamaji.
 	  [Fixes GH-529] https://github.com/ruby/ruby/pull/529
 
-Tue Jun 24 03:52:35 2014  NARUSE, Yui  <naruse@r...>
-
-	* lib/uri/mailto.rb: support RFC6068.
-
 Mon Jun 23 18:44:45 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* tool/config_files.rb: rename class ConfigFiles to Downloader.
Index: lib/uri/mailto.rb
===================================================================
--- lib/uri/mailto.rb	(revision 46540)
+++ lib/uri/mailto.rb	(revision 46541)
@@ -12,7 +12,7 @@ require 'uri/generic' https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L12
 module URI
 
   #
-  # RFC6068, The mailto URL scheme
+  # RFC2368, The mailto URL scheme
   #
   class MailTo < Generic
     include REGEXP
@@ -37,12 +37,28 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L37
     #
     #  Within mailto URLs, the characters "?", "=", "&" are reserved.
 
-    HEADER_REGEXP  = /\A(?<hfield>(?:%\h\h|[!$'-.0-;@-Z_a-z~])*=(?:%\h\h|[!$'-.0-;@-Z_a-z~])*)(?:&\g<hfield>)*\z/
+    # hname      =  *urlc
+    # hvalue     =  *urlc
+    # header     =  hname "=" hvalue
+    HEADER_PATTERN = "(?:[^?=&]*=[^?=&]*)".freeze
+    HEADER_REGEXP  = Regexp.new(HEADER_PATTERN).freeze
     # headers    =  "?" header *( "&" header )
-    # to         =  addr-spec *("," addr-spec )
-    # mailtoURI  =  "mailto:" [ to ] [ hfields ]
-    TO_REGEXP = /\A(?:[!#-'*+\-\/-9=?A-Z^-~]+(?:\.[!#-'*+\-\/-9=?A-Z^-~]+)*|"(?:\\[!-~]|[!#-\[\]-~])*")@(?:[!#-'*+\-\/-9=?A-Z^-~]+(?:\.[!#-'*+\-\/-9=?A-Z^-~]+)*|\[[!-Z^-~]*\])\z/
-    MAILTO_DATA_REGEXP = /\A(?<to>(?<addr-spec>(?<local-part>[!#-'*+\-\/-9=?A-Z^-~]+(?:\.[!#-'*+\-\/-9=?A-Z^-~]+)*|"(?:\\[!-~]|[!#-\[\]-~])*")@(?<domain>[!#-'*+\-\/-9=?A-Z^-~]+(?:\.[!#-'*+\-\/-9=?A-Z^-~]+)*|\[[!-Z^-~]*\]))(?:,\g<addr-spec>)*)?(?<hfields>\?(?<header>(?<hfield>(?:%\h\h|[!$'-.0-;@-Z_a-z~])*=(?:%\h\h|[!$'-.0-;@-Z_a-z~])*)(?:&\g<hfield>)*))?\z/
+    # to         =  #mailbox
+    # mailtoURL  =  "mailto:" [ to ] [ headers ]
+    MAILBOX_PATTERN = "(?:#{PATTERN::ESCAPED}|[^(),%?=&])".freeze
+    MAILTO_REGEXP = Regexp.new(" # :nodoc:
+      \\A
+      (#{MAILBOX_PATTERN}*?)                          (?# 1: to)
+      (?:
+        \\?
+        (#{HEADER_PATTERN}(?:\\&#{HEADER_PATTERN})*)  (?# 2: headers)
+      )?
+      (?:
+        \\#
+        (#{PATTERN::FRAGMENT})                        (?# 3: fragment)
+      )?
+      \\z
+    ", Regexp::EXTENDED).freeze
     # :startdoc:
 
     #
@@ -83,8 +99,8 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L99
 
       if tmp[:headers]
         tmp[:opaque] << '?'
-        case tmp[:headers]
-        when Array
+
+        if tmp[:headers].kind_of?(Array)
           tmp[:opaque] << tmp[:headers].collect { |x|
             if x.kind_of?(Array)
               x[0] + '=' + x[1..-1].join
@@ -92,10 +108,12 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L108
               x.to_s
             end
           }.join('&')
-        when Hash
+
+        elsif tmp[:headers].kind_of?(Hash)
           tmp[:opaque] << tmp[:headers].collect { |h,v|
             h + '=' + v
           }.join('&')
+
         else
           tmp[:opaque] << tmp[:headers].to_s
         end
@@ -119,13 +137,13 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L137
       @to = nil
       @headers = []
 
-      if m = MAILTO_DATA_REGEXP.match(@opaque)
+      if MAILTO_REGEXP =~ @opaque
         if arg[10] # arg_check
-          self.to = m["to"]
-          self.headers = m["header"]
+          self.to = $1
+          self.headers = $2
         else
-          set_to(m["to"])
-          set_headers(m["header"])
+          set_to($1)
+          set_headers($2)
         end
 
       else
@@ -140,12 +158,14 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L158
     # E-mail headers set by the URL, as an Array of Arrays
     attr_reader :headers
 
-    # check the to +v+ component
+    # check the to +v+ component against either
+    # * URI::Parser Regexp for :OPAQUE
+    # * MAILBOX_PATTERN
     def check_to(v)
       return true unless v
       return true if v.size == 0
 
-      if TO_REGEXP !~ v
+      if parser.regexp[:OPAQUE] !~ v || /\A#{MAILBOX_PATTERN}*\z/o !~ v
         raise InvalidComponentError,
           "bad component(expected opaque component): #{v}"
       end
@@ -168,13 +188,14 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L188
     end
 
     # check the headers +v+ component against either
-    # * URI::Parser Regexp for :QUERY
-    # * HEADER_REGEXP
+    # * URI::Parser Regexp for :OPAQUE
+    # * HEADER_PATTERN
     def check_headers(v)
       return true unless v
       return true if v.size == 0
-      if parser.regexp[:QUERY] !~ v ||
-          HEADER_REGEXP !~ v
+
+      if parser.regexp[:OPAQUE] !~ v ||
+          /\A(#{HEADER_PATTERN}(?:\&#{HEADER_PATTERN})*)\z/o !~ v
         raise InvalidComponentError,
           "bad component(expected opaque component): #{v}"
       end
@@ -187,8 +208,8 @@ module URI https://github.com/ruby/ruby/blob/trunk/lib/uri/mailto.rb#L208
     def set_headers(v)
       @headers = []
       if v
-        v.split('&').each do |x|
-          @headers << x.split(/=/, 2)
+        v.scan(HEADER_REGEXP) do |x|
+          @headers << x.split(/=/o, 2)
         end
       end
     end
Index: test/uri/test_mailto.rb
===================================================================
--- test/uri/test_mailto.rb	(revision 46540)
+++ test/uri/test_mailto.rb	(revision 46541)
@@ -100,6 +100,9 @@ class TestMailTo < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/uri/test_mailto.rb#L100
     # mailto:javascript:alert()
     bad << ["javascript:alert()", []]
 
+    # '=' which is in hname or hvalue is wrong.
+    bad << ["foo@e...?subject=1+1=2", []]
+
     ok.each do |x|
       assert_equal(x[0],
 		   @u.build(x[1]).to_s)
@@ -108,7 +111,7 @@ class TestMailTo < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/uri/test_mailto.rb#L111
     end
 
     bad.each do |x|
-      assert_raise(URI::InvalidComponentError, %[URI::MailTo.build(#{x.inspect})]) {
+      assert_raise(URI::InvalidComponentError) {
 	@u.build(x)
       }
     end

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

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