6349566: java.net.CookieManager doesn't set default domain
authorjccollet
Mon, 25 May 2009 22:27:26 +0200
changeset 2924 a67f12d92bdd
parent 2923 eca68ab5d025
child 2925 4976d4999a76
6349566: java.net.CookieManager doesn't set default domain Summary: Enforce default domain in CookieManager Reviewed-by: michaelm
jdk/src/share/classes/java/net/CookieManager.java
jdk/test/java/net/CookieHandler/CookieManagerTest.java
--- a/jdk/src/share/classes/java/net/CookieManager.java	Sun May 24 16:35:32 2009 -0700
+++ b/jdk/src/share/classes/java/net/CookieManager.java	Mon May 25 22:27:26 2009 +0200
@@ -286,6 +286,14 @@
                             }
                             cookie.setPath(path);
                         }
+
+                        // As per RFC 2965, section 3.3.1:
+                        // Domain  Defaults to the effective request-host.  (Note that because
+                        // there is no dot at the beginning of effective request-host,
+                        // the default Domain can only domain-match itself.)
+                        if (cookie.getDomain() == null) {
+                            cookie.setDomain(uri.getHost());
+                        }
                         String ports = cookie.getPortlist();
                         if (ports != null) {
                             int port = uri.getPort();
--- a/jdk/test/java/net/CookieHandler/CookieManagerTest.java	Sun May 24 16:35:32 2009 -0700
+++ b/jdk/test/java/net/CookieHandler/CookieManagerTest.java	Mon May 25 22:27:26 2009 +0200
@@ -218,21 +218,22 @@
                 };
 
         // cookies without domain attributes
+        // RFC 2965 states that domain should default to host
         testPolicies[count] = CookiePolicy.ACCEPT_ALL;
         testCases[count++] = new CookieTestCase[]{
                 new CookieTestCase("Set-Cookie2",
                 "Customer=\"WILE_E_COYOTE\"; Version=\"1\"; Path=\"/acme\"",
-                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\"",
+                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
                 "/acme/login"
                 ),
                 new CookieTestCase("Set-Cookie2",
                 "Part_Number=\"Rocket_Launcher_0001\"; Version=\"1\";Path=\"/acme\"",
-                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\"",
+                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
                 "/acme/pickitem"
                 ),
                 new CookieTestCase("Set-Cookie2",
                 "Shipping=\"FedEx\"; Version=\"1\"; Path=\"/acme\"",
-                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\"" + "; Shipping=\"FedEx\";$Path=\"/acme\"",
+                "$Version=\"1\"; Customer=\"WILE_E_COYOTE\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Part_Number=\"Rocket_Launcher_0001\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"" + "; Shipping=\"FedEx\";$Path=\"/acme\";$Domain=\""+localHostAddr+"\"",
                 "/acme/shipping"
                 )
                 };