📄 cookies.java
字号:
}
continue;
}
// we should have "=" ( tested all other alternatives )
int startValue=skipSpaces( bytes, pos, end);
int endValue=startValue;
cc=bytes[pos];
if( 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;
}
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;
}
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;
}
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;
}
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;
}
// 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;
}
// ---------------------------------------------------------
// -------------------- DEPRECATED, OLD --------------------
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
}
}
}
/**
*
* 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;
}
// log
static final int dbg=0;
public void log(String s ) {
if (log.isDebugEnabled())
log.debug("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("$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");
// 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 ));
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -