Parsing cookie strings in Java with HttpCookie

The other day I was solving a very complex bug involving some sticky session cookies and multiple reverse proxies. During the bug solving process I discovered that I need to parse set-cookie header strings and do some value filtering in one of our reve…


This content originally appeared on DEV Community and was authored by Pavel Polívka

The other day I was solving a very complex bug involving some sticky session cookies and multiple reverse proxies. During the bug solving process I discovered that I need to parse set-cookie header strings and do some value filtering in one of our reverse proxies.

My first idea was to write some kind of regex that would parse the string and get me my desired values. I went with something like this:

(.*?)=(.*?)($|;|,(?! ))

Here is a regexer link.

Turns out this is more complex than simple regex. One string can contain multiple cookies, optional parameters, etc... Then there is an issue with multiple formats of how the cookie string can look like. I would need to write a lot of logic around my regex.

Naturally, I am a bit lazy so I started looking into what Java can offer. There must be an existing solution for this. I found a class named HttpCookie.

Usage is very simple:

List<HttpCookie> cookies = HttpCookie.parse(cookie);

It will parse all the cookies in the string into a collection of objects that have all the needed info.

 private final String name; // NAME= ... "$Name" style is reserved
 private String value; // value of NAME

 // Attributes encoded in the header's cookie fields.
 private String comment; // Comment=VALUE ... describes cookie's use
 private String commentURL; // CommentURL="http URL" ... describes cookie's use
 private boolean toDiscard; // Discard ... discard cookie unconditionally
 private String domain; // Domain=VALUE ... domain that sees cookie
 private long maxAge = MAX_AGE_UNSPECIFIED; // Max-Age=VALUE ... cookies auto-expire
 private String path; // Path=VALUE ... URLs that see the cookie
 private String portlist; // Port[="portlist"] ... the port cookie may be returned to
 private boolean secure; // Secure ... e.g. use SSL
 private boolean httpOnly; // HttpOnly ... i.e. not accessible to scripts
 private int version = 1; // Version=1 ... RFC 2965 style

This saved me a lot of time.


This content originally appeared on DEV Community and was authored by Pavel Polívka


Print Share Comment Cite Upload Translate Updates
APA

Pavel Polívka | Sciencx (2021-07-22T08:24:45+00:00) Parsing cookie strings in Java with HttpCookie. Retrieved from https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/

MLA
" » Parsing cookie strings in Java with HttpCookie." Pavel Polívka | Sciencx - Thursday July 22, 2021, https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/
HARVARD
Pavel Polívka | Sciencx Thursday July 22, 2021 » Parsing cookie strings in Java with HttpCookie., viewed ,<https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/>
VANCOUVER
Pavel Polívka | Sciencx - » Parsing cookie strings in Java with HttpCookie. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/
CHICAGO
" » Parsing cookie strings in Java with HttpCookie." Pavel Polívka | Sciencx - Accessed . https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/
IEEE
" » Parsing cookie strings in Java with HttpCookie." Pavel Polívka | Sciencx [Online]. Available: https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/. [Accessed: ]
rf:citation
» Parsing cookie strings in Java with HttpCookie | Pavel Polívka | Sciencx | https://www.scien.cx/2021/07/22/parsing-cookie-strings-in-java-with-httpcookie/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.