I was just flipping through this code because I did some changes to
CookieTools. To force a V0 cookie to be deleted properly, the time
should actually be set way in the past rather than the current time to
make it be deleted properly. I did it by checking if maxAge was 0 and
setting the expires time way back if it was rather than converting the
current time to the expiration time.
Paul
[EMAIL PROTECTED] wrote:
>
> costin 00/11/30 22:00:39
>
> Modified: src/facade22/org/apache/tomcat/facade
> HttpServletRequestFacade.java
> HttpServletResponseFacade.java
> src/share/org/apache/tomcat/core Request.java
> src/share/org/apache/tomcat/helper CookieTools.java
> src/share/org/apache/tomcat/modules/session SessionId.java
> src/share/org/apache/tomcat/util/http Cookies.java
> ServerCookie.java
> Added: src/examples/WEB-INF/classes CookieExample1.java
> Log:
> - Start using Cookies and the enhanced ServerCookie.
>
> - CookieTools are no longer needed ( it's all commented out, will be
> removed after I test the new code )
>
> - Added a new example, that supports all V1 attributes ( even if no
> browser except lynx can be use V1 ) - it is needed to verify the
> parsing code works for V1.
>
> - The code now uses RFC2109 ( easy to go forward to 2965 ). Discard is removed,
> since it's in the new spec.
>
> Revision Changes Path
> 1.1
>jakarta-tomcat/src/examples/WEB-INF/classes/CookieExample1.java
>
> Index: CookieExample1.java
> ===================================================================
> /* $Id: CookieExample1.java,v 1.1 2000/12/01 06:00:23 costin Exp $
> *
> */
>
> import java.io.*;
> import java.text.*;
> import java.util.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> /**
> * Example servlet showing request headers
> *
> * @author James Duncan Davidson <[EMAIL PROTECTED]>
> */
>
> public class CookieExample1 extends HttpServlet {
>
> ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
>
> public void doGet(HttpServletRequest request,
> HttpServletResponse response)
> throws IOException, ServletException
> {
> response.setContentType("text/html");
>
> PrintWriter out = response.getWriter();
> out.println("<html>");
> out.println("<body bgcolor=\"white\">");
> out.println("<head>");
>
> String title = rb.getString("cookies.title");
> out.println("<title>" + title + "</title>");
> out.println("</head>");
> out.println("<body>");
>
> // relative links
>
> // XXX
> // making these absolute till we work out the
> // addition of a PathInfo issue
>
> out.println("<a href=\"/examples/servlets/cookies.html\">");
> out.println("<img src=\"/examples/images/code.gif\" height=24 " +
> "width=24 align=right border=0 alt=\"view code\"></a>");
> out.println("<a href=\"/examples/servlets/index.html\">");
> out.println("<img src=\"/examples/images/return.gif\" height=24 " +
> "width=24 align=right border=0 alt=\"return\"></a>");
>
> out.println("<h3>" + title + "</h3>");
>
> Cookie[] cookies = request.getCookies();
> if (cookies.length > 0) {
> out.println(rb.getString("cookies.cookies") + "<br>");
> for (int i = 0; i < cookies.length; i++) {
> Cookie cookie = cookies[i];
> out.print("Cookie Name: " + cookie.getName() + "<br>");
> out.print("Cookie Value: " + cookie.getValue() + "<br>");
> out.println("Cookie Version: " + cookie.getVersion() + "<br>");
> out.println("Cookie Domain: " + cookie.getDomain() + "<br>");
> out.println("Cookie Path: " + cookie.getPath() + "<br>");
> out.println("<br>");
> }
> } else {
> out.println(rb.getString("cookies.no-cookies"));
> }
>
> String cookieName = request.getParameter("cookiename");
> String cookieValue = request.getParameter("cookievalue");
> String path= request.getParameter( "cookiepath" );
> String domain= request.getParameter( "cookiedomain" );
> String secure= request.getParameter( "cookiesecure" );
> String version= request.getParameter( "cookieversion" );
> String comment= request.getParameter( "cookiecomment" );
> String maxage= request.getParameter( "cookiemaxage" );
> if (cookieName != null && !"".equals( cookieName) ) {
> // cookie without value is valid !
> Cookie cookie = new Cookie(cookieName, cookieValue);
> if( ! "".equals( path ))
> cookie.setPath( path );
> if( ! "".equals( domain ))
> cookie.setDomain( domain );
> if( ! "".equals( secure ))
> cookie.setSecure( true );
> if( "1".equals( version ))
> cookie.setVersion(1);
> if( ! "".equals( comment ))
> cookie.setComment( comment );
> if( ! "".equals( maxage )) {
> try {
> Integer max=new Integer( maxage );
> cookie.setMaxAge( max.intValue() );
> } catch(Exception ex ) {
> }
> }
>
> response.addCookie(cookie);
> out.println("<P>");
> out.println(rb.getString("cookies.set") + "<br>");
> out.print(rb.getString("cookies.name") + " " + cookieName +
> "<br>");
> out.print(rb.getString("cookies.value") + " " + cookieValue);
> }
>
> out.println("<P>");
> out.println(rb.getString("cookies.make-cookie") + "<br>");
> out.print("<form action=\"");
> out.println("CookieExample\" method=POST>");
> out.print(rb.getString("cookies.name") + " ");
> out.println("<input type=text length=20 name=cookiename><br>");
> out.print(rb.getString("cookies.value") + " ");
> out.println("<input type=text length=20 name=cookievalue><br>");
> out.print("Path ");
> out.println("<input type=text length=20 name=cookiepath><br>");
> out.print("Domain ");
> out.println("<input type=text length=20 name=cookiedomain><br>");
> out.print("Secure ");
> out.println("<input type=text length=20 name=cookiesecure><br>");
> out.print("Version ");
> out.println("<input type=text length=20 name=cookieversion><br>");
> out.print("Comment ");
> out.println("<input type=text length=20 name=cookiecomment><br>");
> out.print("MaxAge ");
> out.println("<input type=text length=20 name=cookiemaxage><br>");
> out.println("<input type=submit></form>");
>
>
> out.println("</body>");
> out.println("</html>");
> }
>
> public void doPost(HttpServletRequest request,
> HttpServletResponse response)
> throws IOException, ServletException
> {
> doGet(request, response);
> }
>
> }
>
>
>
>
>
> 1.11 +3 -2
>jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletRequestFacade.java
>
> Index: HttpServletRequestFacade.java
> ===================================================================
> RCS file:
>/home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletRequestFacade.java,v
> retrieving revision 1.10
> retrieving revision 1.11
> diff -u -r1.10 -r1.11
> --- HttpServletRequestFacade.java 2000/11/30 22:43:46 1.10
> +++ HttpServletRequestFacade.java 2000/12/01 06:00:24 1.11
> @@ -155,7 +155,8 @@
> }
>
> public Cookie[] getCookies() {
> - int count=request.getCookieCount();
> + Cookies cookies=request.getCookies();
> + int count=cookies.getCookieCount();
> Cookie[] cookieArray = new Cookie[ count ];
>
> // Convert from ServerCookie to Cookie.
> @@ -163,7 +164,7 @@
> // ( if you don't call it no allocation happens for cookies )
> // ( well, it happens, the code to reuse have to be written )
> for (int i = 0; i < count; i ++) {
> - ServerCookie sC=request.getCookie(i);
> + ServerCookie sC=cookies.getCookie(i);
> cookieArray[i] = new CookieFacade(sC);
> }
>
>
>
>
> 1.13 +1 -0
>jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java
>
> Index: HttpServletResponseFacade.java
> ===================================================================
> RCS file:
>/home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletResponseFacade.java,v
> retrieving revision 1.12
> retrieving revision 1.13
> diff -u -r1.12 -r1.13
> --- HttpServletResponseFacade.java 2000/11/30 22:43:46 1.12
> +++ HttpServletResponseFacade.java 2000/12/01 06:00:24 1.13
> @@ -452,6 +452,7 @@
>
> private void cookie2serverCookie( Cookie cookie, ServerCookie sc ) {
> sc.getName().setString( cookie.getName() );
> + sc.setVersion( cookie.getVersion());
> sc.getValue().setString( cookie.getValue() );
> sc.getPath().setString(cookie.getPath());
> sc.getDomain().setString( cookie.getDomain());
>
>
>
> 1.77 +40 -33 jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java
>
> Index: Request.java
> ===================================================================
> RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v
> retrieving revision 1.76
> retrieving revision 1.77
> diff -u -r1.76 -r1.77
> --- Request.java 2000/11/30 22:43:48 1.76
> +++ Request.java 2000/12/01 06:00:32 1.77
> @@ -176,9 +176,10 @@
> protected Handler handler = null;
> Container container;
>
> - ServerCookie scookies[]=new ServerCookie[4];
> + Cookies scookies;
> + // ServerCookie scookies[]=new ServerCookie[4];
> // -1 = cookies not processed yet
> - int cookieCount=-1;
> + // int cookieCount=-1;
>
> // sub-request support
> Request top;
> @@ -193,6 +194,7 @@
>
> public Request() {
> headers = new MimeHeaders();
> + scookies = new Cookies( headers );
> initRequest();
> }
>
> @@ -581,34 +583,38 @@
> }
>
> // -------------------- Cookies --------------------
> -
> - public int getCookieCount() {
> - if( cookieCount == -1 ) {
> - cookieCount=0;
> - // compute cookies
> - CookieTools.processCookies( this );
> - }
> - return cookieCount;
> - }
>
> - public ServerCookie getCookie( int idx ) {
> - if( cookieCount == -1 ) {
> - getCookieCount(); // will also update the cookies
> - }
> - return scookies[idx];
> + public Cookies getCookies() {
> + return scookies;
> }
> +
> +// public int getCookieCount() {
> +// if( cookieCount == -1 ) {
> +// cookieCount=0;
> +// // compute cookies
> +// CookieTools.processCookies( this );
> +// }
> +// return cookieCount;
> +// }
>
> - public void addCookie( ServerCookie c ) {
> - // not really needed - happen in 1 thread
> - synchronized ( this ) {
> - if( cookieCount >= scookies.length ) {
> - ServerCookie scookiesTmp[]=new ServerCookie[2*cookieCount];
> - System.arraycopy( scookies, 0, scookiesTmp, 0, cookieCount);
> - scookies=scookiesTmp;
> - }
> - scookies[cookieCount++]=c;
> - }
> - }
> +// public ServerCookie getCookie( int idx ) {
> +// if( cookieCount == -1 ) {
> +// getCookieCount(); // will also update the cookies
> +// }
> +// return scookies[idx];
> +// }
> +
> +// public void addCookie( ServerCookie c ) {
> +// // not really needed - happen in 1 thread
> +// synchronized ( this ) {
> +// if( cookieCount >= scookies.length ) {
> +// ServerCookie scookiesTmp[]=new ServerCookie[2*cookieCount];
> +// System.arraycopy( scookies, 0, scookiesTmp, 0, cookieCount);
> +// scookies=scookiesTmp;
> +// }
> +// scookies[cookieCount++]=c;
> +// }
> +// }
>
> // -------------------- LookupResult
>
> @@ -858,12 +864,13 @@
> sessionIdSource = null;
> sessionId=null;
>
> - for( int i=0; i< cookieCount; i++ ) {
> - if( scookies[i]!=null )
> - scookies[i].recycle();
> - }
> - cookieCount=-1;
> -
> +// for( int i=0; i< cookieCount; i++ ) {
> +// if( scookies[i]!=null )
> +// scookies[i].recycle();
> +// }
> +// cookieCount=-1;
> + scookies.recycle();
> +
> // counters and notes
> cntr.recycle();
> for( int i=0; i<ContextManager.MAX_NOTES; i++ ) notes[i]=null;
>
>
>
> 1.8 +73 -73
>jakarta-tomcat/src/share/org/apache/tomcat/helper/CookieTools.java
>
> Index: CookieTools.java
> ===================================================================
> RCS file:
>/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/helper/CookieTools.java,v
> retrieving revision 1.7
> retrieving revision 1.8
> diff -u -r1.7 -r1.8
> --- CookieTools.java 2000/11/30 22:43:50 1.7
> +++ CookieTools.java 2000/12/01 06:00:38 1.8
> @@ -75,87 +75,87 @@
> */
> public class CookieTools {
>
> - // -------------------- Cookie parsing tools
> - /** Process all Cookie headers of a request, setting them
> - * in a cookie vector
> - */
> - public static void processCookies( Request request ) {
> - // XXX bug in original RequestImpl - might not work if multiple
> - // cookie headers.
> - //
> - // XXX need to use the cookies hint in RequestAdapter
> - MimeHeaders mh=request.getMimeHeaders();
> - MessageBytes cookieMB= mh.getValue( "cookie" );
> +// // -------------------- Cookie parsing tools
> +// /** Process all Cookie headers of a request, setting them
> +// * in a cookie vector
> +// */
> +// public static void processCookies( Request request ) {
> +// // XXX bug in original RequestImpl - might not work if multiple
> +// // cookie headers.
> +// //
> +// // XXX need to use the cookies hint in RequestAdapter
> +// MimeHeaders mh=request.getMimeHeaders();
> +// MessageBytes cookieMB= mh.getValue( "cookie" );
>
> - if( cookieMB==null ) return;
> - if( cookieMB.isNull() ) return;
> +// if( cookieMB==null ) return;
> +// if( cookieMB.isNull() ) return;
>
> - // XXX XXX XXX TODO TODO TODO
> - // byte b[]=cookieMB.getBytes();
> - // if( b!=null ) {
> - // // this is a byte header
> - // parseCookie( request, b, cookieMB.getLength());
> - // return;
> - // }
> +// // XXX XXX XXX TODO TODO TODO
> +// // byte b[]=cookieMB.getBytes();
> +// // if( b!=null ) {
> +// // // this is a byte header
> +// // parseCookie( request, b, cookieMB.getLength());
> +// // return;
> +// // }
>
> - // normal cookie, with a string value.
> - // This is the original code, un-optimized - it shouldn't
> - // happen in normal case
> - String cookieString=cookieMB.toString();
> - StringTokenizer tok = new StringTokenizer(cookieString,
> - ";", false);
> - while (tok.hasMoreTokens()) {
> - String token = tok.nextToken();
> - int i = token.indexOf("=");
> - if (i > -1) {
> +// // normal cookie, with a string value.
> +// // This is the original code, un-optimized - it shouldn't
> +// // happen in normal case
> +// String cookieString=cookieMB.toString();
> +// StringTokenizer tok = new StringTokenizer(cookieString,
> +// ";", false);
> +// while (tok.hasMoreTokens()) {
> +// String token = tok.nextToken();
> +// int i = token.indexOf("=");
> +// if (i > -1) {
>
> - // XXX
> - // the trims here are a *hack* -- this should
> - // be more properly fixed to be spec compliant
> +// // XXX
> +// // the trims here are a *hack* -- this should
> +// // be more properly fixed to be spec compliant
>
> - String name = token.substring(0, i).trim();
> - String value = token.substring(i+1, token.length()).trim();
> - // RFC 2109 and bug
> - value=stripQuote( value );
> - ServerCookie cookie = new ServerCookie();
> - cookie.getName().setString(name);
> - cookie.getValue().setString(value);
> - request.addCookie( cookie );
> - } else {
> - // we have a bad cookie.... just let it go
> - }
> - }
> - }
> +// String name = token.substring(0, i).trim();
> +// String value = token.substring(i+1, token.length()).trim();
> +// // RFC 2109 and bug
> +// value=stripQuote( value );
> +// ServerCookie cookie = new ServerCookie();
> +// cookie.getName().setString(name);
> +// cookie.getValue().setString(value);
> +// request.addCookie( cookie );
> +// } else {
> +// // we have a bad cookie.... just let it go
> +// }
> +// }
> +// }
>
> - /** Parse a cookie from a byte[]
> - */
> - public void parseCookie( Request req, byte b[], int len ) {
> +// /** Parse a cookie from a byte[]
> +// */
> +// public void parseCookie( Request req, byte b[], int len ) {
>
> - }
> +// }
>
> - /**
> - *
> - * Strips quotes from the start and end of the cookie string
> - * This conforms to RFC 2109
> - *
> - * @param value a <code>String</code> specifying the cookie
> - * value (possibly quoted).
> - *
> - * @see #setValue
> - *
> - */
> - private static String stripQuote( String value )
> - {
> - // log("Strip quote from " + value );
> - if (((value.startsWith("\"")) && (value.endsWith("\""))) ||
> - ((value.startsWith("'") && (value.endsWith("'"))))) {
> - try {
> - return value.substring(1,value.length()-1);
> - } catch (Exception ex) {
> - }
> - }
> - return value;
> - }
> +// /**
> +// *
> +// * Strips quotes from the start and end of the cookie string
> +// * This conforms to RFC 2109
> +// *
> +// * @param value a <code>String</code> specifying the cookie
> +// * value (possibly quoted).
> +// *
> +// * @see #setValue
> +// *
> +// */
> +// private static String stripQuote( String value )
> +// {
> +// // log("Strip quote from " + value );
> +// if (((value.startsWith("\"")) && (value.endsWith("\""))) ||
> +// ((value.startsWith("'") && (value.endsWith("'"))))) {
> +// try {
> +// return value.substring(1,value.length()-1);
> +// } catch (Exception ex) {
> +// }
> +// }
> +// return value;
> +// }
>
>
> // /** Return the header name to set the cookie, based on cookie
>
>
>
> 1.4 +2 -2
>jakarta-tomcat/src/share/org/apache/tomcat/modules/session/SessionId.java
>
> Index: SessionId.java
> ===================================================================
> RCS file:
>/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/session/SessionId.java,v
> retrieving revision 1.3
> retrieving revision 1.4
> diff -u -r1.3 -r1.4
> --- SessionId.java 2000/11/30 22:43:53 1.3
> +++ SessionId.java 2000/12/01 06:00:38 1.4
> @@ -165,12 +165,12 @@
> }
>
>
> - int count=request.getCookieCount();
> + int count=request.getCookies().getCookieCount();
>
> // Give priority to cookies. I don't know if that's part
> // of the spec - XXX
> for( int i=0; i<count; i++ ) {
> - ServerCookie cookie = request.getCookie(i);
> + ServerCookie cookie = request.getCookies().getCookie(i);
>
> if (cookie.getName().equals("JSESSIONID")) {
> sessionId = cookie.getValue().toString();
>
>
>
> 1.3 +13 -3
>jakarta-tomcat/src/share/org/apache/tomcat/util/http/Cookies.java
>
> Index: Cookies.java
> ===================================================================
> RCS file:
>/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/http/Cookies.java,v
> retrieving revision 1.2
> retrieving revision 1.3
> diff -u -r1.2 -r1.3
> --- Cookies.java 2000/11/30 22:43:55 1.2
> +++ Cookies.java 2000/12/01 06:00:38 1.3
> @@ -84,9 +84,12 @@
> MimeHeaders headers;
>
> /**
> - *
> + * Construct a new cookie collection, that will extract
> + * the information from headers.
> + *
> */
> - public Cookies() {
> + public Cookies(MimeHeaders headers) {
> + this.headers=headers;
> }
>
> public void recycle() {
> @@ -155,6 +158,7 @@
> // } else {
> processCookieHeader( cookieValue.toString() );
> // }
> + pos++;// search from the next position
> }
> }
>
> @@ -286,7 +290,7 @@
>
> private void processCookieHeader( String cookieString )
> {
> -
> + if( dbg>0 ) log( "Parsing cookie header " + cookieString );
> // normal cookie, with a string value.
> // This is the original code, un-optimized - it shouldn't
> // happen in normal case
> @@ -310,6 +314,7 @@
>
> cookie.getName().setString(name);
> cookie.getValue().setString(value);
> + if( dbg > 0 ) log( "Add cookie " + name + "=" + value);
> } else {
> // we have a bad cookie.... just let it go
> }
> @@ -341,5 +346,10 @@
> }
>
>
> + // log
> + static final int dbg=1;
> + public void log(String s ) {
> + System.out.println("Cookies: " + s);
> + }
>
> }
>
>
>
> 1.3 +28 -14
>jakarta-tomcat/src/share/org/apache/tomcat/util/http/ServerCookie.java
>
> Index: ServerCookie.java
> ===================================================================
> RCS file:
>/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/http/ServerCookie.java,v
> retrieving revision 1.2
> retrieving revision 1.3
> diff -u -r1.2 -r1.3
> --- ServerCookie.java 2000/11/30 22:43:55 1.2
> +++ ServerCookie.java 2000/12/01 06:00:39 1.3
> @@ -168,6 +168,7 @@
> * if it is not
> */
> public static boolean isToken(String value) {
> + if( value==null) return true;
> int len = value.length();
>
> for (int i = 0; i < len; i++) {
> @@ -209,9 +210,14 @@
> * version
> */
> public static String getCookieHeaderName(int version) {
> + if( dbg>0 ) log( (version==1) ? "Set-Cookie2x" : "Set-Cookie");
> if (version == 1) {
> - // RFC2965
> - return "Set-Cookie2";
> + // RFC2109
> + return "Set-Cookie";
> + // XXX RFC2965 is not standard yet, and Set-Cookie2
> + // is not supported by Netscape 4, 6, IE 3, 5 .
> + // It is supported by Lynx, and there is hope
> + // return "Set-Cookie2";
> } else {
> // Old Netscape
> return "Set-Cookie";
> @@ -224,6 +230,7 @@
> public String getCookieHeaderValue() {
> StringBuffer buf = new StringBuffer();
> getCookieHeaderValue( buf );
> + if( dbg > 0 ) log( buf.toString());
> return buf.toString();
> }
>
> @@ -241,19 +248,19 @@
> // add version 1 specific information
> if (version == 1) {
> // Version=1 ... required
> - buf.append (";Version=1");
> + buf.append ("; Version=1");
>
> // Comment=comment
> - if (cookie.getComment() != null) {
> - buf.append (";Comment=");
> + if (! cookie.getComment().isNull()) {
> + buf.append ("; Comment=");
> maybeQuote (version, buf, cookie.getComment().toString());
> }
> }
>
> // add domain information, if present
>
> - if (cookie.getDomain().isNull()) {
> - buf.append(";Domain=");
> + if (! cookie.getDomain().isNull()) {
> + buf.append("; Domain=");
> maybeQuote (version, buf, cookie.getDomain().toString());
> }
>
> @@ -262,7 +269,7 @@
> if (version == 0) {
> // XXX XXX XXX We need to send both, for
> // interoperatibility (long word )
> - buf.append (";Expires=");
> + buf.append ("; Expires=");
> // Wdy, DD-Mon-YY HH:MM:SS GMT ( Expires netscape format )
> DateTool.oldCookieFormat.
> format(new Date( System.currentTimeMillis() +
> @@ -270,21 +277,22 @@
> new FieldPosition(0));
>
> } else {
> - buf.append (";Max-Age=");
> + buf.append ("; Max-Age=");
> buf.append (cookie.getMaxAge());
> }
> - } else if (version == 1)
> - buf.append (";Discard");
> + }
> +// else if (version == 1)
> +// buf.append ("; Discard");
>
> // Path=path
> - if (cookie.getPath().isNull()) {
> - buf.append (";Path=");
> + if (! cookie.getPath().isNull()) {
> + buf.append ("; Path=");
> maybeQuote (version, buf, cookie.getPath().toString());
> }
>
> // Secure
> if (cookie.getSecure()) {
> - buf.append (";Secure");
> + buf.append ("; Secure");
> }
> }
>
> @@ -298,6 +306,12 @@
> buf.append (value);
> buf.append ('"');
> }
> + }
> +
> + // log
> + static final int dbg=1;
> + public static void log(String s ) {
> + System.out.println("ServerCookie: " + s);
> }
>
> }
>
>
>