Cookie value in double quotes [duplicate]

2019-07-14 22:08发布

问题:

This question already has an answer here:

  • Why do cookie values with whitespace arrive at the client side with quotes? 4 answers

I have a application deployed on tomcat 6.0.33 that generates a cookie with forward slash(/) as one of the characters in cookie value. Now for some reason tomcat is enclosing the cookie value in double quotes whenever there is forward slash present. How can I change tomcat behavior to avoid putting double quotes whenever forward slash is present in my cookie value?

N.B. I do not agree with BaluC that this is a duplicate question. I did check the other one before I posted this one. I have specifically asked in this question as to how to change tomcat behavior to remove quotes. This is totally different from the other questions where the questioner is wondering why double quotes are introduced in the first place.

回答1:

I found that the issue is with the Tomcat version. The cookie processor encloses the cookie value in quotes when it encounters forward slash. I upgraded to Tomcat 7 and those quotes are gone. The rules on cookie value are relaxed in Tomcat 7.

I further discovered that even in Tomcat 7, if there is '=' in cookie value, Tomcat encloses the value in double quotes. I overcame this by further upgrading to Tomcat 8 and then changing it's cookie processor to Rfc6265CookieProcessor.



回答2:

You should use rawurlencode(), not urlencode() for escaping path parts. urlencode() is misnamed, it is actually for application/x-www-form-urlencoded data such as in the query string or the body of a POST request, and not for other parts of the URL.