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 2003/07/19 10:46:59 UTC
cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestCookie.java
olegk 2003/07/19 01:46:59
Modified: httpclient/src/java/org/apache/commons/httpclient/cookie
NetscapeDraftSpec.java
httpclient/src/test/org/apache/commons/httpclient
TestCookie.java
Log:
Bug fix #11240 (Cookies with ',' in the value string is not parsed correctly in some cases)
Contributed by Oleg Kalnichevski
Reviewed by Michael Becke & Laura Werner
Revision Changes Path
1.8 +92 -3 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java
Index: NetscapeDraftSpec.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- NetscapeDraftSpec.java 28 Jan 2003 04:40:23 -0000 1.7
+++ NetscapeDraftSpec.java 19 Jul 2003 08:46:59 -0000 1.8
@@ -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 <tt>Cookie</tt>s.
+ *
+ * <p>Syntax of the Set-Cookie HTTP Response Header:</p>
+ *
+ * <p>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.</p>
+ *
+ * <PRE>
+ * Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
+ * </PRE>
+ *
+ * <p>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.</p>
+ *
+ * @link http://wp.netscape.com/newsref/std/cookie_spec.html
+ *
+ * @param host the host from which the <tt>Set-Cookie</tt> value was
+ * received
+ * @param port the port from which the <tt>Set-Cookie</tt> value was
+ * received
+ * @param path the path from which the <tt>Set-Cookie</tt> value was
+ * received
+ * @param secure <tt>true</tt> when the <tt>Set-Cookie</tt> value was
+ * received over secure conection
+ * @param header the <tt>Set-Cookie</tt> received from the server
+ * @return an array of <tt>Cookie</tt>s 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};
}
1.23 +28 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java
Index: TestCookie.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- TestCookie.java 12 Jun 2003 19:12:16 -0000 1.22
+++ TestCookie.java 19 Jul 2003 08:46:59 -0000 1.23
@@ -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());
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org