[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Arturo 'Buanzo' Busleiman

12/25/2004 7:49:00 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail....

iD8DBQFBzcQiAlpOsGhXcE0RAtpAAJ9Bije4yVbx18NkWueTZKiUq3c5AwCeI5vW
ZBkt+G9EOQJiX4hn9p/msUY=
=Jr2a
-----END PGP SIGNATURE-----
2 Answers

Glenn Parker

12/26/2004 4:14:00 AM

0

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.tetrafoi...


Arturo 'Buanzo' Busleiman

12/26/2004 12:56:00 PM

0

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 :)

--
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)