1

I try to make a cookie's value generated from the server side (in Java) identical to a cookie's value generated from this jquery's cookies librarie, which use :

encodeURIComponent( value )

to encode the cookie value.

Let's take this example string :

a b,c;d*~'"()!±

I use this code, in Java, to encode a cookie value :

String result = URLEncoder.encode(str, "UTF-8")
                          .replaceAll("\\+", "%20")
                          .replaceAll("\\%21", "!")
                          .replaceAll("\\%27", "'")
                          .replaceAll("\\%28", "(")
                          .replaceAll("\\%29", ")")
                          .replaceAll("\\%7E", "~");

I then add the cookie to the response :

Cookie cookie = new Cookie("testCookieServerSide", encodeValue("a b,c;d*~'\"()!±"));
response.addCookie(cookie);

This adds double-quotes around the value! :

"a%20b%2Cc%3Bd*~'%22()!%C2%B1"

When encoding the same value using the jquery library, I get the same value but without the double-quotes :

a%20b%2Cc%3Bd*~'%22()!%C2%B1

Is it possible to tell Java to not generate the double-quotes? I've heard of cookie.setVersion(0) or cookie.setVersion(1) but this doesn't seem to change anything.

When retrieving those two values on the server-side, they are identical after a :

URLDecoder.decode(cookie.getValue(), "UTF-8");

... the double-quotes are removed. Great.

But on the client side, the two values are differents!

alert($.cookies.get('testCookieServerSide')); => "a b,c;d*~'"()!±"
alert($.cookies.get('testCookieClientSide')); => a b,c;d*~'"()!±

Any idea on how to generate the exact same value? How do you deal with this?

Community
  • 1
  • 1
electrotype
  • 8,342
  • 11
  • 59
  • 96
  • This is a shot in the dark, but what happens if you try encoding/decoding the string value first (using functions like the ones suggested here: http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html ) I know it's old, but that's all I saw for emulating encoding/decoding of UTF8 in javascript. – Jeff Tratner Aug 20 '12 at 00:31
  • Jeff, thanks for the help. The problem is not how the characters are encoded, this is identical on both side. The problem is that Java adds extra double-quotes around the value (when there are special characters). – electrotype Aug 20 '12 at 11:09
  • 1
    The answer you can find here: [http://stackoverflow.com/questions/14582334/java-cookies-and-double-quoted-marks][1] [1]: http://stackoverflow.com/questions/14582334/java-cookies-and-double-quoted-marks – robson Jan 29 '13 at 12:40

1 Answers1

1

Here's the fix I found :

I didn't find any way to tell Java to stop adding those double-quotes, so I have to live with them.

I modified the jquery's cookies library I use, so instead of decoding the cookie value like this :

value = decodeURIComponent( pair[1] );

It uses:

value = decodeURIComponent( pair[1].replace( /^"(.*)"$/, '$1' ) );

When this line of code is reached, the value in the cookie is still encoded (the js library itself encode it on the client-side and I encode it using Java on the server-side, using the above code). So the double-quotes, at the begining and end of the value, if not encoded, are necessarily characters added by Java... They have to be removed when getting the value.

electrotype
  • 8,342
  • 11
  • 59
  • 96