diff --git a/src/java/org/apache/http/cookie/Cookie.java b/src/java/org/apache/http/cookie/Cookie.java index 38e63e948..a734911af 100644 --- a/src/java/org/apache/http/cookie/Cookie.java +++ b/src/java/org/apache/http/cookie/Cookie.java @@ -186,7 +186,7 @@ public class Cookie extends NameValuePair { * @return true if this cookie should only be sent over secure connections. * @see #setSecure(boolean) */ - public boolean getSecure() { + public boolean isSecure() { return isSecure; } @@ -200,7 +200,7 @@ public class Cookie extends NameValuePair { * * @param secure The value of the secure attribute * - * @see #getSecure() + * @see #isSecure() */ public void setSecure (boolean secure) { isSecure = secure; @@ -241,19 +241,6 @@ public class Cookie extends NameValuePair { && cookieExpiryDate.getTime() <= System.currentTimeMillis()); } - /** - * Returns true if this cookie has expired according to the time passed in. - * - * @param now The current time. - * - * @return true if the cookie expired. - */ - public boolean isExpired(Date now) { - return (cookieExpiryDate != null - && cookieExpiryDate.getTime() <= now.getTime()); - } - - /** * Indicates whether the cookie had a path specified in a * path attribute of the Set-Cookie header. This value diff --git a/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java b/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java new file mode 100644 index 000000000..0ce774830 --- /dev/null +++ b/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java @@ -0,0 +1,48 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-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.http.cookie.impl; + +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieAttributeHandler; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.MalformedCookieException; + +public abstract class AbstractCookieAttributeHandler implements CookieAttributeHandler { + + public void validate(final Cookie cookie, final CookieOrigin origin) + throws MalformedCookieException { + // Do nothing + } + + public boolean match(final Cookie cookie, final CookieOrigin origin) { + // Always match + return true; + } + +} \ No newline at end of file diff --git a/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java b/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java new file mode 100644 index 000000000..abc485a14 --- /dev/null +++ b/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java @@ -0,0 +1,48 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-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.http.cookie.impl; + +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.MalformedCookieException; + +public class BasicCommentHandler extends AbstractCookieAttributeHandler { + + public BasicCommentHandler() { + super(); + } + + public void parse(final Cookie cookie, final String value) + throws MalformedCookieException { + if (cookie == null) { + throw new IllegalArgumentException("Cookie may not be null"); + } + cookie.setComment(value); + } + +} \ No newline at end of file diff --git a/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java b/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java new file mode 100644 index 000000000..57f89394b --- /dev/null +++ b/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java @@ -0,0 +1,60 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-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.http.cookie.impl; + +import java.util.Date; + +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.MalformedCookieException; + +public class BasicMaxAgeHandler extends AbstractCookieAttributeHandler { + + public BasicMaxAgeHandler() { + super(); + } + + public void parse(final Cookie cookie, final String value) + throws MalformedCookieException { + if (cookie == null) { + throw new IllegalArgumentException("Cookie may not be null"); + } + if (value == null) { + throw new MalformedCookieException("Missing value for max-age attribute"); + } + int age; + try { + age = Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new MalformedCookieException ("Invalid max-age attribute: " + + value); + } + cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L)); + } + +} \ No newline at end of file diff --git a/src/java/org/apache/http/cookie/impl/BasicPathHandler.java b/src/java/org/apache/http/cookie/impl/BasicPathHandler.java new file mode 100644 index 000000000..32dcda80e --- /dev/null +++ b/src/java/org/apache/http/cookie/impl/BasicPathHandler.java @@ -0,0 +1,89 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-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.http.cookie.impl; + +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieAttributeHandler; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.MalformedCookieException; + +public class BasicPathHandler implements CookieAttributeHandler { + + public BasicPathHandler() { + super(); + } + + public void parse(final Cookie cookie, String value) + throws MalformedCookieException { + if (cookie == null) { + throw new IllegalArgumentException("Cookie may not be null"); + } + if (value == null || value.trim().equals("")) { + value = "/"; + } + cookie.setPath(value); + cookie.setPathAttributeSpecified(true); + } + + public void validate(final Cookie cookie, final CookieOrigin origin) + throws MalformedCookieException { + if (!match(cookie, origin)) { + throw new MalformedCookieException( + "Illegal path attribute \"" + cookie.getPath() + + "\". Path of origin: \"" + origin.getPath() + "\""); + } + } + + public boolean match(final Cookie cookie, final CookieOrigin origin) { + if (cookie == null) { + throw new IllegalArgumentException("Cookie may not be null"); + } + if (origin == null) { + throw new IllegalArgumentException("Cookie origin may not be null"); + } + String targetpath = origin.getPath(); + String topmostPath = cookie.getPath(); + if (topmostPath == null) { + topmostPath = "/"; + } + if (topmostPath.length() > 1 && topmostPath.endsWith("/")) { + topmostPath = topmostPath.substring(0, topmostPath.length() - 1); + } + boolean match = targetpath.startsWith (topmostPath); + // if there is a match and these values are not exactly the same we have + // to make sure we're not matcing "/foobar" and "/foo" + if (match && targetpath.length() != topmostPath.length()) { + if (!topmostPath.endsWith("/")) { + match = (targetpath.charAt(topmostPath.length()) == '/'); + } + } + return match; + } + +} \ No newline at end of file diff --git a/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java b/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java new file mode 100644 index 000000000..8b7e5ac9f --- /dev/null +++ b/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java @@ -0,0 +1,59 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 2002-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.http.cookie.impl; + +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.MalformedCookieException; + +public class BasicSecureHandler extends AbstractCookieAttributeHandler { + + public BasicSecureHandler() { + super(); + } + + public void parse(final Cookie cookie, final String value) + throws MalformedCookieException { + if (cookie == null) { + throw new IllegalArgumentException("Cookie may not be null"); + } + cookie.setSecure(true); + } + + public boolean match(final Cookie cookie, final CookieOrigin origin) { + if (cookie == null) { + throw new IllegalArgumentException("Cookie may not be null"); + } + if (origin == null) { + throw new IllegalArgumentException("Cookie origin may not be null"); + } + return cookie.isSecure() ? origin.isSecure() : true; + } + +} \ No newline at end of file diff --git a/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java b/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java index ae15af156..562577abf 100644 --- a/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java +++ b/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java @@ -29,6 +29,7 @@ package org.apache.http.cookie.impl; import java.io.IOException; +import java.util.Iterator; import org.apache.http.Header; import org.apache.http.cookie.Cookie; @@ -96,9 +97,18 @@ public class TestAbstractCookieSpec extends TestCase { AbstractCookieSpec cookiespec = new DummyCookieSpec(); cookiespec.registerAttribHandler("this", h1); cookiespec.registerAttribHandler("that", h2); - + cookiespec.registerAttribHandler("thistoo", h1); + cookiespec.registerAttribHandler("thattoo", h2); + assertTrue(h1 == cookiespec.getAttribHandler("this")); assertTrue(h2 == cookiespec.getAttribHandler("that")); + assertTrue(h1 == cookiespec.getAttribHandler("thistoo")); + assertTrue(h2 == cookiespec.getAttribHandler("thattoo")); + + Iterator it = cookiespec.getAttribHandlerIterator(); + assertNotNull(it.next()); + assertNotNull(it.next()); + assertFalse(it.hasNext()); } public void testInvalidHandler() throws IOException { @@ -117,5 +127,21 @@ public class TestAbstractCookieSpec extends TestCase { // expected } } + + public void testBasicPathInvalidInput() throws Exception { + AbstractCookieSpec cookiespec = new DummyCookieSpec(); + try { + cookiespec.registerAttribHandler(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + cookiespec.registerAttribHandler("whatever", null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } } \ No newline at end of file diff --git a/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java b/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java index 26238695d..a039b46a5 100644 --- a/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java +++ b/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java @@ -39,6 +39,7 @@ public class TestAllCookieImpl extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(TestAbstractCookieSpec.suite()); + suite.addTest(TestBasicCookieAttribHandlers.suite()); return suite; } diff --git a/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java b/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java new file mode 100644 index 000000000..bce5c366f --- /dev/null +++ b/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java @@ -0,0 +1,263 @@ +/* + * $HeadURL$ + * $Revisio$ + * $Date$ + * ==================================================================== + * + * Copyright 2002-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.http.cookie.impl; + +import org.apache.http.cookie.Cookie; +import org.apache.http.cookie.CookieAttributeHandler; +import org.apache.http.cookie.CookieOrigin; +import org.apache.http.cookie.MalformedCookieException; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestBasicCookieAttribHandlers extends TestCase { + + public TestBasicCookieAttribHandlers(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(TestBasicCookieAttribHandlers.class); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestBasicCookieAttribHandlers.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public void testBasicPathParse() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieAttributeHandler h = new BasicPathHandler(); + h.parse(cookie, "stuff"); + assertEquals("stuff", cookie.getPath()); + assertTrue(cookie.isPathAttributeSpecified()); + h.parse(cookie, ""); + assertEquals("/", cookie.getPath()); + assertTrue(cookie.isPathAttributeSpecified()); + h.parse(cookie, null); + assertEquals("/", cookie.getPath()); + assertTrue(cookie.isPathAttributeSpecified()); + } + + public void testBasicPathMatch1() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff"); + assertTrue(h.match(cookie, origin)); + } + + public void testBasicPathMatch2() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff"); + assertTrue(h.match(cookie, origin)); + } + + public void testBasicPathMatch3() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/more-stuff", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff"); + assertTrue(h.match(cookie, origin)); + } + + public void testBasicPathMatch4() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuffed", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff"); + assertFalse(h.match(cookie, origin)); + } + + public void testBasicPathMatch5() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/otherstuff", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff"); + assertFalse(h.match(cookie, origin)); + } + + public void testBasicPathMatch6() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff/"); + assertTrue(h.match(cookie, origin)); + } + + public void testBasicPathMatch7() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false); + CookieAttributeHandler h = new BasicPathHandler(); + assertTrue(h.match(cookie, origin)); + } + + public void testBasicPathValidate() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false); + CookieAttributeHandler h = new BasicPathHandler(); + cookie.setPath("/stuff"); + h.validate(cookie, origin); + cookie.setPath("/stuffed"); + try { + h.validate(cookie, origin); + fail("MalformedCookieException must have been thrown"); + } catch (MalformedCookieException ex) { + // expected + } + } + + public void testBasicPathInvalidInput() throws Exception { + CookieAttributeHandler h = new BasicPathHandler(); + try { + h.parse(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + h.match(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + h.match(new Cookie("name", "value"), null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testBasicMaxAgeParse() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieAttributeHandler h = new BasicMaxAgeHandler(); + h.parse(cookie, "2000"); + assertNotNull(cookie.getExpiryDate()); + } + + public void testBasicMaxAgeParseInvalid() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieAttributeHandler h = new BasicMaxAgeHandler(); + try { + h.parse(cookie, "garbage"); + fail("MalformedCookieException must have been thrown"); + } catch (MalformedCookieException ex) { + // expected + } + try { + h.parse(cookie, null); + fail("MalformedCookieException must have been thrown"); + } catch (MalformedCookieException ex) { + // expected + } + } + + public void testBasicMaxAgeInvalidInput() throws Exception { + CookieAttributeHandler h = new BasicMaxAgeHandler(); + try { + h.parse(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testBasicCommentParse() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieAttributeHandler h = new BasicCommentHandler(); + h.parse(cookie, "whatever"); + assertEquals("whatever", cookie.getComment()); + h.parse(cookie, null); + assertEquals(null, cookie.getComment()); + } + + public void testBasicCommentInvalidInput() throws Exception { + CookieAttributeHandler h = new BasicCommentHandler(); + try { + h.parse(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + + public void testBasicSecureParse() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieAttributeHandler h = new BasicSecureHandler(); + h.parse(cookie, "whatever"); + assertTrue(cookie.isSecure()); + h.parse(cookie, null); + assertTrue(cookie.isSecure()); + } + + public void testBasicSecureMatch() throws Exception { + Cookie cookie = new Cookie("name", "value"); + CookieAttributeHandler h = new BasicSecureHandler(); + + CookieOrigin origin1 = new CookieOrigin("somehost", 80, "/stuff", false); + cookie.setSecure(false); + assertTrue(h.match(cookie, origin1)); + cookie.setSecure(true); + assertFalse(h.match(cookie, origin1)); + + CookieOrigin origin2 = new CookieOrigin("somehost", 80, "/stuff", true); + cookie.setSecure(false); + assertTrue(h.match(cookie, origin2)); + cookie.setSecure(true); + assertTrue(h.match(cookie, origin2)); + } + + public void testBasicSecureInvalidInput() throws Exception { + CookieAttributeHandler h = new BasicSecureHandler(); + try { + h.parse(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + h.match(null, null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + try { + h.match(new Cookie("name", "value"), null); + fail("IllegalArgumentException must have been thrown"); + } catch (IllegalArgumentException ex) { + // expected + } + } + +} \ No newline at end of file