/** * add from a token * @param recipe - the recipe to use * @param token - the token to use * @param equalsIndex - the position of the equal sign */ void addTokenWithEqualsSign( CookieRecipe recipe, String token, int equalsIndex ) { String name = token.substring( 0, equalsIndex ).trim(); String value= token.substring( equalsIndex + 1 ).trim(); _value.insert( 0, value ); if (recipe.isCookieAttribute( name.toLowerCase() )) { _attributes.put( name.toLowerCase(), _value.toString() ); } else { addCookieIfValid( new Cookie( name, _value.toString(), _attributes ) ); _attributes.clear(); } _value.setLength(0); }
/** * Extracts cookies from a cookie header. Works in conjunction with a cookie press class, which actually creates * the cookies and adds them to the jar as appropriate. * * 1. Parse the header into tokens, separated by ',' and ';' (respecting single and double quotes) * 2. Process tokens from the end: * a. if the token contains an '=' we have a name/value pair. Add them to the cookie press, which * will decide if it is a cookie name or an attribute name. * b. if the token is a reserved word, flush the cookie press and continue. * c. otherwise, add the token to the cookie press, passing along the last character of the previous token. */ void findCookies( String cookieHeader ) { Vector tokens = getCookieTokens( cookieHeader ); for (int i = tokens.size() - 1; i >= 0; i--) { String token = (String) tokens.elementAt( i ); int equalsIndex = getEqualsIndex( token ); if (equalsIndex != -1) { _press.addTokenWithEqualsSign( this, token, equalsIndex ); } else if (isCookieReservedWord( token )) { _press.clear(); } else { _press.addToken( token, lastCharOf( (i == 0) ? "" : (String) tokens.elementAt( i - 1 ) ) ); } } }
/** * accept the given cookie * @param cookie * @return */ 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; }
/** * accept the given cookie * @param cookie * @return */ 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; }
/** * accept the given cookie * @param cookie * @return */ 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; }
/** * Extracts cookies from a cookie header. Works in conjunction with a cookie press class, which actually creates * the cookies and adds them to the jar as appropriate. * * 1. Parse the header into tokens, separated by ',' and ';' (respecting single and double quotes) * 2. Process tokens from the end: * a. if the token contains an '=' we have a name/value pair. Add them to the cookie press, which * will decide if it is a cookie name or an attribute name. * b. if the token is a reserved word, flush the cookie press and continue. * c. otherwise, add the token to the cookie press, passing along the last character of the previous token. */ void findCookies( String cookieHeader ) { Vector tokens = getCookieTokens( cookieHeader ); for (int i = tokens.size() - 1; i >= 0; i--) { String token = (String) tokens.elementAt( i ); int equalsIndex = getEqualsIndex( token ); if (equalsIndex != -1) { _press.addTokenWithEqualsSign( this, token, equalsIndex ); } else if (isCookieReservedWord( token )) { _press.clear(); } else { _press.addToken( token, lastCharOf( (i == 0) ? "" : (String) tokens.elementAt( i - 1 ) ) ); } } }
/** * Extracts cookies from a cookie header. Works in conjunction with a cookie press class, which actually creates * the cookies and adds them to the jar as appropriate. * * 1. Parse the header into tokens, separated by ',' and ';' (respecting single and double quotes) * 2. Process tokens from the end: * a. if the token contains an '=' we have a name/value pair. Add them to the cookie press, which * will decide if it is a cookie name or an attribute name. * b. if the token is a reserved word, flush the cookie press and continue. * c. otherwise, add the token to the cookie press, passing along the last character of the previous token. */ void findCookies( String cookieHeader ) { Vector tokens = getCookieTokens( cookieHeader ); for (int i = tokens.size() - 1; i >= 0; i--) { String token = (String) tokens.elementAt( i ); int equalsIndex = getEqualsIndex( token ); if (equalsIndex != -1) { _press.addTokenWithEqualsSign( this, token, equalsIndex ); } else if (isCookieReservedWord( token )) { _press.clear(); } else { _press.addToken( token, lastCharOf( (i == 0) ? "" : (String) tokens.elementAt( i - 1 ) ) ); } } }
/** * add from a token * @param recipe - the recipe to use * @param token - the token to use * @param equalsIndex - the position of the equal sign */ void addTokenWithEqualsSign( CookieRecipe recipe, String token, int equalsIndex ) { String name = token.substring( 0, equalsIndex ).trim(); String value= token.substring( equalsIndex + 1 ).trim(); _value.insert( 0, value ); if (recipe.isCookieAttribute( name.toLowerCase() )) { _attributes.put( name.toLowerCase(), _value.toString() ); } else { addCookieIfValid( new Cookie( name, _value.toString(), _attributes ) ); _attributes.clear(); } _value.setLength(0); }
/** * add from a token * @param recipe - the recipe to use * @param token - the token to use * @param equalsIndex - the position of the equal sign */ void addTokenWithEqualsSign( CookieRecipe recipe, String token, int equalsIndex ) { String name = token.substring( 0, equalsIndex ).trim(); String value= token.substring( equalsIndex + 1 ).trim(); _value.insert( 0, value ); if (recipe.isCookieAttribute( name.toLowerCase() )) { _attributes.put( name.toLowerCase(), _value.toString() ); } else { addCookieIfValid( new Cookie( name, _value.toString(), _attributes ) ); _attributes.clear(); } _value.setLength(0); }
/** * Creates a cookie jar which is initially populated with cookies parsed from the <code>Set-Cookie</code> and * <code>Set-Cookie2</code> header fields. * <p> * Note that the parsing does not strictly follow the specifications, but * attempts to imitate the behavior of popular browsers. Specifically, * it allows cookie values to contain commas, which the * Netscape standard does not allow for, but which is required by some servers. * </p> */ public CookieJar( CookieSource source ) { _press = new CookiePress( source.getURL() ); findCookies( source.getHeaderFields( "Set-Cookie" ), new RFC2109CookieRecipe() ); findCookies( source.getHeaderFields( "Set-Cookie2" ), new RFC2965CookieRecipe() ); }
/** * Creates a cookie jar which is initially populated with cookies parsed from the <code>Set-Cookie</code> and * <code>Set-Cookie2</code> header fields. * <p> * Note that the parsing does not strictly follow the specifications, but * attempts to imitate the behavior of popular browsers. Specifically, * it allows cookie values to contain commas, which the * Netscape standard does not allow for, but which is required by some servers. * </p> */ public CookieJar( CookieSource source ) { _press = new CookiePress( source.getURL() ); findCookies( source.getHeaderFields( "Set-Cookie" ), new RFC2109CookieRecipe() ); findCookies( source.getHeaderFields( "Set-Cookie2" ), new RFC2965CookieRecipe() ); }
/** * Creates a cookie jar which is initially populated with cookies parsed from the <code>Set-Cookie</code> and * <code>Set-Cookie2</code> header fields. * <p> * Note that the parsing does not strictly follow the specifications, but * attempts to imitate the behavior of popular browsers. Specifically, * it allows cookie values to contain commas, which the * Netscape standard does not allow for, but which is required by some servers. * </p> */ public CookieJar( CookieSource source ) { _press = new CookiePress( source.getURL() ); findCookies( source.getHeaderFields( "Set-Cookie" ), new RFC2109CookieRecipe() ); findCookies( source.getHeaderFields( "Set-Cookie2" ), new RFC2965CookieRecipe() ); }
/** * add the given cookie if it is valid * @param cookie */ private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); }
/** * Creates an empty cookie jar. */ public CookieJar() { _press = new CookiePress( null ); }
/** * Creates an empty cookie jar. */ public CookieJar() { _press = new CookiePress( null ); }
/** * add the given cookie if it is valid * @param cookie */ private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); }
/** * add the given cookie if it is valid * @param cookie */ private void addCookieIfValid( Cookie cookie ) { if (acceptCookie( cookie )) addUniqueCookie( cookie ); }
/** * Creates an empty cookie jar. */ public CookieJar() { _press = new CookiePress( null ); }