Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Pattern in spark

I'm trying to make a filter that matches the following URL's:

/foo and /foo/*

So anything under /foo/ and also the base case /foo

I have this filter:

    Spark.before("/foo/*", (request, response) -> {
        String ticket = request.cookie("session");
        if (ticket == null) {
            Spark.halt(302);
        }
    });

But of course this does not execute when I enter to /foo

I tried with the following but with no luck:

/foo* /foo.* /foo/

Is there anyway to achieve this? Or maybe to use a list of URL? so that I can assign both url to the same filter.

And please don't say to store the function in a variable so that the I use it two times because I think that is not clean at all..

like image 289
Pablo Matias Gomez Avatar asked Jan 29 '15 15:01

Pablo Matias Gomez


1 Answers

According to https://github.com/perwendel/spark/blob/1ecd428c8e2e5b0d1b8f221e9bf9e82429bd73bb/src/main/java/spark/route/RouteEntry.java#L31 (where the path matching takes place), it does not look like what you want to do is possible.

The RouteEntry code splits the given pattern, and the given url at the '/' character, and then looks for "matches" (equality or wildcard) for each of the components.

Here's a more detailed explanation:

The url /foo/blah has 2 parts (in the terminology of RouteEntry code linked to above), while /foo has 1 part. For a pattern to match the first, it must have 2 parts : /foo/* is the only one that makes sense. But this pattern has 2 parts, which makes /foo fail the if checks on both lines 49 and 78. The only special case is the hack on line 71, which should make the pattern /foo/* match the url /foo/, but not /foo.

like image 89
Manish Avatar answered Nov 12 '22 21:11

Manish