[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit/cookies Cookie.java,1.2,1.3 CookieJar.jav
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-11-04 21:01:41
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/cookies
In directory usw-pr-cvs1:/tmp/cvs-serv6863/src/com/meterware/httpunit/cookies
Modified Files:
Cookie.java CookieJar.java
Log Message:
Correct global vs rooted cookie handling
Index: Cookie.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/cookies/Cookie.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Cookie.java 3 Nov 2002 17:37:37 -0000 1.2
+++ Cookie.java 4 Nov 2002 21:01:37 -0000 1.3
@@ -117,8 +117,13 @@
private boolean equals( Cookie other ) {
return _name.equalsIgnoreCase( other._name ) &&
- getDomain().equals( other.getDomain() ) &&
- getPath().equals( other.getPath() );
+ equalProperties( getDomain(), other.getDomain() ) &&
+ equalProperties( getPath(), other.getPath() );
+ }
+
+
+ private boolean equalProperties( String first, String second ) {
+ return first == second || (first != null && first.equals( second ));
}
Index: CookieJar.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/cookies/CookieJar.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CookieJar.java 17 Oct 2002 23:50:49 -0000 1.1
+++ CookieJar.java 4 Nov 2002 21:01:37 -0000 1.2
@@ -37,6 +37,7 @@
private static final int DEFAULT_HEADER_SIZE = 80;
private ArrayList _cookies = new ArrayList();
+ private ArrayList _globalCookies = new ArrayList();
private CookiePress _press;
@@ -77,6 +78,7 @@
*/
public void clear() {
_cookies.clear();
+ _globalCookies.clear();
}
@@ -85,7 +87,7 @@
* certain cookies are sent based on their host and path.
**/
public void addCookie( String name, String value ) {
- addUniqueCookie( new Cookie( name, value ) );
+ _globalCookies.add( new Cookie( name, value ) );
}
@@ -93,9 +95,13 @@
* Returns the name of all the active cookies in this cookie jar.
**/
public String[] getCookieNames() {
- String[] names = new String[ _cookies.size() ];
- for (int i = 0; i < names.length; i++) {
- names[i] = ((Cookie) _cookies.get(i)).getName();
+ final int numGlobalCookies = _globalCookies.size();
+ String[] names = new String[ _cookies.size() + numGlobalCookies ];
+ for (int i = 0; i < numGlobalCookies; i++) {
+ names[i] = ((Cookie) _globalCookies.get(i)).getName();
+ }
+ for (int i = numGlobalCookies; i < names.length; i++) {
+ names[ i + numGlobalCookies ] = ((Cookie) _cookies.get(i)).getName();
}
return names;
}
@@ -105,7 +111,9 @@
* Returns a collection containing all of the cookies in this jar.
*/
public Collection getCookies() {
- return (Collection) _cookies.clone();
+ final Collection collection = (Collection) _cookies.clone();
+ collection.addAll( _globalCookies );
+ return collection;
}
@@ -127,6 +135,10 @@
Cookie cookie = (Cookie) iterator.next();
if (name.equals( cookie.getName() )) return cookie;
}
+ for (Iterator iterator = _globalCookies.iterator(); iterator.hasNext();) {
+ Cookie cookie = (Cookie) iterator.next();
+ if (name.equals( cookie.getName() )) return cookie;
+ }
return null;
}
@@ -136,11 +148,19 @@
* Will return null if no compatible cookie is defined.
**/
public String getCookieHeaderField( URL targetURL ) {
- if (_cookies.isEmpty()) return null;
+ if (_cookies.isEmpty() && _globalCookies.isEmpty()) return null;
StringBuffer sb = new StringBuffer( DEFAULT_HEADER_SIZE );
+ HashSet restrictedCookies = new HashSet();
for (Iterator i = _cookies.iterator(); i.hasNext();) {
Cookie cookie = (Cookie) i.next();
if (!cookie.mayBeSentTo( targetURL )) continue;
+ restrictedCookies.add( cookie.getName() );
+ if (sb.length() != 0) sb.append( ';' );
+ sb.append( cookie.getName() ).append( '=' ).append( cookie.getValue() );
+ }
+ for (Iterator i = _globalCookies.iterator(); i.hasNext();) {
+ Cookie cookie = (Cookie) i.next();
+ if (restrictedCookies.contains( cookie.getName() )) continue;
if (sb.length() != 0) sb.append( ';' );
sb.append( cookie.getName() ).append( '=' ).append( cookie.getValue() );
}
|