Cookie Parse Regex

From: user 1.
Sent on: Sunday, February 3, 2013 7:31 PM
So,

I'm trying to write an über regex to capture all the parts of a Set-Cookie header value.

This is what I've come up with:

private static final Pattern COOKIE_REGEX = Pattern.compile(
    "(?i)(?:^|\\s)([^\\s\\(\\)\\[\
\]\\{\\}=,\"\"\\\\/?@:;]+)=([^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\/?@:;]+)" +
    "(?:" +
        ";\\s+(
Comment=[^\\(\\)\\[\\]\\{\\}=,\"\"\\\\/?@:;]+)|" +
        ";\\s+(
Domain=[^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\@:;]+)|" +
        ";\\s+(
Expires=[^\\(\\)\\[\\]\\{\\}=\"\"\\\\/?@;]+)|" +
        ";\\s+(
Max-Age=\\d+)|" +
        ";\\s+(
Path=[^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\?@:;]+)|" +
        ";\\s+(
Version=[\\d]+)|" +
        ";\\s+(
Secure)|" +
        "(this is here to force the Secure capture. Don't know why it's needed.)" +
    ")*");

// Raw regex.
// (?i)(?:^|\s)([^\s\(\)\[\]\{\}=,""\\/?@:;]+)=([^\s\(\)\[\]\{\}=,""\\/?@:;]+)(?:;\s+(Comment=[^\(\)\[\]\{\}=,""\\/?@:;]+)|;\s+(Domain=[^\s\(\)\[\]\{\}=,""\\@:;]+)|;\s+(Expires=[^\(\)\[\]\{\}=""\\/?@;]+)|;\s+(Max-Age=\d+)|;\s+(Path=[^\s\(\)\[\]\{\}=,""\\?@:;]+)|;\s+(Version=[\d]+)|;\s+(Secure)|(this is here to force the Secure capture. Don't know why it's needed.))*

Now if you run this regex over a sample cookie value such as:

cookie_name=cookie_value; Domain=www.cookie.com; Comment=cookie_comment; Max-Age=86399; Path=/cookie; Expires=Sun, 03 Feb[masked]:56:51 GMT; Secure; Version=1

It works fine and we get the following captures:

[
    "cookie_name=cookie_value; Domain=www.cookie.com; Comment=cookie_comment; Max-Age=86399; Path=/cookie; Expires=Sun, 03 Feb[masked]:56:51 GMT; Secure; Version=1",
    "cookie_name",
    "cookie_value",
    "Comment=cookie_comment",
    "Domain=www.cookie.com",
    "Expires=Sun, 03 Feb[masked]:57:52 GMT",
    "Max-Age=86399",
    "Path=/cookie",
    "Version=1",
    "Secure"

]

Now as you might have already noticed there is a rather odd capture at the end of that regex. The problem is, if it's not there the "Secure" fragment won't be captured. Could someone please tell me why this is? Is my regex wrong? It is a little bonkers.

Here is some starter code to help with debugging:

public class CookieRegexTest {

    public static final String COMMENT = "Comment";
    public static final String DOMAIN = "Domain";
    public static final String EXPIRES = "Expires";
    public static final String MAX_AGE = "Max-Age";
    public static final String PATH = "Path";
    public static final String VERSION = "Version";
    public static final String SECURE = "Secure";

    private static final Pattern COOKIE_REGEX = Pattern.compile(
            "(?i)(?:^|\\s)([^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\/?@:;]+)=([^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\/?@:;]+)" +
                    "(?:" +
                    /**/";\\s+(" + COMMENT + "=[^\\(\\)\\[\\]\\{\\}=,\"\"\\\\/?@:;]+)|" +
                    /**/";\\s+(" + DOMAIN + "=[^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\@:;]+)|" +
                    /**/";\\s+(" + EXPIRES + "=[^\\(\\)\\[\\]\\{\\}=\"\"\\\\/?@;]+)|" +
                    /**/";\\s+(" + MAX_AGE + "=\\d+)|" +
                    /**/";\\s+(" + PATH + "=[^\\s\\(\\)\\[\\]\\{\\}=,\"\"\\\\?@:;]+)|" +
                    /**/";\\s+(" + VERSION + "=[\\d]+)|" +
                    /**/";\\s+(" + SECURE + ")|" +
                    /**/"(this is here to force the Secure capture. Don't know why it's needed.)" +
                    ")*");

    private static final String COOKIE = "cookie_name=cookie_value; " +
            DOMAIN + "=www.cookie.com; " +
            COMMENT + "=cookie_comment; " +
            MAX_AGE + "=86399; " +
            PATH + "=/cookie; " +
            EXPIRES + "=Sun, 03 Feb[masked]:31:57 GMT; " +
            SECURE + "; " +
            VERSION + "=1";

    private static String printGroups(Matcher matcher) {

        StringBuilder builder = new StringBuilder();

        for (int i = 1; i < matcher.groupCount(); i++) builder.append(matcher.group(i)).append('\n');

        return builder.toString();
    }

    public static void main(String[] args) {

        Matcher matcher = COOKIE_REGEX.matcher(COOKIE);
        matcher.matches();

        System.out.println(printGroups(matcher));
    }
}


Cheers,
Karl

Our Sponsors

  • Our Blog

    Read the latest news from the LJC

  • RecWorks Ltd

    Fixing Tech Recruitment using the Power of Community

  • jClarity

    Java/JVM Performance Analysis Tools & mentoring for Java related matters

  • LJC Aggrity

    Our LJC Aggrity site contains blog posts from our members

  • LJC Book Club

    Our Book club with book reviews from our members

  • Devoxx UK

    Java Community Conference, in collaboration with the LJC 12/13 Jun 14

  • SkillsMatter

    "Host, help organise, promote, film many of our meetings."

  • Packt Publishing

    A publishing company specializing on specific technologies and solutions

  • Java.Net

    We are an official Java User Group recognised by Oracle's JUG program

  • JRebel

    Free 3 month J-Rebel license.

  • O'Reilly

    40% discount on printed books and 50% on e-books.

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy