You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2006/05/04 16:18:28 UTC
svn commit: r399709 - in
/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src:
java/org/apache/commons/httpclient/cookie/
test/org/apache/commons/httpclient/cookie/
Author: olegk
Date: Thu May 4 07:18:27 2006
New Revision: 399709
URL: http://svn.apache.org/viewcvs?rev=399709&view=rev
Log:
Improved compliance with the section 3.3.4 of the RFC 2965. Arrange cookies by value of the path attribute (cookies with a more specific path attribute take precedence)
Added:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java (with props)
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java (with props)
Modified:
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java
Added: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java?rev=399709&view=auto
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java (added)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java Thu May 4 07:18:27 2006
@@ -0,0 +1,80 @@
+/*
+ * $Header$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 1999-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.commons.httpclient.cookie;
+
+import java.util.Comparator;
+
+import org.apache.commons.httpclient.Cookie;
+
+/**
+ * This cookie comparator ensures that multiple cookies satisfying
+ * a common criteria are ordered in the <tt>Cookie</tt> header such
+ * that those with more specific Path attributes precede those with
+ * less specific.
+ *
+ * <p>
+ * This comparator assumes that Path attributes of two cookies
+ * path-match a commmon request-URI. Otherwise, the result of the
+ * comparison is undefined.
+ * </p>
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class CookiePathComparator implements Comparator {
+
+ private String normalizePath(final Cookie cookie) {
+ String path = cookie.getPath();
+ if (path == null) {
+ path = "/";
+ }
+ if (!path.endsWith("/")) {
+ path = path + "/";
+ }
+ return path;
+ }
+
+ public int compare(final Object o1, final Object o2) {
+ Cookie c1 = (Cookie) o1;
+ Cookie c2 = (Cookie) o2;
+ String path1 = normalizePath(c1);
+ String path2 = normalizePath(c2);
+ if (path1.equals(path2)) {
+ return 0;
+ } else if (path1.startsWith(path2)) {
+ return -1;
+ } else if (path2.startsWith(path1)) {
+ return 1;
+ } else {
+ // Does not really matter
+ return 0;
+ }
+ }
+
+}
Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/CookiePathComparator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java?rev=399709&r1=399708&r2=399709&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java Thu May 4 07:18:27 2006
@@ -30,6 +30,8 @@
package org.apache.commons.httpclient.cookie;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
@@ -53,6 +55,8 @@
*/
public class RFC2965Spec extends CookieSpecBase implements CookieVersionSupport {
+ private static final Comparator PATH_COMPOARATOR = new CookiePathComparator();
+
/**
* Cookie Response Header name for cookies processed
* by this spec.
@@ -173,7 +177,7 @@
* portnum = 1*DIGIT
* </PRE>
*
- * @param host the host from which the <tt>Set-Cookie2</tt> value was
+ * @param host the host from whichPATH_COMPOARATOR the <tt>Set-Cookie2</tt> value was
* received
* @param port the port from which the <tt>Set-Cookie2</tt> value was
* received
@@ -432,7 +436,6 @@
throw new IllegalArgumentException("Cookie may not be null");
}
if (cookie instanceof Cookie2) {
- /* format cookie2 cookie */
Cookie2 cookie2 = (Cookie2) cookie;
int version = cookie2.getVersion();
final StringBuffer buffer = new StringBuffer();
@@ -479,7 +482,9 @@
// delegate old-style cookie formatting to rfc2109Spec
return this.rfc2109.formatCookies(cookies);
}
- /* format cookie2 cookies */
+ // Arrange cookies by path
+ Arrays.sort(cookies, PATH_COMPOARATOR);
+
final StringBuffer buffer = new StringBuffer();
// format cookie version
this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));
Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java?rev=399709&r1=399708&r2=399709&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java Thu May 4 07:18:27 2006
@@ -51,6 +51,7 @@
suite.addTest(TestCookieIgnoreSpec.suite());
suite.addTest(TestCookiePolicy.suite());
suite.addTest(TestDateParser.suite());
+ suite.addTest(TestCookiePathComparator.suite());
return suite;
}
Added: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java?rev=399709&view=auto
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java (added)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java Thu May 4 07:18:27 2006
@@ -0,0 +1,105 @@
+/*
+ * $HeaderURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ * Copyright 1999-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.commons.httpclient.cookie;
+
+import java.util.Comparator;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.httpclient.Cookie;
+
+/**
+ * Test cases for {@link CookiePathComparator}.
+ */
+public class TestCookiePathComparator extends TestCookieBase {
+
+
+ // ------------------------------------------------------------ Constructor
+
+ public TestCookiePathComparator(String name) {
+ super(name);
+ }
+
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestCookiePathComparator.class);
+ }
+
+ public void testUnequality1() {
+ Cookie cookie1 = new Cookie(".whatever.com", "name1", "value", "/a/b/", null, false);
+ Cookie cookie2 = new Cookie(".whatever.com", "name1", "value", "/a/", null, false);
+ Comparator comparator = new CookiePathComparator();
+ assertTrue(comparator.compare(cookie1, cookie2) < 0);
+ assertTrue(comparator.compare(cookie2, cookie1) > 0);
+ }
+
+ public void testUnequality2() {
+ Cookie cookie1 = new Cookie(".whatever.com", "name1", "value", "/a/b", null, false);
+ Cookie cookie2 = new Cookie(".whatever.com", "name1", "value", "/a", null, false);
+ Comparator comparator = new CookiePathComparator();
+ assertTrue(comparator.compare(cookie1, cookie2) < 0);
+ assertTrue(comparator.compare(cookie2, cookie1) > 0);
+ }
+
+ public void testEquality1() {
+ Cookie cookie1 = new Cookie(".whatever.com", "name1", "value", "/a", null, false);
+ Cookie cookie2 = new Cookie(".whatever.com", "name1", "value", "/a", null, false);
+ Comparator comparator = new CookiePathComparator();
+ assertTrue(comparator.compare(cookie1, cookie2) == 0);
+ assertTrue(comparator.compare(cookie2, cookie1) == 0);
+ }
+
+ public void testEquality2() {
+ Cookie cookie1 = new Cookie(".whatever.com", "name1", "value", "/a/", null, false);
+ Cookie cookie2 = new Cookie(".whatever.com", "name1", "value", "/a", null, false);
+ Comparator comparator = new CookiePathComparator();
+ assertTrue(comparator.compare(cookie1, cookie2) == 0);
+ assertTrue(comparator.compare(cookie2, cookie1) == 0);
+ }
+
+ public void testEquality3() {
+ Cookie cookie1 = new Cookie(".whatever.com", "name1", "value", null, null, false);
+ Cookie cookie2 = new Cookie(".whatever.com", "name1", "value", "/", null, false);
+ Comparator comparator = new CookiePathComparator();
+ assertTrue(comparator.compare(cookie1, cookie2) == 0);
+ assertTrue(comparator.compare(cookie2, cookie1) == 0);
+ }
+
+ public void testEquality4() {
+ Cookie cookie1 = new Cookie(".whatever.com", "name1", "value", "/this", null, false);
+ Cookie cookie2 = new Cookie(".whatever.com", "name1", "value", "/that", null, false);
+ Comparator comparator = new CookiePathComparator();
+ assertTrue(comparator.compare(cookie1, cookie2) == 0);
+ assertTrue(comparator.compare(cookie2, cookie1) == 0);
+ }
+
+}
+
Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookiePathComparator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org