How can I disable URL-encoding of cookies in Rails

一笑奈何 提交于 2019-12-06 07:34:57

After doing some digging I have found the answer to my question and I am documenting it here in case it is of use to anyone else.

In order to turn off URL-encoding in Rails 2.3.2 it is necessary to edit the following file: actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/response.rb

Around line 70 the ID and value of the cookie is set. I made the following change to turn of URL-encoding:

cookie = Utils.escape(key) + "=" +
    #value.map { |v| Utils.escape v }.join("&") +
    value.map { |v| v }.join("&") +
    "#{domain}#{path}#{expires}#{secure}#{httponly}"

NOTE: This modification only affects standard cookies - not the cookies used as session data by Rails in version 2.3.2.

DISCLAIMER: I am in no way recommending this modification as a best practice. This modification was only done for the specific reason of handling legacy code requirement that required a cookie to be in a particular format. A better option would even be to modify the legacy code to handle URL-encoding. Unfortunately, that option was closed to me so I was forced to hack around on the underlying Rails code - which is not something I would generally recommend. Of course it should go without saying that making this type of modification runs the risk that the problem will have to be re-addressed every time you upgrade your Rails installation as the underlying code may change. That is actually what happened in my case. And of course there are also probably good reasons (security, standards compliance, etc.) for keeping the URL-encoding if at all possible.

user348163

A simple way to do this is using the rack method,

response["set-cookie"]="id_cookie=this cookie will be not escaped"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!