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

  • Thread starter Arturo 'Buanzo' Busleiman
  • Start date
A

Arturo 'Buanzo' Busleiman

--------------050108040907040507020608
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

-----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.mozdev.org

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

--------------050108040907040507020608
Content-Type: text/x-vcard; charset=utf-8;
name="buanzo.vcf"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="buanzo.vcf"

begin:vcard
fn:Arturo 'Buanzo' Busleiman
n:Busleiman;Arturo
org:OISSG
adr:;;;;BA;;Argentina
email;internet:[email protected]
x-mozilla-html:FALSE
url:http://www.buanzo.com.ar
version:2.1
end:vcard


--------------050108040907040507020608--
 
G

Glenn Parker

Arturo said:
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.
 
A

Arturo 'Buanzo' Busleiman

--------------000301060902010409000305
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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


--------------000301060902010409000305
Content-Type: text/x-vcard; charset=utf-8;
name="buanzo.vcf"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="buanzo.vcf"

begin:vcard
fn:Arturo 'Buanzo' Busleiman
n:Busleiman;Arturo
org:OISSG
adr:;;;;BA;;Argentina
email;internet:[email protected]
x-mozilla-html:FALSE
url:http://www.buanzo.com.ar
version:2.1
end:vcard


--------------000301060902010409000305--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top