WEBrick's cookie class has a method for parsing Set-Cookie headers, but
that method doesn't support multiple cookies per Set-Cookie header.
This patch gives WEBrick a new method for parsing multiple cookies out
of a header.

--Aaron

--- ruby-1.8.5.old/lib/webrick/cookie.rb	2005-10-30 12:38:42.000000000 -0800
+++ ruby-1.8.5/lib/webrick/cookie.rb	2006-09-05 19:22:07.000000000 -0700
@@ -77,28 +77,36 @@
     end
 
     def self.parse_set_cookie(str)
-      cookie_elem = str.split(/;/)
-      first_elem = cookie_elem.shift
-      first_elem.strip!
-      key, value = first_elem.split(/=/, 2)
-      cookie = new(key, HTTPUtils.dequote(value))
-      cookie_elem.each{|pair|
-        pair.strip!
-        key, value = pair.split(/=/, 2)
-        if value
-          value = HTTPUtils.dequote(value.strip)
-        end
-        case key.downcase
-        when "domain"  then cookie.domain  = value
-        when "path"    then cookie.path    = value
-        when "expires" then cookie.expires = value
-        when "max-age" then cookie.max_age = Integer(value)
-        when "comment" then cookie.comment = value
-        when "version" then cookie.version = Integer(value)
-        when "secure"  then cookie.secure = true
-        end
+      parse_set_cookies(str).first
+    end
+
+    def self.parse_set_cookies(str)
+      cookies = []
+      str.gsub(/(,([^;,]*=)|,$)/) { "\r\n#{$2}" }.split(/\r\n/).each { |c|
+        cookie_elem = c.split(/;/)
+        first_elem = cookie_elem.shift
+        first_elem.strip!
+        key, value = first_elem.split(/=/, 2)
+        cookie = new(key, HTTPUtils.dequote(value))
+        cookie_elem.each{|pair|
+          pair.strip!
+          key, value = pair.split(/=/, 2)
+          if value
+            value = HTTPUtils.dequote(value.strip)
+          end
+          case key.downcase
+          when "domain"  then cookie.domain  = value
+          when "path"    then cookie.path    = value
+          when "expires" then cookie.expires = value
+          when "max-age" then cookie.max_age = Integer(value)
+          when "comment" then cookie.comment = value
+          when "version" then cookie.version = Integer(value)
+          when "secure"  then cookie.secure = true
+          end
+        }
+        cookies << cookie
       }
-      return cookie
+      return cookies
     end
   end
 end
--- ruby-1.8.5.old/test/webrick/test_cookie.rb	2005-10-30 12:38:42.000000000 -0800
+++ ruby-1.8.5/test/webrick/test_cookie.rb	2006-09-05 19:24:04.000000000 -0700
@@ -70,4 +70,25 @@
     assert_equal("/acme", cookie.path)
     assert_equal(true, cookie.secure)
   end
+
+  def test_parse_set_cookies
+    data = %(Shipping="FedEx"; Version="1"; Path="/acme"; Secure, name="Aaron"; Version="1"; path="/acme")
+    cookies = WEBrick::Cookie.parse_set_cookies(data)
+    assert_equal(2, cookies.length)
+
+    fed_ex = cookies.find { |c| c.name == 'Shipping' }
+    assert_not_nil(fed_ex)
+    assert_equal("Shipping", fed_ex.name)
+    assert_equal("FedEx", fed_ex.value)
+    assert_equal(1, fed_ex.version)
+    assert_equal("/acme", fed_ex.path)
+    assert_equal(true, fed_ex.secure)
+
+    name = cookies.find { |c| c.name == 'name' }
+    assert_not_nil(name)
+    assert_equal("name", name.name)
+    assert_equal("Aaron", name.value)
+    assert_equal(1, name.version)
+    assert_equal("/acme", name.path)
+  end
 end