From 97c0be00456a698bd1cb6bc0a39603e0318df3d4 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Collet Date: Mon, 25 May 2009 22:27:26 +0200 Subject: [PATCH] 6349566: java.net.CookieManager doesn't set default domain Enforce default domain in CookieManager Reviewed-by: michaelm --- jdk/src/share/classes/java/net/CookieManager.java | 8 ++++++++ jdk/test/java/net/CookieHandler/CookieManagerTest.java | 7 ++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/jdk/src/share/classes/java/net/CookieManager.java b/jdk/src/share/classes/java/net/CookieManager.java index 6b7b1165329..32790a8f185 100644 --- a/jdk/src/share/classes/java/net/CookieManager.java +++ b/jdk/src/share/classes/java/net/CookieManager.java @@ -286,6 +286,14 @@ public class CookieManager extends CookieHandler } 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(); diff --git a/jdk/test/java/net/CookieHandler/CookieManagerTest.java b/jdk/test/java/net/CookieHandler/CookieManagerTest.java index 99af6149809..d64936b4fd5 100644 --- a/jdk/test/java/net/CookieHandler/CookieManagerTest.java +++ b/jdk/test/java/net/CookieHandler/CookieManagerTest.java @@ -218,21 +218,22 @@ class CookieHttpTransaction implements HttpCallback { }; // 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" ) };