Return-Path: Delivered-To: apmail-tomcat-dev-archive@www.apache.org Received: (qmail 20830 invoked from network); 19 Jul 2007 02:49:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 19 Jul 2007 02:49:45 -0000 Received: (qmail 64224 invoked by uid 500); 19 Jul 2007 02:49:23 -0000 Delivered-To: apmail-tomcat-dev-archive@tomcat.apache.org Received: (qmail 64009 invoked by uid 500); 19 Jul 2007 02:49:23 -0000 Mailing-List: contact dev-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Developers List" Delivered-To: mailing list dev@tomcat.apache.org Received: (qmail 63998 invoked by uid 500); 19 Jul 2007 02:49:23 -0000 Delivered-To: apmail-jakarta-tomcat-dev@jakarta.apache.org Received: (qmail 63995 invoked by uid 99); 19 Jul 2007 02:49:23 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jul 2007 19:49:23 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Jul 2007 19:49:18 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id E569D1A981A; Wed, 18 Jul 2007 19:48:57 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r557469 - /tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/Cookies.java Date: Thu, 19 Jul 2007 02:48:57 -0000 To: tomcat-dev@jakarta.apache.org From: markt@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070719024857.E569D1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: markt Date: Wed Jul 18 19:48:57 2007 New Revision: 557469 URL: http://svn.apache.org/viewvc?view=rev&rev=557469 Log: Tabs -> 8 spaces No functional change Modified: tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/Cookies.java Modified: tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/Cookies.java URL: http://svn.apache.org/viewvc/tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/Cookies.java?view=diff&rev=557469&r1=557468&r2=557469 ============================================================================== --- tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/Cookies.java (original) +++ tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/Cookies.java Wed Jul 18 19:48:57 2007 @@ -50,7 +50,7 @@ * information from the provided headers. */ public Cookies(MimeHeaders headers) { - this.headers=headers; + this.headers=headers; } /** @@ -80,12 +80,12 @@ * Recycle. */ public void recycle() { - for( int i=0; i< cookieCount; i++ ) { - if( scookies[i]!=null ) - scookies[i].recycle(); - } - cookieCount=0; - unprocessed=true; + for( int i=0; i< cookieCount; i++ ) { + if( scookies[i]!=null ) + scookies[i].recycle(); + } + cookieCount=0; + unprocessed=true; } /** @@ -105,18 +105,18 @@ // -------------------- Indexed access -------------------- public ServerCookie getCookie( int idx ) { - if( unprocessed ) { - getCookieCount(); // will also update the cookies - } - return scookies[idx]; + if( unprocessed ) { + getCookieCount(); // will also update the cookies + } + return scookies[idx]; } public int getCookieCount() { - if( unprocessed ) { - unprocessed=false; - processCookies(headers); - } - return cookieCount; + if( unprocessed ) { + unprocessed=false; + processCookies(headers); + } + return cookieCount; } // -------------------- Adding cookies -------------------- @@ -126,19 +126,19 @@ * The caller can set the name/value and attributes for the cookie */ public ServerCookie addCookie() { - if( cookieCount >= scookies.length ) { - ServerCookie scookiesTmp[]=new ServerCookie[2*cookieCount]; - System.arraycopy( scookies, 0, scookiesTmp, 0, cookieCount); - scookies=scookiesTmp; - } - - ServerCookie c = scookies[cookieCount]; - if( c==null ) { - c= new ServerCookie(); - scookies[cookieCount]=c; - } - cookieCount++; - return c; + if( cookieCount >= scookies.length ) { + ServerCookie scookiesTmp[]=new ServerCookie[2*cookieCount]; + System.arraycopy( scookies, 0, scookiesTmp, 0, cookieCount); + scookies=scookiesTmp; + } + + ServerCookie c = scookies[cookieCount]; + if( c==null ) { + c= new ServerCookie(); + scookies[cookieCount]=c; + } + cookieCount++; + return c; } @@ -147,32 +147,32 @@ /** Add all Cookie found in the headers of a request. */ public void processCookies( MimeHeaders headers ) { - if( headers==null ) - return;// nothing to process - // process each "cookie" header - int pos=0; - while( pos>=0 ) { - // Cookie2: version ? not needed - pos=headers.findHeader( "Cookie", pos ); - // no more cookie headers headers - if( pos<0 ) break; - - MessageBytes cookieValue=headers.getValue( pos ); - if( cookieValue==null || cookieValue.isNull() ) continue; - - // Uncomment to test the new parsing code - if( cookieValue.getType() == MessageBytes.T_BYTES ) { - if( dbg>0 ) log( "Parsing b[]: " + cookieValue.toString()); - ByteChunk bc=cookieValue.getByteChunk(); - processCookieHeader( bc.getBytes(), - bc.getOffset(), - bc.getLength()); - } else { - if( dbg>0 ) log( "Parsing S: " + cookieValue.toString()); - processCookieHeader( cookieValue.toString() ); - } - pos++;// search from the next position - } + if( headers==null ) + return;// nothing to process + // process each "cookie" header + int pos=0; + while( pos>=0 ) { + // Cookie2: version ? not needed + pos=headers.findHeader( "Cookie", pos ); + // no more cookie headers headers + if( pos<0 ) break; + + MessageBytes cookieValue=headers.getValue( pos ); + if( cookieValue==null || cookieValue.isNull() ) continue; + + // Uncomment to test the new parsing code + if( cookieValue.getType() == MessageBytes.T_BYTES ) { + if( dbg>0 ) log( "Parsing b[]: " + cookieValue.toString()); + ByteChunk bc=cookieValue.getByteChunk(); + processCookieHeader( bc.getBytes(), + bc.getOffset(), + bc.getLength()); + } else { + if( dbg>0 ) log( "Parsing S: " + cookieValue.toString()); + processCookieHeader( cookieValue.toString() ); + } + pos++;// search from the next position + } } /** Process a byte[] header - allowing fast processing of the @@ -180,182 +180,182 @@ */ void processCookieHeader( byte bytes[], int off, int len ) { - if( len<=0 || bytes==null ) return; - int end=off+len; - int pos=off; - - int version=0; //sticky - ServerCookie sc=null; - - - while( pos=end ) - return; // only spaces - int startName=pos; - if( dbg>0 ) log( "SN: " + pos ); - - // Version should be the first token - boolean isSpecial=false; - if(bytes[pos]=='$') { pos++; isSpecial=true; } - - pos= findDelim1( bytes, startName, end); // " =;," - int endName=pos; - // current = "=" or " " or DELIM - pos= skipSpaces( bytes, endName, end ); - if( dbg>0 ) log( "DELIM: " + endName + " " + (char)bytes[pos]); - - if(pos >= end ) { - // it's a name-only cookie ( valid in RFC2109 ) - if( ! isSpecial ) { - sc=addCookie(); - sc.getName().setBytes( bytes, startName, - endName-startName ); - sc.getValue().setString(""); - sc.setVersion( version ); - if( dbg>0 ) log( "Name only, end: " + startName + " " + - endName); - } - return; - } - - cc=bytes[pos]; - pos++; - if( cc==';' || cc==',' ) { - if( ! isSpecial && startName!= endName ) { - sc=addCookie(); - sc.getName().setBytes( bytes, startName, - endName-startName ); - sc.getValue().setString(""); - sc.setVersion( version ); - if( dbg>0 ) log( "Name only: " + startName + " " + endName); - } - continue; - } - - // we should have "=" ( tested all other alternatives ) - int startValue=skipSpaces( bytes, pos, end); - int endValue=startValue; - - // quote is valid only in version=1 cookies - cc=bytes[pos]; - if( ( version == 1 || isSpecial ) && ( cc== '\'' || cc=='"' ) ) { - startValue++; - endValue=indexOf( bytes, startValue, end, cc ); - pos=endValue+1; // to skip to next cookie - } else { - endValue=findDelim2( bytes, startValue, end ); - pos=endValue+1; - } - - // if not $Version, etc - if( ! isSpecial ) { - sc=addCookie(); - sc.getName().setBytes( bytes, startName, endName-startName ); - sc.getValue().setBytes( bytes, startValue, endValue-startValue); - sc.setVersion( version ); - if( dbg>0 ) log( "New: " + sc.getName() + "X=X" + sc.getValue()); - continue; - } - - // special - Path, Version, Domain, Port - if( dbg>0 ) log( "Special: " + startName + " " + endName); - // XXX TODO - if( equals( "$Version", bytes, startName, endName ) ) { - if(dbg>0 ) log( "Found version " ); - if( bytes[startValue]=='1' && endValue==startValue+1 ) { - version=1; - if(dbg>0 ) log( "Found version=1" ); - } - continue; - } - if( sc==null ) { - // Path, etc without a previous cookie - continue; - } - if( equals( "$Path", bytes, startName, endName ) ) { - sc.getPath().setBytes( bytes, startValue, endValue-startValue ); - } - if( equals( "$Domain", bytes, startName, endName ) ) { - sc.getDomain().setBytes( bytes, startValue, endValue-startValue ); - } - if( equals( "$Port", bytes, startName, endName ) ) { - // sc.getPort().setBytes( bytes, startValue, endValue-startValue ); - } - } + if( len<=0 || bytes==null ) return; + int end=off+len; + int pos=off; + + int version=0; //sticky + ServerCookie sc=null; + + + while( pos=end ) + return; // only spaces + int startName=pos; + if( dbg>0 ) log( "SN: " + pos ); + + // Version should be the first token + boolean isSpecial=false; + if(bytes[pos]=='$') { pos++; isSpecial=true; } + + pos= findDelim1( bytes, startName, end); // " =;," + int endName=pos; + // current = "=" or " " or DELIM + pos= skipSpaces( bytes, endName, end ); + if( dbg>0 ) log( "DELIM: " + endName + " " + (char)bytes[pos]); + + if(pos >= end ) { + // it's a name-only cookie ( valid in RFC2109 ) + if( ! isSpecial ) { + sc=addCookie(); + sc.getName().setBytes( bytes, startName, + endName-startName ); + sc.getValue().setString(""); + sc.setVersion( version ); + if( dbg>0 ) log( "Name only, end: " + startName + " " + + endName); + } + return; + } + + cc=bytes[pos]; + pos++; + if( cc==';' || cc==',' ) { + if( ! isSpecial && startName!= endName ) { + sc=addCookie(); + sc.getName().setBytes( bytes, startName, + endName-startName ); + sc.getValue().setString(""); + sc.setVersion( version ); + if( dbg>0 ) log( "Name only: " + startName + " " + endName); + } + continue; + } + + // we should have "=" ( tested all other alternatives ) + int startValue=skipSpaces( bytes, pos, end); + int endValue=startValue; + + // quote is valid only in version=1 cookies + cc=bytes[pos]; + if( ( version == 1 || isSpecial ) && ( cc== '\'' || cc=='"' ) ) { + startValue++; + endValue=indexOf( bytes, startValue, end, cc ); + pos=endValue+1; // to skip to next cookie + } else { + endValue=findDelim2( bytes, startValue, end ); + pos=endValue+1; + } + + // if not $Version, etc + if( ! isSpecial ) { + sc=addCookie(); + sc.getName().setBytes( bytes, startName, endName-startName ); + sc.getValue().setBytes( bytes, startValue, endValue-startValue); + sc.setVersion( version ); + if(dbg>0) log("New: " + sc.getName() + "X=X" + sc.getValue()); + continue; + } + + // special - Path, Version, Domain, Port + if( dbg>0 ) log( "Special: " + startName + " " + endName); + // XXX TODO + if( equals( "$Version", bytes, startName, endName ) ) { + if(dbg>0 ) log( "Found version " ); + if( bytes[startValue]=='1' && endValue==startValue+1 ) { + version=1; + if(dbg>0 ) log( "Found version=1" ); + } + continue; + } + if( sc==null ) { + // Path, etc without a previous cookie + continue; + } + if(equals("$Path", bytes, startName, endName)) { + sc.getPath().setBytes(bytes, startValue, endValue-startValue); + } + if(equals("$Domain", bytes, startName, endName)) { + sc.getDomain().setBytes(bytes, startValue, endValue-startValue); + } + if(equals("$Port", bytes, startName, endName)) { + //sc.getPort().setBytes(bytes, startValue, endValue-startValue); + } + } } // -------------------- Utils -------------------- public static int skipSpaces( byte bytes[], int off, int end ) { - while( off < end ) { - byte b=bytes[off]; - if( b!= ' ' ) return off; - off ++; - } - return off; + while( off < end ) { + byte b=bytes[off]; + if( b!= ' ' ) return off; + off ++; + } + return off; } public static int findDelim1( byte bytes[], int off, int end ) { - while( off < end ) { - byte b=bytes[off]; - if( b==' ' || b=='=' || b==';' || b==',' ) - return off; - off++; - } - return off; + while( off < end ) { + byte b=bytes[off]; + if( b==' ' || b=='=' || b==';' || b==',' ) + return off; + off++; + } + return off; } public static int findDelim2( byte bytes[], int off, int end ) { - while( off < end ) { - byte b=bytes[off]; - if( b==';' || b==',' ) - return off; - off++; - } - return off; + while( off < end ) { + byte b=bytes[off]; + if( b==';' || b==',' ) + return off; + off++; + } + return off; } public static int indexOf( byte bytes[], int off, int end, byte qq ) { - while( off < end ) { - byte b=bytes[off]; - if( b==qq ) - return off; - off++; - } - return off; + while( off < end ) { + byte b=bytes[off]; + if( b==qq ) + return off; + off++; + } + return off; } public static int indexOf( byte bytes[], int off, int end, char qq ) { - while( off < end ) { - byte b=bytes[off]; - if( b==qq ) - return off; - off++; - } - return off; + while( off < end ) { + byte b=bytes[off]; + if( b==qq ) + return off; + off++; + } + return off; } // XXX will be refactored soon! public static boolean equals( String s, byte b[], int start, int end) { - int blen = end-start; - if (b == null || blen != s.length()) { - return false; - } - int boff = start; - for (int i = 0; i < blen; i++) { - if (b[boff++] != s.charAt(i)) { - return false; - } - } - return true; + int blen = end-start; + if (b == null || blen != s.length()) { + return false; + } + int boff = start; + for (int i = 0; i < blen; i++) { + if (b[boff++] != s.charAt(i)) { + return false; + } + } + return true; } @@ -364,35 +364,35 @@ 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 - - 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 - - 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 = addCookie(); - - 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 - } - } + 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 + + 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 + + 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 = addCookie(); + + 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 + } + } } /** @@ -408,59 +408,59 @@ */ 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; + // 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; } // log static final int dbg=0; public void log(String s ) { - System.out.println("Cookies: " + s); + System.out.println("Cookies: " + s); } /* public static void main( String args[] ) { - test("foo=bar; a=b"); - test("foo=bar;a=b"); - test("foo=bar;a=b;"); - test("foo=bar;a=b; "); - test("foo=bar;a=b; ;"); - test("foo=;a=b; ;"); - test("foo;a=b; ;"); - // v1 - test("$Version=1; foo=bar;a=b"); + test("foo=bar; a=b"); + test("foo=bar;a=b"); + test("foo=bar;a=b;"); + test("foo=bar;a=b; "); + test("foo=bar;a=b; ;"); + test("foo=;a=b; ;"); + test("foo;a=b; ;"); + // v1 + test("$Version=1; foo=bar;a=b"); test("$Version=\"1\"; foo='bar'; $Path=/path; $Domain=\"localhost\""); - test("$Version=1;foo=bar;a=b; ; "); - test("$Version=1;foo=;a=b; ; "); - test("$Version=1;foo= ;a=b; ; "); - test("$Version=1;foo;a=b; ; "); - test("$Version=1;foo=\"bar\";a=b; ; "); - test("$Version=1;foo=\"bar\";$Path=/examples;a=b; ; "); - test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b"); - test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b;$Domain=yahoo.com"); - // rfc2965 - test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b"); + test("$Version=1;foo=bar;a=b; ; "); + test("$Version=1;foo=;a=b; ; "); + test("$Version=1;foo= ;a=b; ; "); + test("$Version=1;foo;a=b; ; "); + test("$Version=1;foo=\"bar\";a=b; ; "); + test("$Version=1;foo=\"bar\";$Path=/examples;a=b; ; "); + test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b"); + test("$Version=1;foo=\"bar\";$Domain=apache.org;a=b;$Domain=yahoo.com"); + // rfc2965 + test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b"); - // wrong - test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b"); + // wrong + test("$Version=1;foo=\"bar\";$Domain=apache.org;$Port=8080;a=b"); } public static void test( String s ) { - System.out.println("Processing " + s ); - Cookies cs=new Cookies(null); - cs.processCookieHeader( s.getBytes(), 0, s.length()); - for( int i=0; i< cs.getCookieCount() ; i++ ) { - System.out.println("Cookie: " + cs.getCookie( i )); - } - + System.out.println("Processing " + s ); + Cookies cs=new Cookies(null); + cs.processCookieHeader( s.getBytes(), 0, s.length()); + for( int i=0; i< cs.getCookieCount() ; i++ ) { + System.out.println("Cookie: " + cs.getCookie( i )); + } + } */ --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org For additional commands, e-mail: dev-help@tomcat.apache.org