📄 cookiejar.java
字号:
} } } private char lastCharOf( String string ) { return (string.length() == 0) ? ' ' : string.charAt( string.length()-1 ); } /** * Returns the index (if any) of the equals sign separating a cookie name from the its value. * Equals signs at the end of the token are ignored in this calculation, since they may be * part of a Base64-encoded value. */ private int getEqualsIndex( String token ) { if (!token.endsWith( "==" )) { return token.indexOf( '=' ); } else { return getEqualsIndex( token.substring( 0, token.length()-2 ) ); } } /** * Tokenizes a cookie header and returns the tokens in a * <code>Vector</code>. **/ private Vector getCookieTokens(String cookieHeader) { StringReader sr = new StringReader(cookieHeader); StreamTokenizer st = new StreamTokenizer(sr); Vector tokens = new Vector(); // clear syntax tables of the StreamTokenizer st.resetSyntax(); // set all characters as word characters st.wordChars(0,Character.MAX_VALUE); // set up characters for quoting st.quoteChar( '"' ); //double quotes st.quoteChar( '\'' ); //single quotes // set up characters to separate tokens st.whitespaceChars(59,59); //semicolon st.whitespaceChars(44,44); //comma try { while (st.nextToken() != StreamTokenizer.TT_EOF) { tokens.addElement( st.sval.trim() ); } } catch (IOException ioe) { // this will never happen with a StringReader } sr.close(); return tokens; } abstract protected boolean isCookieAttribute( String stringLowercase ); abstract protected boolean isCookieReservedWord( String token ); } class CookiePress { private StringBuffer _value = new StringBuffer(); private HashMap _attributes = new HashMap(); private URL _sourceURL; public CookiePress( URL sourceURL ) { _sourceURL = sourceURL; } void clear() { _value.setLength(0); _attributes.clear(); } void addToken( String token, char lastChar ) { _value.insert( 0, token ); if (lastChar != '=') _value.insert( 0, ',' ); } void addTokenWithEqualsSign( CookieRecipe recipe, String token, int equalsIndex ) { String name = token.substring( 0, equalsIndex ).trim(); _value.insert( 0, token.substring( equalsIndex + 1 ).trim() ); if (recipe.isCookieAttribute( name.toLowerCase() )) { _attributes.put( name.toLowerCase(), _value.toString() ); } else { addCookieIfValid( new Cookie( name, _value.toString(), _attributes ) ); _attributes.clear(); } _value.setLength(0); } private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); } private boolean acceptCookie( Cookie cookie ) { if (cookie.getPath() == null) { cookie.setPath( getParentPath( _sourceURL.getPath() ) ); } else { int status = getPathAttributeStatus( cookie.getPath(), _sourceURL.getPath() ); if (status != CookieListener.ACCEPTED) { reportCookieRejected( status, cookie.getPath(), cookie.getName() ); return false; } } if (cookie.getDomain() == null) { cookie.setDomain( _sourceURL.getHost() ); } else if (!CookieProperties.isDomainMatchingStrict() && cookie.getDomain().equalsIgnoreCase( _sourceURL.getHost() )) { cookie.setDomain( _sourceURL.getHost() ); } else { int status = getDomainAttributeStatus( cookie.getDomain(), _sourceURL.getHost() ); if (status != CookieListener.ACCEPTED) { reportCookieRejected( status, cookie.getDomain(), cookie.getName() ); return false; } } return true; } private String getParentPath( String path ) { int rightmostSlashIndex = path.lastIndexOf( '/' ); return rightmostSlashIndex < 0 ? "/" : path.substring( 0, rightmostSlashIndex ); } private int getPathAttributeStatus( String pathAttribute, String sourcePath ) { if (!CookieProperties.isPathMatchingStrict() || sourcePath.length() == 0 || sourcePath.startsWith( pathAttribute )) { return CookieListener.ACCEPTED; } else { return CookieListener.PATH_NOT_PREFIX; } } private int getDomainAttributeStatus( String domainAttribute, String sourceHost ) { if (!domainAttribute.startsWith(".")) domainAttribute = '.' + domainAttribute; if (domainAttribute.lastIndexOf('.') == 0) { return CookieListener.DOMAIN_ONE_DOT; } else if (!sourceHost.endsWith( domainAttribute )) { return CookieListener.DOMAIN_NOT_SOURCE_SUFFIX; } else if (CookieProperties.isDomainMatchingStrict() && sourceHost.lastIndexOf( domainAttribute ) > sourceHost.indexOf( '.' )) { return CookieListener.DOMAIN_TOO_MANY_LEVELS; } else { return CookieListener.ACCEPTED; } } private boolean reportCookieRejected( int reason, String attribute, String source ) { CookieProperties.reportCookieRejected( reason, attribute, source ); return false; } } /** * Parses cookies according to * <a href="http://www.ietf.org/rfc/rfc2109.txt">RFC 2109</a> * * <br /> * These cookies come from the <code>Set-Cookie:</code> header **/ class RFC2109CookieRecipe extends CookieRecipe { protected boolean isCookieAttribute( String stringLowercase ) { return stringLowercase.equals("path") || stringLowercase.equals("domain") || stringLowercase.equals("expires") || stringLowercase.equals("comment") || stringLowercase.equals("max-age") || stringLowercase.equals("version"); } protected boolean isCookieReservedWord( String token ) { return token.equalsIgnoreCase( "secure" ); } } /** * Parses cookies according to * <a href="http://www.ietf.org/rfc/rfc2965.txt">RFC 2965</a> * * <br /> * These cookies come from the <code>Set-Cookie2:</code> header **/ class RFC2965CookieRecipe extends CookieRecipe { protected boolean isCookieAttribute( String stringLowercase ) { return stringLowercase.equals("path") || stringLowercase.equals("domain") || stringLowercase.equals("comment") || stringLowercase.equals("commenturl") || stringLowercase.equals("max-age") || stringLowercase.equals("version") || stringLowercase.equals("$version") || stringLowercase.equals("port"); } protected boolean isCookieReservedWord( String token ) { return token.equalsIgnoreCase( "discard" ) || token.equalsIgnoreCase( "secure" ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -