hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1418654 - in /httpcomponents/httpclient/trunk: ./ httpclient/src/main/java/org/apache/http/impl/cookie/ httpclient/src/test/java/org/apache/http/impl/cookie/
Date Sat, 08 Dec 2012 12:12:13 GMT
Author: olegk
Date: Sat Dec  8 12:12:12 2012
New Revision: 1418654

URL: http://svn.apache.org/viewvc?rev=1418654&view=rev
Log:
HTTPCLIENT-1263: BrowserCompatSpec: attribute values containing spaces or special characters
should be enclosed with quotes marks for version 1 cookies
Contributed by Francois-Xavier Bonnet <francois-xavier.bonnet at centraliens.net>

Added:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1418654&r1=1418653&r2=1418654&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Sat Dec  8 12:12:12 2012
@@ -1,6 +1,10 @@
 Changes in trunk
 -------------------
 
+* [HTTPCLIENT-1263] BrowserCompatSpec: attribute values containing spaces or special characters

+  should be enclosed with quotes marks for version 1 cookies.
+  Contributed by Francois-Xavier Bonnet <francois-xavier.bonnet at centraliens.net>

+
 * [HTTPCLIENT-1266] NTLM engine refactoring and compatibility improvements.
   Contributed by Karl Wright <DaddyWri at gmail.com>  
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java?rev=1418654&r1=1418653&r2=1418654&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java
Sat Dec  8 12:12:12 2012
@@ -40,6 +40,8 @@ import org.apache.http.cookie.Cookie;
 import org.apache.http.cookie.CookieOrigin;
 import org.apache.http.cookie.MalformedCookieException;
 import org.apache.http.cookie.SM;
+import org.apache.http.message.BasicHeaderElement;
+import org.apache.http.message.BasicHeaderValueFormatter;
 import org.apache.http.message.BufferedHeader;
 import org.apache.http.message.ParserCursor;
 import org.apache.http.util.CharArrayBuffer;
@@ -89,6 +91,7 @@ public class BrowserCompatSpec extends C
         registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
         registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
                 this.datepatterns));
+        registerAttribHandler(ClientCookie.VERSION_ATTR, new BrowserCompatVersionAttributeHandler());
     }
 
     /** Default constructor */
@@ -160,11 +163,19 @@ public class BrowserCompatSpec extends C
             if (i > 0) {
                 buffer.append("; ");
             }
-            buffer.append(cookie.getName());
-            buffer.append("=");
-            String s = cookie.getValue();
-            if (s != null) {
-                buffer.append(s);
+            if (cookie.getVersion() > 0) {
+                BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(
+                        buffer,
+                        new BasicHeaderElement(cookie.getName(), cookie.getValue()),
+                        false);
+            } else {
+                // Netscape style cookies do not support quoted values
+                buffer.append(cookie.getName());
+                buffer.append("=");
+                String s = cookie.getValue();
+                if (s != null) {
+                    buffer.append(s);
+                }
             }
         }
         List<Header> headers = new ArrayList<Header>(1);

