# CGI::Cookie.parse problem?

**URL:** <https://www.ruby-forum.com/t/cgi-cookie-parse-problem/97413>\
**Category:** Ruby\
**Created:** [January 25, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/cgi-cookie-parse-problem/97413 "2006-01-25T22:35:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [January 25, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/cgi-cookie-parse-problem/97413/1 "2006-01-25T22:35:59Z")

</div>

Hi,

I’m interfacing with a site that is sending me back a cookie like  
the following, in the HTTP ‘set-cookie’ field.

```auto
require 'cgi'

raw_cookie =
  "ID=867.5309; path=/; domain=site.com, " +
  "SESS_mem=deleted; expires=Mon, 24 Jan 2005 19:48:10 GMT; path=/; 
domain=.site.com, " +
  "SESS_mem=deleted; expires=Mon, 24 Jan 2005 19:48:10 GMT; path=/; 
domain=.site.com, " +
  "SESS_mem=xyzzy-plugh-plover; path=/; domain=.site.com"

k = CGI::Cookie.parse(raw_cookie)

p k

```

This program outputs:

{“expires”=\>[“Mon, 24 Jan 2005 19:48:10 GMT”],  
“domain”=\>[“[site.com](http://site.com), SESS\_mem=deleted”],  
“ID”=\>[“867.5309”], “path”=\>[“/”]}

Is this a reasonable way for this set-cookie data to be parsed? It  
seems  
that CGI::Cookie.parse() isn’t aware that there are multiple cookies  
specified in the data… It’s sort of giving a mixture of fields from  
the four separate cookies.

Looking at the RFC, [RFC 2965 - HTTP State Management Mechanism (RFC2965)](http://www.faqs.org/rfcs/rfc2965.html)  
it _seems_ to me the above raw\_cookie data, conforms to the spec:

Informally, the Set-Cookie2 response header comprises the token Set-  
Cookie2:, followed by a comma-separated list of one or more cookies.  
Each cookie begins with a NAME=VALUE pair, followed by zero or more  
semi-colon-separated attribute-value pairs.

Has anyone else run into this? I tried Perl’s CGI cookie parsing and  
it returned similarly garbled results for this set-cookie data.

So I’m wondering if it’s really a bug? Or if I’m somehow misusing  
CGI::Cookie.parse() , sending it the wrong format data, or whatever.

Thanks,

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [January 27, 2006, 6:44am UTC](https://www.ruby-forum.com/t/cgi-cookie-parse-problem/97413/2 "2006-01-27T06:44:29Z")

</div>

[replying to my own post]

> So I’m wondering if it’s really a bug? Or if I’m somehow misusing  
> CGI::Cookie.parse() , sending it the wrong format data, or whatever.

Ah… It seems the problem is I’m trying to parse server-\>client  
cookies, which are apparently of a different syntax than  
client-\>server cookies.

I’ve written my own simplistic ServerCookie parser to meet my  
current needs. But, anyone know if a ServerCookie parser already  
exists for Ruby?

Thanks,

Bill
