Matching a route including slashes (for directory listing)
Hi all, I'm trying to create an Apache-style directory view with links to files and subdirectories and showing file length. I'm attempting to match a route like '/media/foo/bar' and generate a directory listing for /public/foo/bar. I was playing with this tonight: get '/media/*' => sub { my $path = splat; ... } This matches '/media/foo' but I was expecting it to match '/media/foo/bar'. It appears that * doesn't match slashes. '/media/([\w/]+)' doesn't seem to work, either. I'd welcome suggestions for either problem: 1. matching paths with several levels of hierarchy 2. listing a directory Kind regards, Joel -- Joel Roth
On Sun, Jan 30, 2011 at 11:46 AM, Joel Roth <joelz@pobox.com> wrote:
Hi all,
Hey Joel,
get '/media/*' => sub { my $path = splat; ... }
This matches '/media/foo' but I was expecting it to match '/media/foo/bar'.
You're right, it doesn't match /media/foo/bar. The reason is simple: the syntax for routes, as defined by Sinatra, is not pure regex, and avoids matching a slash on purpose.[1] The idea is that you could declare a variable being there, without providing a name, and then splat() returns it.
'/media/([\w/]+)' doesn't seem to work, either.
Since the route syntax doesn't cover regexp.
I'd welcome suggestions for either problem:
1. matching paths with several levels of hierarchy
If you know the level, and you want to be explicit, you can try the default route syntax: '/media/*/*'. However, you will most likely prefer a regular expression (which you tried earlier), and Dancer allows that very easily since Perl considers regexes as first class: get qr/.../ => sub { ... }; We prefer to wrap qr() with something other than slashes, so we don't get into a backslash fight: get qr{ / media / (.*) }x => sub { ... }; Enjoy! [1] As they say in Microsoft: "it's a feature, not a bug!"
On Sun, Jan 30, 2011 at 5:05 AM, sawyer x <xsawyerx@gmail.com> wrote:
On Sun, Jan 30, 2011 at 11:46 AM, Joel Roth <joelz@pobox.com> wrote:
Hey Joel,
If you know the level, and you want to be explicit, you can try the default route syntax: '/media/*/*'. However, you will most likely prefer a regular expression (which you tried earlier), and Dancer allows that very easily since Perl considers regexes as first class: get qr/.../ => sub { ... };
We prefer to wrap qr() with something other than slashes, so we don't get into a backslash fight: get qr{ / media / (.*) }x => sub { ... };
Enjoy!
I feel compelled to caution about how the use of regexs can open security holes in your application. Another way to go would be to traverse the directory(s) and generate routes based on the findings in a before action. -- my 2 cents -- Al
On Sun, Jan 30, 2011 at 2:21 PM, Al Newkirk & Associates <we@ana.im> wrote:
On Sun, Jan 30, 2011 at 5:05 AM, sawyer x <xsawyerx@gmail.com> wrote:
On Sun, Jan 30, 2011 at 11:46 AM, Joel Roth <joelz@pobox.com> wrote:
Hey Joel,
If you know the level, and you want to be explicit, you can try the default route syntax: '/media/*/*'. However, you will most likely prefer a regular expression (which you tried earlier), and Dancer allows that very easily since Perl considers regexes as first class: get qr/.../ => sub { ... };
We prefer to wrap qr() with something other than slashes, so we don't get into a backslash fight: get qr{ / media / (.*) }x => sub { ... };
Enjoy!
I feel compelled to caution about how the use of regexs can open security holes in your application. Another way to go would be to traverse the directory(s) and generate routes based on the findings in a before action.
That's a good point and a good idea! Thanks, Al!
participants (3)
-
Al Newkirk & Associates -
Joel Roth -
sawyer x