Net::HTTPResponse extracting name=value from "set-cookies"

Hi!

I have this String, gotten from the set-cookies value of an HTTPResponse (from a post call):

KEY1=VALUE1; Path=/, KEY2=VALUE2; Domain=.somedomain.net; Expires=Mon, 24-Jan-2005 19:40:10 GMT;
Path=/, KEY3=VALUE3; Domain=e-messenger.net; Expires=Mon, 24-Jan-2005 19:40:10 GMT; Path=/

How can I extract each KEY,VALUE pair? I'm currently TRYING to use the scan String method, but I've
never been that good with regular expressions... any help?

Bye and thanks!

- --
Arturo "Buanzo" Busleiman - www.buanzo.com.ar - GNU/Linux Documentation
President, Open Information System Security Group - Argentina

In the darkest night, If my memory serves me right, I'll never turn back
time, Forgetting you, but not the time (Green Day, Whatsername)

Arturo 'Buanzo' Busleiman wrote:

I have this String, gotten from the set-cookies value of an HTTPResponse (from a post call):

KEY1=VALUE1; Path=/, KEY2=VALUE2; Domain=.somedomain.net; Expires=Mon, 24-Jan-2005 19:40:10 GMT;
Path=/, KEY3=VALUE3; Domain=e-messenger.net; Expires=Mon, 24-Jan-2005 19:40:10 GMT; Path=/

How can I extract each KEY,VALUE pair? I'm currently TRYING to use the scan String method, but I've
never been that good with regular expressions... any help?

input = 'KEY1=VALUE1; Path=/, KEY2=VALUE2; Domain=.somedomain.net; ' +
'Expires=Mon, 24-Jan-2005 19:40:10 GMT; Path=/, KEY3=VALUE3; ' +
'Domain=e-messenger.net; Expires=Mon, 24-Jan-2005 19:40:10 GMT; Path=/'

pkv = Regexp.new(';?\s*(?:Path=([^,]+),?)?\s*(?:([^=]+)=([^;]+))?')
input.scan(pkv) do |path, key, value|
   puts "path=#{path} key=#{key} value=#{value}"
end

Produces:

path= key=KEY1 value=VALUE1
path=/ key=KEY2 value=VALUE2
path= key=Domain value=.somedomain.net
path= key=Expires value=Mon, 24-Jan-2005 19:40:10 GMT
path=/ key=KEY3 value=VALUE3
path= key=Domain value=e-messenger.net
path= key=Expires value=Mon, 24-Jan-2005 19:40:10 GMT
path=/ key= value=
path= key= value=

The last line is annoying, but hard to fix. Since you can have either Path alone, or Key=Value alone, or both together, it's hard to avoid matching the fourth option, which is neither Path nor Key=Value, i.e. the empty string. That's what I get at the end of the scan, but it's easy to ignore that case.

···

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoil.com/&gt;

Glenn Parker wrote:

pkv = Regexp.new(';?\s*(?:Path=([^,]+),?)?\s*(?:([^=]+)=([^;]+))?')
input.scan(pkv) do |path, key, value|
  puts "path=#{path} key=#{key} value=#{value}"
end

Whoa! Thank you very much! Now I'll try to understand that regexp, and learn from it. Thanks for your precious time :slight_smile:

···

--
Arturo "Buanzo" Busleiman - www.buanzo.com.ar - GNU/Linux Documentation
President, Open Information System Security Group - Argentina

In the darkest night, If my memory serves me right, I'll never turn back
time, Forgetting you, but not the time (Green Day, Whatsername)