[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit WebClient.java,1.43,1.44 WebConversation.
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-10-17 23:51:22
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv3856/src/com/meterware/httpunit
Modified Files:
WebClient.java WebConversation.java WebResponse.java
Log Message:
Added support for associating cookies with servers
Index: WebClient.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebClient.java,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- WebClient.java 14 Oct 2002 12:40:05 -0000 1.43
+++ WebClient.java 17 Oct 2002 23:50:48 -0000 1.44
@@ -24,10 +24,13 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
+import java.net.URL;
import java.util.*;
import org.xml.sax.SAXException;
+import com.meterware.httpunit.cookies.CookieJar;
+import com.meterware.httpunit.cookies.CookieJar;
/**
@@ -204,7 +207,7 @@
**/
public void clearContents() {
_mainWindow = new WebWindow( this );
- _cookies = new Hashtable();
+ _cookieJar.clear();
_headers = new HeaderDictionary();
}
@@ -213,7 +216,7 @@
* Defines a cookie to be sent to the server on every request.
**/
public void addCookie( String name, String value ) {
- _cookies.put( name, value );
+ _cookieJar.addCookie( name, value );
}
@@ -221,12 +224,7 @@
* Returns the name of all the active cookies which will be sent to the server.
**/
public String[] getCookieNames() {
- String[] names = new String[ _cookies.size() ];
- int i = 0;
- for (Enumeration e = _cookies.keys(); e.hasMoreElements();) {
- names[i++] = (String) e.nextElement();
- }
- return names;
+ return _cookieJar.getCookieNames();
}
@@ -234,7 +232,7 @@
* Returns the value of the specified cookie.
**/
public String getCookieValue( String name ) {
- return (String) _cookies.get( name );
+ return _cookieJar.getCookieValue( name );
}
@@ -431,34 +429,22 @@
/**
- * Returns the value of the cookie header, or null if none is defined.
- **/
- protected String getCookieHeaderField() {
- String names[] = getCookieNames();
- if (names.length == 0) return null;
-
- StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_BUFFER_SIZE);
- for (int i = 0; i < names.length; i++) {
- if (i != 0) sb.append( ";" );
- String name = names[i];
- sb.append( name ).append( '=' ).append( getCookieValue( name ) );
- }
- return sb.toString();
- }
-
-
- /**
* Returns the value of all current header fields.
**/
- protected Dictionary getHeaderFields() {
- setHeaderField( "User-Agent", getClientProperties().getUserAgent() );
+ protected Dictionary getHeaderFields( URL targetURL ) {
Hashtable result = (Hashtable) _headers.clone();
- if (getCookieHeaderField() != null) result.put( "Cookie", getCookieHeaderField() );
- if (_authorizationString != null) result.put( getAuthorizationHeaderName(), _authorizationString );
+ result.put( "User-Agent", getClientProperties().getUserAgent() );
+ AddHeaderIfNotNull( result, "Cookie", _cookieJar.getCookieHeaderField( targetURL ) );
+ AddHeaderIfNotNull( result, getAuthorizationHeaderName(), _authorizationString );
return result;
}
+ private void AddHeaderIfNotNull( Hashtable result, final String headerName, final String headerValue ) {
+ if (headerValue != null) result.put( headerName, headerValue );
+ }
+
+
private String getAuthorizationHeaderName() {
return getProxyHost() != null ? "Proxy-Authorization" : "Authorization";
}
@@ -475,7 +461,7 @@
void updateClient( WebResponse response ) throws IOException {
- updateCookies( response );
+ if (HttpUnitOptions.isAcceptCookies()) _cookieJar.updateCookies( response.getCookieJar() );
validateHeaders( response );
}
@@ -565,7 +551,7 @@
private LinkedList _alerts = new LinkedList();
/** The currently defined cookies. **/
- private Hashtable _cookies = new Hashtable();
+ private CookieJar _cookieJar = new CookieJar();
/** A map of header names to values. **/
@@ -596,19 +582,6 @@
throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() );
} else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() );
- }
- }
-
-
- /**
- * Updates the cookies maintained in this client based on new cookies requested by the server.
- **/
- private void updateCookies( WebResponse response ) {
- if (!HttpUnitOptions.isAcceptCookies()) return;
-
- String[] names = response.getNewCookieNames();
- for (int i = 0; i < names.length; i++) {
- addCookie( names[i], response.getNewCookieValue( names[i] ) );
}
}
Index: WebConversation.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebConversation.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- WebConversation.java 4 Oct 2002 00:40:28 -0000 1.26
+++ WebConversation.java 17 Oct 2002 23:50:49 -0000 1.27
@@ -57,7 +57,7 @@
URLConnection connection = openConnection( request.getURL() );
sendHeaders( connection, request.getHeaderDictionary() );
if (HttpUnitOptions.isLoggingHttpHeaders()) {
- for (Enumeration e = getHeaderFields().keys(); e.hasMoreElements(); ) {
+ for (Enumeration e = getHeaderFields( request.getURL() ).keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) );
}
@@ -83,7 +83,7 @@
private void sendHeaders( URLConnection connection ) {
- sendHeaders( connection, getHeaderFields() );
+ sendHeaders( connection, getHeaderFields( connection.getURL() ) );
}
Index: WebResponse.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v
retrieving revision 1.92
retrieving revision 1.93
diff -u -r1.92 -r1.93
--- WebResponse.java 14 Oct 2002 04:19:06 -0000 1.92
+++ WebResponse.java 17 Oct 2002 23:50:49 -0000 1.93
@@ -20,6 +20,9 @@
*
*******************************************************************************************************************/
import com.meterware.httpunit.scripting.ScriptableDelegate;
+import com.meterware.httpunit.cookies.CookieJar;
+import com.meterware.httpunit.cookies.Cookie;
+import com.meterware.httpunit.cookies.CookieSource;
import java.io.*;
import java.net.HttpURLConnection;
@@ -45,7 +48,7 @@
* @author <a href="mailto:bx...@bi...">Benoit Xhenseval</a>
**/
abstract
-public class WebResponse implements HTMLSegment {
+public class WebResponse implements HTMLSegment, CookieSource {
/**
* Returns a web response built from a URL connection. Provided to allow
@@ -206,12 +209,7 @@
* Returns a list of new cookie names defined as part of this response.
**/
public String[] getNewCookieNames() {
- String[] names = new String[ getNewCookies().size() ];
- int i = 0;
- for (Enumeration e = getNewCookies().keys(); e.hasMoreElements(); i++ ) {
- names[i] = (String) e.nextElement();
- }
- return names;
+ return getCookieJar().getCookieNames();
}
@@ -219,7 +217,7 @@
* Returns the new cookie value defined as part of this response.
**/
public String getNewCookieValue( String name ) {
- return (String) getNewCookies().get( name );
+ return getCookieJar().getCookieValue( name );
}
@@ -230,7 +228,7 @@
public String[] getHeaderFieldNames();
- /**
+ /**
* Returns the value for the specified header field. If no such field is defined, will return null.
* If more than one header is defined for the specified name, returns only the first found.
**/
@@ -239,13 +237,6 @@
/**
- * Returns the values for the specified header field. If no such field is defined, will return an empty array.
- **/
- abstract
- public String[] getHeaderFields( String fieldName );
-
-
- /**
* Returns the text of the response (excluding headers) as a string. Use this method in preference to 'toString'
* which may be used to represent internal state of this object.
**/
@@ -775,26 +766,6 @@
//--------------------------------- private members --------------------------------------
- /**
- * A version flag indicating a cookie is based on the
- * Internet Engineering Task Force's (IETF)
- * <a href="http://www.ietf.org/rfc/rfc2109.txt">RFC 2109</a>
- *
- * <br />
- * These cookies come from the <code>Set-Cookie:</code> header
- **/
- final private static int IETF_RFC2109 = 0;
-
- /**
- * A version flag indicating a cookie is based on the
- * Internet Engineering Task Force's (IETF)
- * <a href="http://www.ietf.org/rfc/rfc2965.txt">RFC 2965</a>
- *
- * <br />
- * These cookies come from the <code>Set-Cookie2:</code> header
- **/
- final private static int IETF_RFC2965 = 1;
-
final private static String HTML_CONTENT = "text/html";
final private static int UNINITIALIZED_INT = -2;
@@ -813,8 +784,6 @@
private String _characterSet;
- private Hashtable _newCookies;
-
private WebRequest _refreshRequest;
private int _refreshDelay;
@@ -899,152 +868,13 @@
}
- /**
- * Parses cookies from the <code>Set-Cookie</code> and the
- * <code>Set-Cookie2</code> header fields.
- * <p>
- * This class does not strictly follow the specifications, but
- * attempts to imitate the behavior of popular browsers. Specifically,
- * this method allows cookie values to contain commas, which the
- * Netscape standard does not allow for.
- * </p><p>
- * This method does not parse path,domain,expires or secure information
- * about the cookie.</p>
- *
- * @return Hashtable a <code>Hashtable</code> of where the name of the
- * cookie is the key and the value of the cookie is
- * the value
- */
- private Hashtable getNewCookies() {
- if (_newCookies == null) _newCookies = new Hashtable();
- processCookieHeaders( getHeaderFields( "Set-Cookie" ), IETF_RFC2109 );
- processCookieHeaders( getHeaderFields( "Set-Cookie2" ), IETF_RFC2965 );
- return _newCookies;
- }
-
-
- private void processCookieHeaders( String cookieHeader[], int version ) {
- for (int i = 0; i < cookieHeader.length; i++) {
- processCookieHeader( cookieHeader[i], version );
- }
- }
-
-
- private void processCookieHeader( String cookieHeader, int version ) {
- Vector tokens = getCookieTokens( cookieHeader );
- String tokensToAdd = "";
- for (int i = tokens.size() - 1; i >= 0; i--) {
- String token = (String) tokens.elementAt( i );
-
- int equalsIndex = getEqualsIndex( token );
- if (equalsIndex != -1) {
- String name = token.substring( 0, equalsIndex ).trim();
- if (!isCookieAttribute( name, version )) {
- String value = token.substring( equalsIndex + 1 ).trim();
- _newCookies.put( name, value + tokensToAdd );
- }
- tokensToAdd = "";
- } else if (isCookieReservedWord( token, version )) {
- tokensToAdd = "";
- } else {
- tokensToAdd = token + tokensToAdd;
- if (i > 0) {
- String preceedingToken = (String) tokens.elementAt( i - 1 );
- char lastChar = preceedingToken.charAt( preceedingToken.length() - 1 );
- if (lastChar != '=') {
- tokensToAdd = "," + tokensToAdd;
- }
- }
- }
- }
- }
-
-
- /**
- * 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 ) );
- }
+ CookieJar getCookieJar() {
+ if (_cookies == null) _cookies = new CookieJar( this );
+ return _cookies;
}
- /**
- * 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;
- }
-
-
- private boolean isCookieAttribute( String string, int version ) {
- String stringLowercase = string.toLowerCase();
- if (version == IETF_RFC2109) {
- return stringLowercase.equals("path") ||
- stringLowercase.equals("domain") ||
- stringLowercase.equals("expires") ||
- stringLowercase.equals("comment") ||
- stringLowercase.equals("max-age") ||
- stringLowercase.equals("version");
- } else if (version == IETF_RFC2965) {
- 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");
- } else {
- return false;
- }
- }
-
-
- private boolean isCookieReservedWord( String token,
- int version ) {
- if (version == IETF_RFC2109) {
- return token.equalsIgnoreCase( "secure" );
- } else if (version == IETF_RFC2965) {
- return token.equalsIgnoreCase( "discard" ) || token.equalsIgnoreCase( "secure" );
- } else {
- return false;
- }
- }
-
+ private CookieJar _cookies;
private void readContentTypeHeader() {
String contentHeader = (_contentHeader != null) ? _contentHeader
|