align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

RE: [ljc] Cookie Parse Regex

From: Somay
Sent on: Wednesday, February 6, 2013 11:19 AM

Hi Karl,

 

If you want to set and get the “expires” cookie attribute use the setMaxAge() and getMaxAge() methods which are available for both java.net.HttpCookie and javax.servlet.httpCookie.

 

While working out the regex is a good exercise to practice your regular expressions; you’ll find that the cookie standard implementation cover most aspects of cookies and creating your own might just be reinventing the wheel.

 

Somay

 

From: [address removed] [mailto:[address removed]] On Behalf Of Karl Bennett
Sent: 05 February[masked]:11
To: [address removed]
Subject: Re: [ljc] Cookie Parse Regex

 

Hi Luigi,

I am toying with writing my own Cookie object, one that supports "Expires".

Neither java.net.HttpCookie, javax.ws.rs.core.Cookie, or javax.servlet.http.Cookie support this attribute.

Does anyone know the reason for that? Has it been deprecated?

 

On 5 February[masked]:06, Luigi Bitonti <[address removed]> wrote:

You are welcome Karl. By the way, is there any specific reason why you are not using  the standard JDK HttpCookie#parse(String) method?

docs.oracle.com/javase/6/docs/api/java/net/HttpCookie.html

Can you share it?

Cheers,

Luigi

 

 


From: Karl Bennett <[address removed]>
To: [address removed]

Sent: Tuesday, February 5,[masked]:39 AM
Subject: Re: [ljc] Cookie Parse Regex

 

Haha thanks Luigi,

The old "off by one" error :D

Maybe I should actually "read" the API docs?

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#groupCount%28%29

"Group zero denotes the entire pattern by convention. It is not included in this count."

 

On 4 February[masked]:53, Luigi Bitonti <[address removed]> wrote:

Hi Karl,

The problem is not with the regexp. It is in this line:

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

I believe it should read:


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

 

As group 0 is the entire match, so the groups go from 1 to matcher.groupCount() included.

 

From a very brief test it seems to work.

 

I hope this helps

 

Cheers,

Luigi

 

 

 


From: Karl Bennett <[address removed]>
To: [address removed]
Sent: Sunday, February 3,[masked]:57 PM
Subject: [ljc] Cookie Parse Regex

 

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





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Karl Bennett ([address removed]) from LJC - London Java Community.
To learn more about Karl Bennett, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

 





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])

This message was sent by Luigi Bitonti ([address removed]) from LJC - London Java Community.
To learn more about Luigi Bitonti, visit his/her member profile

 





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Karl Bennett ([address removed]) from LJC - London Java Community.
To learn more about Karl Bennett, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

 




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])

This message was sent by Luigi Bitonti ([address removed]) from LJC - London Java Community.
To learn more about Luigi Bitonti, visit his/her member profile

 





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Karl Bennett ([address removed]) from LJC - London Java Community.
To learn more about Karl Bennett, visit his/her member profile
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

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, 8-10th June 16

  • SkillsMatter

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

  • IBM

    Build Enterprise-grade apps at start-up speed.

  • New Relic

    New Relic makes sense of billions of metrics a day in real time.

  • Hazelcast

    Hazelcast is the leader in operating in-memory computing.

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

  • Craft Rebellion

    Your choice of fresh craft beer, delivered. For 10% off use ‘LJC'

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