Index: java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v retrieving revision 1.7 diff -u -r1.7 NetscapeDraftSpec.java --- java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java 28 Jan 2003 04:40:23 -0000 1.7 +++ java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java 18 Jul 2003 21:46:42 -0000 @@ -69,6 +69,8 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; + +import org.apache.commons.httpclient.HeaderElement; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.Cookie; @@ -94,6 +96,93 @@ /** Default constructor */ public NetscapeDraftSpec() { super(); + } + + /** + * Parses the Set-Cookie value into an array of Cookies. + * + *
Syntax of the Set-Cookie HTTP Response Header:
+ * + *This is the format a CGI script would use to add to + * the HTTP headers a new piece of data which is to be stored by + * the client for later retrieval.
+ * + *+ * Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure + *+ * + *
Please note that Netscape draft specification does not fully + * conform to the HTTP header format. Netscape draft does not specify + * whether multiple cookies may be sent in one header. Hence, comma + * character may be present in unquoted cookie value or unquoted + * parameter value.
+ * + * @link http://wp.netscape.com/newsref/std/cookie_spec.html + * + * @param host the host from which the Set-Cookie value was + * received + * @param port the port from which the Set-Cookie value was + * received + * @param path the path from which the Set-Cookie value was + * received + * @param secure true when the Set-Cookie value was + * received over secure conection + * @param header the Set-Cookie received from the server + * @return an array of Cookies parsed from the Set-Cookie value + * @throws MalformedCookieException if an exception occurs during parsing + */ + public Cookie[] parse(String host, int port, String path, + boolean secure, final String header) + throws MalformedCookieException { + + LOG.trace("enter NetscapeDraftSpec.parse(String, port, path, boolean, Header)"); + + if (host == null) { + throw new IllegalArgumentException("Host of origin may not be null"); + } + if (host.trim().equals("")) { + throw new IllegalArgumentException("Host of origin may not be blank"); + } + if (port < 0) { + throw new IllegalArgumentException("Invalid port: " + port); + } + if (path == null) { + throw new IllegalArgumentException("Path of origin may not be null."); + } + if (header == null) { + throw new IllegalArgumentException("Header may not be null."); + } + + if (path.trim().equals("")) { + path = PATH_DELIM; + } + host = host.toLowerCase(); + + String defaultPath = path; + int lastSlashIndex = defaultPath.lastIndexOf(PATH_DELIM); + if (lastSlashIndex >= 0) { + if (lastSlashIndex == 0) { + //Do not remove the very first slash + lastSlashIndex = 1; + } + defaultPath = defaultPath.substring(0, lastSlashIndex); + } + HeaderElement headerelement = new HeaderElement(header.toCharArray()); + Cookie cookie = new Cookie(host, + headerelement.getName(), + headerelement.getValue(), + defaultPath, + null, + false); + // cycle through the parameters + NameValuePair[] parameters = headerelement.getParameters(); + // could be null. In case only a header element and no parameters. + if (parameters != null) { + for (int j = 0; j < parameters.length; j++) { + parseAttribute(parameters[j], cookie); + } + } + return new Cookie[] {cookie}; } Index: test/org/apache/commons/httpclient/TestCookie.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java,v retrieving revision 1.22 diff -u -r1.22 TestCookie.java --- test/org/apache/commons/httpclient/TestCookie.java 12 Jun 2003 19:12:16 -0000 1.22 +++ test/org/apache/commons/httpclient/TestCookie.java 18 Jul 2003 21:46:49 -0000 @@ -65,6 +65,7 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; + import java.util.Date; import java.util.Vector; import java.util.SortedSet; @@ -999,6 +1000,29 @@ assertEquals("$Version=0; name=; $Domain=.whatever.com; $Path=/", s); } + + /** + * Tests if cookie values with embedded comma are handled correctly. + */ + public void testCookieWithComma() throws Exception { + CookieSpec parser = null; + Cookie[] cookies = null; + Header header = new Header("Set-Cookie", "a=b,c"); + + parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109); + cookies = parser.parse("localhost", 80, "/", false, header); + assertEquals("number of cookies", 2, cookies.length); + assertEquals("a", cookies[0].getName()); + assertEquals("b", cookies[0].getValue()); + assertEquals("c", cookies[1].getName()); + assertEquals(null, cookies[1].getValue()); + + parser = CookiePolicy.getSpecByPolicy(CookiePolicy.NETSCAPE_DRAFT); + cookies = parser.parse("localhost", 80, "/", false, header); + assertEquals("number of cookies", 1, cookies.length); + assertEquals("a", cookies[0].getName()); + assertEquals("b,c", cookies[0].getValue()); + } }