You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2005/10/22 13:08:03 UTC

svn commit: r327651 - /jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java

Author: sebb
Date: Sat Oct 22 04:07:52 2005
New Revision: 327651

URL: http://svn.apache.org/viewcvs?rev=327651&view=rev
Log:
Path fix was slightly wrong - trailing slash only needed for Set-Cookie2.
Fix ClassCastException when loading cookies from file - get(i)

Modified:
    jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java

Modified: jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java
URL: http://svn.apache.org/viewcvs/jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java?rev=327651&r1=327650&r2=327651&view=diff
==============================================================================
--- jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java (original)
+++ jakarta/jmeter/branches/rel-2-1/src/protocol/http/org/apache/jmeter/protocol/http/control/CookieManager.java Sat Oct 22 04:07:52 2005
@@ -67,20 +67,18 @@
 	public static final String COOKIES = "CookieManager.cookies";
 
 	// SimpleDateFormat isn't thread-safe
-	// TestElements are cloned for each thread, so
-	// we use an instance variable.
+	// TestElements are cloned for each thread, so we use an instance variable.
 	private SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US);
 
 	// See bug 33796
-	private static final boolean DELETE_NULL_COOKIES = JMeterUtils.getPropDefault("CookieManager.delete_null_cookies",
-			true);// $NON-NLS-1$
+	private static final boolean DELETE_NULL_COOKIES 
+        = JMeterUtils.getPropDefault("CookieManager.delete_null_cookies", true);// $NON-NLS-1$
 
 	public CookieManager() {
 		// The cookie specification requires that the timezone be GMT.
-		// See
-		// http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm
-		// See http://www.cookiecentral.com/faq/
-		// See http://wp.netscape.com/newsref/std/cookie_spec.html
+		// See:
+		// http://wp.netscape.com/newsref/std/cookie_spec.html (Netscape)
+        // http://www.w3.org/Protocols/rfc2109/rfc2109.txt
 		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
 
 		setProperty(new CollectionProperty(COOKIES, new ArrayList()));
@@ -91,7 +89,7 @@
 		return (CollectionProperty) getProperty(COOKIES);
 	}
 
-	public int getCookieCount() {
+	public int getCookieCount() {// Used by GUI
 		return getCookies().size();
 	}
 
@@ -115,7 +113,8 @@
 	}
 
 	/**
-	 * Save the cookie data to a file.
+	 * Save the static cookie data to a file.
+     * Cookies are only taken from the GUI - runtime cookies are not included. 
 	 */
 	public void save(String authFile) throws IOException {
 		File file = new File(authFile);
@@ -187,19 +186,23 @@
 	 */
 	public void add(Cookie c) {
 		String cv = c.getValue();
+        String cn = c.getName();
 		if (DELETE_NULL_COOKIES && (null == cv || "".equals(cv))) {
             if (log.isDebugEnabled()) {
                 log.debug("Removing cookie with null value " + c.toString());
             }
-			removeCookieNamed(c.getName());
+			removeCookieNamed(cn);
 		} else {
             if (log.isDebugEnabled()) {
                 log.debug("Add cookie " + c.toString());
             }
-			JMeterContext context = getThreadContext();
 			getCookies().addItem(c);
+            // Store cookie as a thread variable. 
+            // TODO - should we add a prefix to these variables?
+            // TODO - should storing cookie values be optional?
+            JMeterContext context = getThreadContext();
 			if (context.isSamplingStarted()) {
-				context.getVariables().put(c.getName(), c.getValue());
+				context.getVariables().put(cn, cv);
 			}
 		}
 	}
@@ -228,22 +231,16 @@
 	/**
 	 * Remove a cookie.
 	 */
-	public void remove(int index) {
+	public void remove(int index) {// TODO not used by GUI
 		getCookies().remove(index);
 	}
 
 	/**
-	 * Return the number of cookies.
-	 */
-	public int size() {
-		return getCookies().size();
-	}
-
-	/**
 	 * Return the cookie at index i.
 	 */
-	public Cookie get(int i) {
-		return (Cookie) getCookies().get(i);
+	public Cookie get(int i) {// Only used by GUI
+        //JMeterProperty ck=getCookies().get(i);
+		return (Cookie) getCookies().get(i).getObjectValue();
 	}
 
 	public String convertLongToDateFormatStr(long dateLong) {
@@ -337,16 +334,16 @@
 			path = "/"; // default if no path specified
 		} else {
 			int lastSlash = path.lastIndexOf("/");
-			if (lastSlash > -1) {
-				path=path.substring(0,lastSlash+1);
+			if (lastSlash > 0) {// Must be after initial character
+                // Upto, but not including, trailing slash for Set-Cookie:
+                // (Set-Cookie2: would need the trailing slash as well
+				path=path.substring(0,lastSlash);
 			}
 		}
 
-		Cookie newCookie = new Cookie(name, value, domain, path, false, 0); // No
-																			// expiry
-																			// means
-																			// session
-																			// cookie
+		Cookie newCookie = new Cookie(name, value, domain, path, false
+                                    , 0); // No expiry means session cookie
+
 		// check the rest of the headers
 		while (st.hasMoreTokens()) {
 			nvp = st.nextToken();
@@ -400,22 +397,24 @@
 			}
 		}
 
-		Vector removeIndices = new Vector();
+        // Scan for any matching cookies
+        Vector removeIndices = new Vector();
 		for (int i = getCookies().size() - 1; i >= 0; i--) {
 			Cookie cookie = (Cookie) getCookies().get(i).getObjectValue();
 			if (cookie == null)
 				continue;
-			if (cookie.getPath().equals(newCookie.getPath()) && cookie.getDomain().equals(newCookie.getDomain())
+			if (cookie.getPath().equals(newCookie.getPath()) 
+                    && cookie.getDomain().equals(newCookie.getDomain())
 					&& cookie.getName().equals(newCookie.getName())) {
 				if (debugEnabled) {
-					log
-							.debug("New Cookie = " + newCookie.toString() + " removing matching Cookie "
-									+ cookie.toString());
+					log.debug("New Cookie = " + newCookie.toString()
+                              + " removing matching Cookie " + cookie.toString());
 				}
 				removeIndices.addElement(new Integer(i));
 			}
 		}
 
+        // Now remove the matching cookies
 		for (Enumeration e = removeIndices.elements(); e.hasMoreElements();) {
 			index = ((Integer) e.nextElement()).intValue();
 			remove(index);



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org