Per Sarah: only strip the hostname if we have reason to believe we have a
hostname (foo.bar.com), prepend a "." to what appears to be a hostless domain (bar.com), and don't call setDomain() at all if we don't have someting that looks like a domain (localhost). git-svn-id: https://samskivert.googlecode.com/svn/trunk@2231 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -47,8 +47,7 @@ public class CookieUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the value of the cookie for the cookie of the specified name,
|
* Get the value of the cookie for the cookie of the specified name, or null if not found.
|
||||||
* or null if not found.
|
|
||||||
*/
|
*/
|
||||||
public static String getCookieValue (HttpServletRequest req, String name)
|
public static String getCookieValue (HttpServletRequest req, String name)
|
||||||
{
|
{
|
||||||
@@ -68,18 +67,24 @@ public class CookieUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the domain of the specified cookie to the server name
|
* Sets the domain of the specified cookie to the server name associated with the supplied
|
||||||
* associated with the supplied request minus the hostname
|
* request minus the hostname (ie. <code>www.samskivert.com</code> becomes
|
||||||
* (ie. <code>www.samskivert.com</code> becomes
|
|
||||||
* <code>.samskivert.com</code>).
|
* <code>.samskivert.com</code>).
|
||||||
*/
|
*/
|
||||||
public static void widenDomain (HttpServletRequest req, Cookie cookie)
|
public static void widenDomain (HttpServletRequest req, Cookie cookie)
|
||||||
{
|
{
|
||||||
String domain = req.getServerName();
|
String server = req.getServerName();
|
||||||
int didx = domain.indexOf(".");
|
int didx = server.indexOf(".");
|
||||||
if (didx != -1) {
|
// if no period was found (e.g. localhost) don't set the domain
|
||||||
domain = domain.substring(didx);
|
if (didx == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// if two or more periods are found (e.g. www.domain.com) strip up to the first one
|
||||||
|
if (server.indexOf(".", didx+1) != -1) {
|
||||||
|
cookie.setDomain(server.substring(didx));
|
||||||
|
} else {
|
||||||
|
// ...otherwise prepend a "." because we're seeing something like "domain.com"
|
||||||
|
cookie.setDomain("." + server);
|
||||||
}
|
}
|
||||||
cookie.setDomain(domain);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user