Added: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java?rev=1418654&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
Sat Dec  8 12:12:12 2012
@@ -0,0 +1,67 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.cookie;
+
+import org.apache.http.annotation.Immutable;
+import org.apache.http.cookie.MalformedCookieException;
+import org.apache.http.cookie.SetCookie;
+
+/**
+ * <tt>"Version"</tt> cookie attribute handler for BrowserCompat cookie spec.
+ *
+ * @since 4.3
+ */
+@Immutable
+public class BrowserCompatVersionAttributeHandler extends
+		AbstractCookieAttributeHandler {
+
+	public BrowserCompatVersionAttributeHandler() {
+		super();
+	}
+
+	/**
+	 * Parse cookie version attribute.
+	 */
+	public void parse(final SetCookie 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 version attribute");
+		}
+		int version = 0;
+		try {
+			version = Integer.parseInt(value);
+		} catch (NumberFormatException e) {
+			// Just ignore invalid versions
+		}
+		cookie.setVersion(version);
+	}
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatVersionAttributeHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java?rev=1418654&r1=1418653&r2=1418654&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java
Sat Dec  8 12:12:12 2012
@@ -94,7 +94,7 @@ public class TestBrowserCompatSpec {
         Assert.assertEquals("custno", cookies.get(0).getName());
         Assert.assertEquals("12345", cookies.get(0).getValue());
         Assert.assertEquals("test", cookies.get(0).getComment());
-        Assert.assertEquals(0, cookies.get(0).getVersion());
+        Assert.assertEquals(1, cookies.get(0).getVersion());
         Assert.assertEquals("www.apache.org", cookies.get(0).getDomain());
         Assert.assertEquals("/", cookies.get(0).getPath());
         Assert.assertFalse(cookies.get(0).isSecure());
@@ -102,7 +102,7 @@ public class TestBrowserCompatSpec {
         Assert.assertEquals("name", cookies.get(1).getName());
         Assert.assertEquals("John", cookies.get(1).getValue());
         Assert.assertEquals(null, cookies.get(1).getComment());
-        Assert.assertEquals(0, cookies.get(1).getVersion());
+        Assert.assertEquals(1, cookies.get(1).getVersion());
         Assert.assertEquals(".apache.org", cookies.get(1).getDomain());
         Assert.assertEquals("/", cookies.get(1).getPath());
         Assert.assertTrue(cookies.get(1).isSecure());
@@ -130,7 +130,7 @@ public class TestBrowserCompatSpec {
         Assert.assertEquals("custno", cookies.get(0).getName());
         Assert.assertEquals("12345", cookies.get(0).getValue());
         Assert.assertEquals("test", cookies.get(0).getComment());
-        Assert.assertEquals(0, cookies.get(0).getVersion());
+        Assert.assertEquals(1, cookies.get(0).getVersion());
         Assert.assertEquals("www.apache.org", cookies.get(0).getDomain());
         Assert.assertEquals("/", cookies.get(0).getPath());
         Assert.assertFalse(cookies.get(0).isSecure());
@@ -138,7 +138,7 @@ public class TestBrowserCompatSpec {
         Assert.assertEquals("name", cookies.get(1).getName());
         Assert.assertEquals("John", cookies.get(1).getValue());
         Assert.assertEquals(null, cookies.get(1).getComment());
-        Assert.assertEquals(0, cookies.get(1).getVersion());
+        Assert.assertEquals(1, cookies.get(1).getVersion());
         Assert.assertEquals(".apache.org", cookies.get(1).getDomain());
         Assert.assertEquals("/", cookies.get(1).getPath());
         Assert.assertTrue(cookies.get(1).isSecure());
@@ -166,7 +166,7 @@ public class TestBrowserCompatSpec {
         Assert.assertEquals("name", cookies.get(0).getName());
         Assert.assertEquals("Doe, John", cookies.get(0).getValue());
         Assert.assertEquals(null, cookies.get(0).getComment());
-        Assert.assertEquals(0, cookies.get(0).getVersion());
+        Assert.assertEquals(1, cookies.get(0).getVersion());
         Assert.assertEquals(".apache.org", cookies.get(0).getDomain());
         Assert.assertEquals("/", cookies.get(0).getPath());
         Assert.assertTrue(cookies.get(0).isSecure());
@@ -458,7 +458,7 @@ public class TestBrowserCompatSpec {
         Assert.assertTrue("Secure",cookies.get(0).isSecure());
         Assert.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate());
         Assert.assertEquals("Comment","This is a comment.",cookies.get(0).getComment());
-        Assert.assertEquals("Version",0,cookies.get(0).getVersion());
+        Assert.assertEquals("Version",1,cookies.get(0).getVersion());
     }
 
     @Test
@@ -998,4 +998,38 @@ public class TestBrowserCompatSpec {
         }
     }
 
+    /**
+     * Tests cookie version 1 with space in cookie value.
+     */
+    @Test
+    public void testFormatCookieWithSpaceInValue() throws Exception {
+        CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
+        CookieSpec cookieSpec = new BrowserCompatSpec();
+        Header setCookieHeader = new BasicHeader("Set-Cookie", "test=\"value 1\"; Version=1");
+        Cookie cookie = cookieSpec.parse(setCookieHeader, origin).get(0);
+        List<Cookie> cookies = new ArrayList<Cookie>();
+        cookies.add(cookie);
+        List<Header> headers = cookieSpec.formatCookies(cookies);
+        Assert.assertNotNull(headers);
+        Assert.assertEquals(1, headers.size());
+        Assert.assertEquals("test=\"value 1\"", headers.get(0).getValue());
+    }
+
+    /**
+     * Tests Netscape cookie with space in cookie value.
+     */
+    @Test
+    public void testFormatCookieVersion0WithSpaceInValue() throws Exception {
+        CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
+        CookieSpec cookieSpec = new BrowserCompatSpec();
+        Header setCookieHeader = new BasicHeader("Set-Cookie", "test=value 1");
+        Cookie cookie = cookieSpec.parse(setCookieHeader, origin).get(0);
+        List<Cookie> cookies = new ArrayList<Cookie>();
+        cookies.add(cookie);
+        List<Header> headers = cookieSpec.formatCookies(cookies);
+        Assert.assertNotNull(headers);
+        Assert.assertEquals(1, headers.size());
+        Assert.assertEquals("test=value 1", headers.get(0).getValue());
+    }
+
 }



Mime
View raw message