Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 20373 invoked from network); 4 May 2006 14:19:36 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 4 May 2006 14:19:36 -0000 Received: (qmail 54108 invoked by uid 500); 4 May 2006 14:19:26 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 54034 invoked by uid 500); 4 May 2006 14:19:25 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 54023 invoked by uid 500); 4 May 2006 14:19:25 -0000 Received: (qmail 54020 invoked by uid 99); 4 May 2006 14:19:25 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 May 2006 07:19:25 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 04 May 2006 07:19:24 -0700 Received: (qmail 19615 invoked by uid 65534); 4 May 2006 14:18:59 -0000 Message-ID: <20060504141859.19552.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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/ Date: Thu, 04 May 2006 14:18:28 -0000 To: commons-cvs@jakarta.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.8 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 + * . + * + */ + +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 Cookie header such + * that those with more specific Path attributes precede those with + * less specific. + * + *

+ * This comparator assumes that Path attributes of two cookies + * path-match a commmon request-URI. Otherwise, the result of the + * comparison is undefined. + *

+ * + * @author Oleg Kalnichevski + */ +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 * * - * @param host the host from which the Set-Cookie2 value was + * @param host the host from whichPATH_COMPOARATOR the Set-Cookie2 value was * received * @param port the port from which the Set-Cookie2 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 + * . + * + */ + +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