Author: markt
Date: Tue Jul 24 19:19:40 2007
New Revision: 559283

URL: http://svn.apache.org/viewvc?view=rev&rev=559283
Log:
Port changes from 5.5

Modified:
    
tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/ServerCookie.java

Modified: 
tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/ServerCookie.java
URL: 
http://svn.apache.org/viewvc/tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/ServerCookie.java?view=diff&rev=559283&r1=559282&r2=559283
==============================================================================
--- 
tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/ServerCookie.java
 (original)
+++ 
tomcat/connectors/branches/tc5.0.x/util/java/org/apache/tomcat/util/http/ServerCookie.java
 Tue Jul 24 19:19:40 2007
@@ -123,6 +123,7 @@
     //
     // private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
     private static final String tspecials = ",;";
+    private static final String tspecials2 = ",; \"";
 
     /*
      * Tests a string and returns true if the string counts as a
@@ -147,6 +148,20 @@
        return true;
     }
 
+    public static boolean isToken2(String value) {
+        if( value==null) return true;
+        int len = value.length();
+
+        for (int i = 0; i < len; i++) {
+            char c = value.charAt(i);
+
+            if (c < 0x20 || c >= 0x7f || tspecials2.indexOf(c) != -1)
+                return false;
+        }
+        return true;
+    }
+
+
     public static boolean checkName( String name ) {
        if (!isToken(name)
                || name.equalsIgnoreCase("Comment")     // rfc2019
@@ -206,7 +221,7 @@
         // this part is the same for all cookies
        buf.append( name );
         buf.append("=");
-        maybeQuote(version, buf, value);
+        maybeQuote2(version, buf, value);
 
        // XXX Netscape cookie: "; "
        // add version 1 specific information
@@ -276,16 +291,56 @@
                throw new IllegalArgumentException( value );
            else {
                buf.append ('"');
-               buf.append (value);
+               buf.append (escapeDoubleQuotes(value));
                buf.append ('"');
            }
        }
     }
 
+    public static void maybeQuote2 (int version, StringBuffer buf,
+            String value) {
+        // special case - a \n or \r  shouldn't happen in any case
+        if (isToken2(value)) {
+            buf.append(value);
+        } else {
+            buf.append('"');
+            buf.append(escapeDoubleQuotes(value));
+            buf.append('"');
+        }
+    }
+
+
     // log
     static final int dbg=1;
     public static void log(String s ) {
        System.out.println("ServerCookie: " + s);
+    }
+
+    /**
+     * Escapes any double quotes in the given string.
+     *
+     * @param s the input string
+     *
+     * @return The (possibly) escaped string
+     */
+    private static String escapeDoubleQuotes(String s) {
+
+        if (s == null || s.length() == 0 || s.indexOf('"') == -1) {
+            return s;
+        }
+
+        StringBuffer b = new StringBuffer();
+        char p = s.charAt(0);
+        for (int i = 0; i < s.length(); i++) {
+            char c = s.charAt(i);
+            if (c == '"' && p != '\\')
+                b.append('\\').append('"');
+            else
+                b.append(c);
+            p = c;
+        }
+
+        return b.toString();
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to