Sign in to follow this  
Followers 0
BAM5

Disecting Set-Cookie header with StringRegExp (half-solved)

5 posts in this topic

#1 ·  Posted (edited)

I'm attempting to take the string a server returns in the Set-Cookie header and break it up into its bits and pieces and have come pretty close with this.

Set-Cookie string looks like this: "Name=value; expires=Thu, 02-Sep-2010 16:44:12 GMT; path=/ Name=value; expires=Thu, 02-Sep-2010 16:44:12 GMT; path=/ secure Name2=Value2; expires=Wed, 02-Sep-2010 16:44:12 GMT; path=/ HttpOnly name3=value3; expires=Thu, 02-Sep-2010 16:44:12 GMT; path=/ secure HttpOnly"

([^=]+)=([^;]+); (?:(path)=(\S+)\s?)?

This will work for all cookies without the HttpOnly and secure flag.

([^=]+)=([^;]+); (?:(path)=(\S+)\s?)?(?:(secure)?\s?|(HttpOnly)?\s?){0,2}

Is what I came up with for the flags, but for some reason it doesn't work.

Could some sre master out there point out the error of my ways?

Thanks

BAM5

Edited by BAM5

[center]JSON Encoding UDF[/center]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This pattern seems to do what you want in the example you give, but would probably need some sophistication to work with pathnames having whitespaces in them.

(?i)([^=]+)=([^;]+);\s([^=]+)=([^;]+);\s([^=]+)=([^ ]+)\s(secure)?\s?(HttpOnly)?\s*

(Use mode 3 of course)

EDIT: I forgot to mention that I take the presence of namexxx=yyy expires=ttt and path=zzz for obligatory.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Ah, namexxx=yyy expires=ttt and path=zzz are all required for cookies, but there are other values that can be added in optionally as well (such as domain and max-age), so unfortunately, your sre will not work for me.

A modification of the second pattern I posted almost works, except for when the httponly flag is up and secure isn't, it'll cause an blank element in the array.

Modification: ([^=]+)=([^;]+); (?:(path)=(\S+)\s?)?(?:(secure)\s?|(HttpOnly)\s?){0,2}


[center]JSON Encoding UDF[/center]

Share this post


Link to post
Share on other sites

After messing around a bit I figured out this pattern:

([^=]+)=([^;]+); (?:(path)=(\S+)\s?)?(secure|HttpOnly)?\s?(secure|HttpOnly)?\s?

It isn't exactly what I was looking for, but it works.


[center]JSON Encoding UDF[/center]

Share this post


Link to post
Share on other sites

The more optional entities are to be parsed (furthermore using varying syntax) the more complicated and potentially fragile your SRE will become.

If I had to do it myself, I wouldn't rely on a single SRE to do it entirely in one shot, as some new option/syntax is likely to make it fail almost anytime soon. I would dissect things with a more pedestrian approach. Except if you have millions of such cookies to routinely process, the added time spent (if any) is most certainly unnoticeable.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0