使用Ruby中的URI gem,我如何实际解析具有路径等的cookie?

时间:2017-06-03 23:45:32

标签: ruby-on-rails ruby cookies

当我从服务器返回一些cookie时,我只需要在下一个请求中传递key = value参数。我不需要路径信息,过期等。但是,当我检查哈希时,我得到了所有这些,并找不到真正的方法来解析并保留重要信息。

例如,这里是我的HTTP POST请求返回的cookie:

[1] pry(main)> headers['Cookie']
=> "ds_user=testuser; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, rur=ATN; Path=/, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm; expires=Sat, 02-Jun-2018 22:20:55 GMT; Max-Age=31449600; Path=/; Secure, mid=WTM2RwABAAHw09zRkX55hIbjqszz; expires=Fri, 29-May-2037 22:20:55 GMT; Max-Age=630720000; Path=/, uid=554580241; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D; expires=Fri, 01-Sep-2017 22:20:55 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure"

我无法执行headers['Cookie'].split("; ")之类的操作,因为分号(;)出现在标题中返回的Cookie的随机部分。

例如,当我使用从该请求返回的cookie提交下一个请求时,它需要看起来像:

Cookie: csrftoken=hgsdco49G6E1EOyR48ofKhtG0P; ds_user=testuser; uid=55458041; mid=WTIRigABAAFjQJOKtJ3jaWGbi70P; rur=ATN; sessionid=a9c568a15f567fb8bf9752a3%3AIMAaCgikVRROWcqGu6aoBhJTx5VGutKh%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3AyvzFW559ItJDPMOY9CyBja4NfW049qxy%3A4c5e7c48a20d3c8a1cbc4d0f9a0631449ff24c9b1dbc35a0f80f50568d0a3365%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496453514.4971137047%7D

知道最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

认为cookie是由";"并且在路径下用逗号分隔,您可以按照以下方式继续工作:

pry(main)> cookie = headers['Cookie']
=> "ds_user=testuser; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, rur=ATN; Path=/, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm; expires=Sat, 02-Jun-2018 22:20:55 GMT; Max-Age=31449600; Path=/; Secure, mid=WTM2RwABAAHw09zRkX55hIbjqszz; expires=Fri, 29-May-2037 22:20:55 GMT; Max-Age=630720000; Path=/, uid=554580241; expires=Fri, 01-Sep-2017 22:20:55 GMT; Max-Age=7776000; Path=/, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D; expires=Fri, 01-Sep-2017 22:20:55 GMT; HttpOnly; Max-Age=7776000; Path=/; Secure"

pry(main)> cookie.split(";").each do |f|
        f =~ /^(.*?)=(.*?)$/
      if $1 == " Path" then
        puts "There is a #{$1} that contains #{$2}"
      else
       puts "#{$1} = #{$2}"
      end
    end

pry(main)* ds_user = testuser
 expires = Fri, 01-Sep-2017 22:20:55 GMT
 Max-Age = 7776000
There is a  Path that contains /, rur=ATN
There is a  Path that contains /, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm
 expires = Sat, 02-Jun-2018 22:20:55 GMT
 Max-Age = 31449600
There is a  Path that contains /
 Secure, mid = WTM2RwABAAHw09zRkX55hIbjqszz
 expires = Fri, 29-May-2037 22:20:55 GMT
 Max-Age = 630720000
There is a  Path that contains /, uid=554580241
 expires = Fri, 01-Sep-2017 22:20:55 GMT
 Max-Age = 7776000
There is a  Path that contains /, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D
 expires = Fri, 01-Sep-2017 22:20:55 GMT
 = 
 Max-Age = 7776000
There is a  Path that contains /
 = 

=> ["ds_user=testuser",
 " expires=Fri, 01-Sep-2017 22:20:55 GMT",
 " Max-Age=7776000",
 " Path=/, rur=ATN",
 " Path=/, csrftoken=V6pcoCJdHJLb7BGu2BV8TwE5ZoA5fm",
 " expires=Sat, 02-Jun-2018 22:20:55 GMT",
 " Max-Age=31449600",
 " Path=/",
 " Secure, mid=WTM2RwABAAHw09zRkX55hIbjqszz",
 " expires=Fri, 29-May-2037 22:20:55 GMT",
 " Max-Age=630720000",
 " Path=/, uid=554580241",
 " expires=Fri, 01-Sep-2017 22:20:55 GMT",
 " Max-Age=7776000",
 " Path=/, sessionid=4be2b3d03caf0f3073ce8f1215a2d723a23981a52b0c%3AZ0Y0P0yRXSvpy2gBwZguWo4zoOpGajRA%3A%7B%22_auth_user_id%22%3A5545802841%2C%22_auth_user_backend%22%3A%22accounts.backends.CaseInsensitiveModelBackend%22%2C%22_auth_user_hash%22%3A%22%22%2C%22_token_ver%22%3A2%2C%22_token%22%3A%225545802841%3Ai2MlZYtSByn9IVBMuL8AHkCq0fX4HI7N%3A480fd4a05bfcdd081c70d0c4c4391883064a35cdd43402506937e611590d92a2%22%2C%22_platform%22%3A1%2C%22last_refreshed%22%3A1496528455.4560296535%7D",
 " expires=Fri, 01-Sep-2017 22:20:55 GMT",
 " HttpOnly",
 " Max-Age=7776000",
 " Path=/",
 " Secure"]

小心后的空间;你继续走在路径里面