Changelog:
- In non-strict mode each cookie sent with the request is put on a
separate request header.
- In strict mode all cookies are crammed into one request header, as
before
The patch requires the test webapp to be recompiled & redeployed
It's a fairly minor change blessed by Jandalf long time ago. I'll commit
this patch later today or tomorrow if nobody loudly complains.
Your cookie monster
Index: java/org/apache/commons/httpclient/HttpMethodBase.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
retrieving revision 1.113
diff -u -r1.113 HttpMethodBase.java
--- java/org/apache/commons/httpclient/HttpMethodBase.java 16 Feb 2003 17:56:17 -0000 1.113
+++ java/org/apache/commons/httpclient/HttpMethodBase.java 17 Feb 2003 19:49:53 -0000
@@ -1317,7 +1317,17 @@
Cookie[] cookies = matcher.match(conn.getHost(), conn.getPort(),
getPath(), conn.isSecure(), state.getCookies());
if ((cookies != null) && (cookies.length > 0)) {
- setRequestHeader(matcher.formatCookieHeader(cookies));
+ if (this.isStrictMode()) {
+ // In strict mode put all cookies on the same header
+ getRequestHeaderGroup().addHeader(
+ matcher.formatCookieHeader(cookies));
+ } else {
+ // In non-strict mode put each cookie on a separate header
+ for (int i = 0; i < cookies.length; i++) {
+ getRequestHeaderGroup().addHeader(
+ matcher.formatCookieHeader(cookies[i]));
+ }
+ }
}
}
Index: java/org/apache/commons/httpclient/cookie/CookieSpecBase.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java,v
retrieving revision 1.11
diff -u -r1.11 CookieSpecBase.java
--- java/org/apache/commons/httpclient/cookie/CookieSpecBase.java 30 Jan 2003 05:01:55 -0000 1.11
+++ java/org/apache/commons/httpclient/cookie/CookieSpecBase.java 17 Feb 2003 19:49:55 -0000
@@ -375,8 +375,8 @@
+ paramValue);
}
} else {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Unrecognized cookie attribute: "
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Unrecognized cookie attribute: "
+ attribute.toString());
}
}
Index: test/org/apache/commons/httpclient/TestWebappCookie.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappCookie.java,v
retrieving revision 1.9
diff -u -r1.9 TestWebappCookie.java
--- test/org/apache/commons/httpclient/TestWebappCookie.java 2 Feb 2003 11:05:20 -0000 1.9
+++ test/org/apache/commons/httpclient/TestWebappCookie.java 17 Feb 2003 19:49:46 -0000
@@ -109,6 +109,7 @@
public void testSetCookieGet() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
GetMethod method = new GetMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set");
@@ -129,6 +130,7 @@
public void testSetCookiePost() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
PostMethod method = new PostMethod("/" + context + "/cookie/write");
method.setRequestBody(new NameValuePair[] { new NameValuePair("simple","set") } );
@@ -149,6 +151,7 @@
public void testSetCookiePut() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
PutMethod method = new PutMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set");
@@ -169,6 +172,7 @@
public void testSetExpiredCookieGet() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
GetMethod method = new GetMethod("/" + context + "/cookie/write");
method.setQueryString("simple=unset");
@@ -187,6 +191,7 @@
public void testSetExpiredCookiePut() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
PutMethod method = new PutMethod("/" + context + "/cookie/write");
method.setQueryString("simple=unset");
@@ -205,6 +210,7 @@
public void testSetUnsetCookieGet() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
GetMethod method = new GetMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set");
@@ -238,8 +244,9 @@
assertEquals(0,client.getState().getCookies().length);
}
- public void testSetMultiCookieGet() throws Exception {
+ public void testSetMultiCookieGetStrict() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
GetMethod method = new GetMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set&domain=set");
@@ -261,8 +268,48 @@
assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue());
}
+
+ public void testMultiSendCookieGetNonstrict() throws Exception {
+ HttpClient client = new HttpClient();
+ client.getHostConfiguration().setHost(host, port, "http");
+ GetMethod method = new GetMethod("/" + context + "/cookie/write");
+ method.setQueryString("simple=set&domain=set");
+ try {
+ client.executeMethod(method);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Unable to execute method : " + t.toString());
+ }
+ assertEquals(200,method.getStatusCode());
+ assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0);
+ assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0);
+ assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0);
+ assertEquals(2,client.getState().getCookies().length);
+ assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName());
+ assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue());
+ assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName());
+ assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue());
+
+ GetMethod method2 = new GetMethod("/" + context + "/cookie/read");
+ try {
+ client.executeMethod(method2);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ fail("Unable to execute method : " + t.toString());
+ }
+ assertEquals(200,method2.getStatusCode());
+ String s = method2.getResponseBodyAsString();
+ assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0);
+ assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"</tt></p>") >= 0);
+ assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; domaincookie=\"value\"; $Domain=\"" + host + "\"</tt></p>") >= 0);
+ assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0);
+ assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0);
+ }
+
+
public void testSetMultiCookiePut() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
PutMethod method = new PutMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set&domain=set");
@@ -286,6 +333,7 @@
public void testSendCookieGet() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
GetMethod method = new GetMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set");
@@ -320,6 +368,7 @@
public void testMultiSendCookieGet() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
GetMethod method = new GetMethod("/" + context + "/cookie/write");
method.setQueryString("simple=set&domain=set");
@@ -358,6 +407,7 @@
public void testDeleteCookieGet() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
{
@@ -435,6 +485,7 @@
public void testDeleteCookiePut() throws Exception {
HttpClient client = new HttpClient();
+ client.setStrictMode(true);
client.getHostConfiguration().setHost(host, port, "http");
{
@@ -655,7 +706,7 @@
}
assertEquals(200,method.getStatusCode());
assertTrue(method.getResponseBodyAsString().indexOf("<title>ReadCookieServlet: GET</title>") >= 0);
- assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<p><tt>Cookie: null</tt></p>") >= 0);
+ assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<p><tt>Cookie: ") == -1);
assertTrue(method.getResponseBodyAsString().indexOf("<tt>pathcookie=value</tt><br>") == -1);
}
}
Index: test-webapp/src/org/apache/commons/httpclient/ReadCookieServlet.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient/ReadCookieServlet.java,v
retrieving revision 1.3
diff -u -r1.3 ReadCookieServlet.java
--- test-webapp/src/org/apache/commons/httpclient/ReadCookieServlet.java 23 Jan 2003 22:48:49 -0000 1.3
+++ test-webapp/src/org/apache/commons/httpclient/ReadCookieServlet.java 17 Feb 2003 19:49:43 -0000
@@ -76,7 +76,10 @@
out.println("<head><title>ReadCookieServlet: " + request.getMethod() + "</title></head>");
out.println("<body>");
out.println("<p>This is a response to an HTTP " + request.getMethod() + " request.</p>");
- out.println("<p><tt>Cookie: " + request.getHeader("Cookie") + "</tt></p>");
+ Enumeration enum = request.getHeaders("Cookie");
+ while (enum.hasMoreElements()) {
+ out.println("<p><tt>Cookie: " + (String)enum.nextElement() + "</tt></p>");
+ }
Cookie[] cookies = request.getCookies();
if(null != cookies) {
for(int i=0;i<cookies.length;i++) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]