374504: updated cookies for rfc6265

This commit is contained in:
Greg Wilkins 2012-04-12 20:24:50 +10:00
parent 4089f3af77
commit f9a75984f0
6 changed files with 27 additions and 72 deletions

View File

@ -293,7 +293,6 @@ public class HttpFields
/* -------------------------------------------------------------- */
private final ArrayList<Field> _fields = new ArrayList<Field>(20);
private final HashMap<Buffer,Field> _names = new HashMap<Buffer,Field>(32);
private final int _maxCookieVersion;
/* ------------------------------------------------------------ */
/**
@ -301,19 +300,8 @@ public class HttpFields
*/
public HttpFields()
{
_maxCookieVersion=1;
}
/* ------------------------------------------------------------ */
/**
* Constructor.
*/
public HttpFields(int maxCookieVersion)
{
_maxCookieVersion=maxCookieVersion;
}
// TODO externalize this cache so it can be configurable
private static ConcurrentMap<String, Buffer> __cache = new ConcurrentHashMap<String, Buffer>();
private static int __cacheSize = Integer.getInteger("org.eclipse.jetty.http.HttpFields.CACHE",2000);
@ -929,7 +917,7 @@ public class HttpFields
final boolean isHttpOnly,
int version)
{
String delim=_maxCookieVersion==0?"":__COOKIE_DELIM;
String delim=__COOKIE_DELIM;
// Check arguments
if (name == null || name.length() == 0)
@ -938,29 +926,18 @@ public class HttpFields
// Format value and params
StringBuilder buf = new StringBuilder(128);
String name_value_params;
boolean quoted = QuotedStringTokenizer.quoteIfNeeded(buf, name, delim);
QuotedStringTokenizer.quoteIfNeeded(buf, name, delim);
buf.append('=');
String start=buf.toString();
if (value != null && value.length() > 0)
quoted|=QuotedStringTokenizer.quoteIfNeeded(buf, value, delim);
// upgrade to version 1 cookies if quoted.
if (quoted&&version==0 && _maxCookieVersion>=1)
version=1;
QuotedStringTokenizer.quoteIfNeeded(buf, value, delim);
if (version>_maxCookieVersion)
version=_maxCookieVersion;
if (version > 0)
if (comment != null && comment.length() > 0)
{
buf.append(";Version=");
buf.append(version);
if (comment != null && comment.length() > 0)
{
buf.append(";Comment=");
QuotedStringTokenizer.quoteIfNeeded(buf, comment, delim);
}
buf.append(";Comment=");
QuotedStringTokenizer.quoteIfNeeded(buf, comment, delim);
}
if (path != null && path.length() > 0)
{
buf.append(";Path=");
@ -977,23 +954,19 @@ public class HttpFields
if (maxAge >= 0)
{
// Always add the expires param as some browsers still don't handle max-age
buf.append(";Expires=");
if (maxAge == 0)
buf.append(__01Jan1970_COOKIE);
else
formatCookieDate(buf, System.currentTimeMillis() + 1000L * maxAge);
// Always add the expires param as some browsers still don't handle max-age
buf.append(";Expires=");
if (maxAge == 0)
buf.append(__01Jan1970_COOKIE);
else
formatCookieDate(buf, System.currentTimeMillis() + 1000L * maxAge);
if (version >0)
{
buf.append(";Max-Age=");
buf.append(maxAge);
}
}
else if (version > 0)
{
buf.append(";Discard");
}
if (isSecure)
buf.append(";Secure");

View File

@ -361,28 +361,18 @@ public class HttpFieldsTest
fields.clear();
fields.addSetCookie("everything","wrong","wrong","wrong",0,"to be replaced",true,true,0);
fields.addSetCookie("everything","value","domain","path",0,"comment",true,true,0);
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",fields.getStringField("Set-Cookie"));
assertEquals("everything=value;Comment=comment;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",fields.getStringField("Set-Cookie"));
Enumeration<String> e =fields.getValues("Set-Cookie");
assertTrue(e.hasMoreElements());
assertEquals("everything=value;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",e.nextElement());
assertEquals("everything=value;Comment=comment;Path=path;Domain=domain;Expires=Thu, 01-Jan-1970 00:00:00 GMT;Secure;HttpOnly",e.nextElement());
assertFalse(e.hasMoreElements());
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT",fields.getStringField("Expires"));
assertEquals("Thu, 01 Jan 1970 00:00:00 GMT",fields.getStringField("Expires"));
fields.clear();
fields.addSetCookie("ev erything","va lue","do main","pa th",1,"co mment",true,true,2);
String setCookie=fields.getStringField("Set-Cookie");
assertTrue(setCookie.startsWith("\"ev erything\"=\"va lue\";Version=1;Comment=\"co mment\";Path=\"pa th\";Domain=\"do main\";Expires="));
assertTrue(setCookie.startsWith("\"ev erything\"=\"va lue\";Comment=\"co mment\";Path=\"pa th\";Domain=\"do main\";Expires="));
assertTrue(setCookie.endsWith("GMT;Max-Age=1;Secure;HttpOnly"));
fields.clear();
fields.addSetCookie("name","value",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals(-1,setCookie.indexOf("Version="));
fields.clear();
fields.addSetCookie("name","v a l u e",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals(17,setCookie.indexOf("Version=1"));
fields.clear();
fields.addSetCookie("json","{\"services\":[\"cwa\", \"aa\"]}",null,null,-1,null,false,false,-1);
@ -401,12 +391,6 @@ public class HttpFieldsTest
e=fields.getValues("Set-Cookie");
assertEquals("name=more;Domain=domain",e.nextElement());
assertEquals("foo=bob;Domain=domain",e.nextElement());
fields=new HttpFields(0);
fields.addSetCookie("name","value==",null,null,-1,null,false,false,0);
setCookie=fields.getStringField("Set-Cookie");
assertEquals("name=value==",setCookie);
}
private Set<String> enum2set(Enumeration<String> e)

View File

@ -146,7 +146,7 @@ public abstract class AbstractHttpConnection extends AbstractConnection
HttpBuffers ab = (HttpBuffers)_connector;
_parser = newHttpParser(ab.getRequestBuffers(), endpoint, new RequestHandler());
_requestFields = new HttpFields();
_responseFields = new HttpFields(server.getMaxCookieVersion());
_responseFields = new HttpFields();
_request = new Request(this);
_response = new Response(this);
_generator = newHttpGenerator(ab.getResponseBuffers(), endpoint);
@ -164,7 +164,7 @@ public abstract class AbstractHttpConnection extends AbstractConnection
_connector = connector;
_parser = parser;
_requestFields = new HttpFields();
_responseFields = new HttpFields(server.getMaxCookieVersion());
_responseFields = new HttpFields();
_request = request;
_response = new Response(this);
_generator = generator;

View File

@ -136,7 +136,7 @@ public class Response implements HttpServletResponse
if (i>=0)
{
http_only=true;
comment=comment.substring(i,i+HTTP_ONLY_COMMENT.length()).trim();
comment=comment.replace(HTTP_ONLY_COMMENT,"").trim();
if (comment.length()==0)
comment=null;
}

View File

@ -72,7 +72,6 @@ public class Server extends HandlerWrapper implements Attributes
private boolean _sendDateHeader = false; //send Date: header
private int _graceful=0;
private boolean _stopAtShutdown;
private int _maxCookieVersion=1;
private boolean _dumpAfterStart=false;
private boolean _dumpBeforeStop=false;
private boolean _uncheckedPrintWriter=false;
@ -450,21 +449,20 @@ public class Server extends HandlerWrapper implements Attributes
}
/* ------------------------------------------------------------ */
/** Get the maximum cookie version.
* @return the maximum set-cookie version sent by this server
/**
*/
@Deprecated
public int getMaxCookieVersion()
{
return _maxCookieVersion;
return 1;
}
/* ------------------------------------------------------------ */
/** Set the maximum cookie version.
* @param maxCookieVersion the maximum set-cookie version sent by this server
/**
*/
@Deprecated
public void setMaxCookieVersion(int maxCookieVersion)
{
_maxCookieVersion = maxCookieVersion;
}
/* ------------------------------------------------------------ */

View File

@ -516,7 +516,7 @@ public class ResponseTest
String set = response.getHttpFields().getStringField("Set-Cookie");
assertEquals("name=value;Path=/path;Domain=domain;Secure;HttpOnly",set);
assertEquals("name=value;Comment=comment;Path=/path;Domain=domain;Secure;HttpOnly",set);
}
private Response newResponse()