Ruby CGI :: Cookie raw cookie parsing

I want to be able to parse raw cookie strings in ruby ​​1.9.3p194 (hotfix 2012-04-20 version 35410) [x86_64-darwin10.8.0].

The CGI :: Cookie library looked promising, however it does not work as I thought it might be.

For instance,

CGI::Cookie::parse("ASPSESSIONIDSCDRSRTS=HHALOHOBJGJMLPIANNLDOMCJ; path=/").each_key {|name| p 'Cookie name: ' + name} 

Will return:

 "Cookie name: ASPSESSIONIDSCDRSRTS" "Cookie name: path" 

I would like something like a CGI::new instance to work, except that you pass it a raw cookie string:

 cookie1 = CGI::Cookie::new('name' => 'name', 'value' => ['value1', 'value2', ...], 'path' => 'path', # optional 'domain' => 'domain', # optional 'expires' => Time.now, # optional 'secure' => true # optional ) name = cookie1.name values = cookie1.value path = cookie1.path domain = cookie1.domain expires = cookie1.expires secure = cookie1.secure 

I cannot figure out how to do this eloquently from the raw cookie line.

EDIT ---

The following code is in the ~/.rvm/src/ruby-1.9.3-p194/lib/cgi/cookie.rb . Therefore, it should return as follows cookies[name] = Cookie::new(name, *values) . It seems like I'm not getting it.

 # Parse a raw cookie string into a hash of cookie-name=>Cookie # pairs. # # cookies = CGI::Cookie::parse("raw_cookie_string") # # { "name1" => cookie1, "name2" => cookie2, ... } # def Cookie::parse(raw_cookie) cookies = Hash.new([]) return cookies unless raw_cookie raw_cookie.split(/[;,]\s?/).each do |pairs| name, values = pairs.split('=',2) next unless name and values name = CGI::unescape(name) values ||= "" values = values.split('&').collect{|v| CGI::unescape(v,@@accept_charset) } if cookies.has_key?(name) values = cookies[name].value + values end cookies[name] = Cookie::new(name, *values) end cookies end 

EDIT ---

This seems like an error in the Ruby CGI::Cookie.parse . I opened an error report in the Ruby tracker buffer - https://bugs.ruby-lang.org/issues/7364

+4
source share
2 answers

I had the same problem. CGI::Cookie.new simply not able to consume what CGI::Cookie::parse produces; they simply do not complement each other as we expected.

A search through gems quickly found https://github.com/dwaite/cookiejar . This pearl seems to be trying to do a lot, but at least it has the code to correctly analyze raw cookies, as we expect. I did not try to understand the library, because it seems that it is so much, and in fact I am only worried that "eating a cookie → modiyfing it → produces a cookie".

I came up with this quick hack:

 require 'cgi' require 'cookiejar' # Some sample raw_cookie = "somecookie=some%7Cvalue; expires=Wed, 02 Apr 2014 13:36:50 GMT; path=/; domain=.somedomain.com" parts = CookieJar::CookieValidation.parse_set_cookie raw_cookie # Needs manual unescape parts[:value] = CGI::unescape(parts[:value]) # Per spec, this name is different parts[:expires] = parts[:expires_at] # Remove old ones parts.delete :expires_at # CookieJar adds them always, remove parts.delete :version # Convert symbol keys to strings for GGI::Cookie cookie = parts.inject(Hash.new) do |acc, (k,v)| acc[k.to_s] = v acc end puts CGI::Cookie.new(cookie).to_s #=> somecookie=some%7Cvalue; domain=.somedomain.com; path=/; expires=Wed, 02 Apr 2014 13:36:50 GMT 

I guess this should be a call for a gem that does only one thing with cookies and does it well: the ability to parse and generate raw cookie strings. But TBH, the problem area, is probably a lot harder than tricking me like me.

+2
source

I did not use the library, but according to the Ruby docs ( http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI/Cookie.html ) that you call | name | in your block is actually a cookie.

 parse(raw_cookie) Parse a raw cookie string into a hash of cookie-name=>Cookie pairs. 

You probably just see the name printed as that implicit .to_s will give you a cookie.

Instead, try typing the name .path or name.value (or any of the accessories on the cookie).

+1
source

Source: https://habr.com/ru/post/1446254/


All Articles