Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Help with static and RegEx

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


mikeatvdomck.org

Jun 29, 2005, 10:29 AM

Post #1 of 6 (473 views)
Permalink
Help with static and RegEx

So I am trying to serve all static pages out of the root directory.
Should be simple, but I am having trouble. It seems that the regex
matches, but then the handler doesn't execute:

# match anything after the slash that is not a slash
sub files : Regex('/*([^/]*\.(css|js|html))$') {
my ( $self, $c ) = @_;
my ($file, $extension) = @{ $c->req->snippets };
$c->req->path( "$file" );
$c->serve_static;
}


But this is what happens:

[Wed Jun 29 09:08:41 2005] [catalyst] [debug]
**********************************
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] * Request 2 (0.333/s) [26154]
[Wed Jun 29 09:08:41 2005] [catalyst] [debug]
**********************************
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] Requested action is
"booking/style.css" and matched "/*([^/]*\.(css|js|html))$"
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] Snippets are "style.css css"
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] "GET" request for
"booking/style.css" from 192.168.1.3
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] Rendering template
"booking/style.css"
[Template::Service] process(booking/style.css, HASH(0x91be5fc))
[Wed Jun 29 09:08:41 2005] [catalyst] [error] Couldn't render template
"file error - booking/style.css: not found"
[Wed Jun 29 09:08:41 2005] [catalyst] [info] Request took 0.023221s
(43.064/s)

So it is trying to serve the template instead. And if I insert debug
code in the files method it never shows it. What is happening?

Mike


mikeatvdomck.org

Jun 29, 2005, 10:45 AM

Post #2 of 6 (446 views)
Permalink
Help with static and RegEx [In reply to]

Answering my own question:

Mike McKay wrote:
> So I am trying to serve all static pages out of the root directory.
> Should be simple, but I am having trouble. It seems that the regex
> matches, but then the handler doesn't execute:
>
> # match anything after the slash that is not a slash
> sub files : Regex('/*([^/]*\.(css|js|html))$') {

<snip/>

> code in the files method it never shows it. What is happening?

From the documentation:
---
If you want to pass variable arguments at the end of a URL, you must
use regex actions keys with '^' and '$' anchors, and the arguments must
be separated with forward slashes (/) in the URL
---

So boys and girls, today Mike learned that all RegEx URL handlers need
to start with a caret.

Cheers,
Mike


infoatwhawes.co.uk

Jun 29, 2005, 10:52 AM

Post #3 of 6 (447 views)
Permalink
Help with static and RegEx [In reply to]

I think ideally Catalyst should test for that and throw an error.

wh

----- Original Message -----
From: Mike McKay <mike [at] vdomck>
To: The elegant MVC web framework <catalyst [at] lists>
Sent: Wed, 29 Jun 2005 09:48:19 +0100
Subject: Re: [Catalyst] Help with static and RegEx


> Answering my own question:
>
> Mike McKay wrote:
> > So I am trying to serve all static pages out of the root directory.
> > Should be simple, but I am having trouble. It seems that the regex
> > matches, but then the handler doesn't execute:
> >
> > # match anything after the slash that is not a slash
> > sub files : Regex('/*([^/]*\.(css|js|html))$') {
>
> <snip/>
>
> > code in the files method it never shows it. What is happening?
>
> From the documentation:
> ---
> If you want to pass variable arguments at the end of a URL, you must
> use regex actions keys with '^' and '$' anchors, and the arguments must
> be separated with forward slashes (/) in the URL
> ---
>
> So boys and girls, today Mike learned that all RegEx URL handlers need
> to start with a caret.
>
> Cheers,
> Mike
>
>
> _______________________________________________
> Catalyst mailing list
> Catalyst [at] lists
> http://lists.rawmode.org/mailman/listinfo/catalyst
>
>


mikeatvdomck.org

Jun 29, 2005, 11:35 AM

Post #4 of 6 (445 views)
Permalink
Help with static and RegEx [In reply to]

Will Hawes wrote:
> I think ideally Catalyst should test for that and throw an error.

That would be great.

By the way, below is what I finally came up with. Not that I think this
is best practice, or even the assumed usage for Catalyst::Plugin::Static
but in case it would be useful to have some more code in the docs. I
need to to make it ignore case, like m/foo/i - but I don't now how to do
it with the Regex notation - can anyone help?

# want to match booking/style.css
# and style.css
# match anything after the last slash that is not a slash
sub staticFiles : Regex('^.*?/*([^/]*\.(css|js|html))$') {
my ( $self, $c ) = @_;
my ($file, $extension) = @{ $c->req->snippets };
$c->req->path( "$file" );
$c->serve_static;
}

sub images : Regex('^.*?/*([^/]*\.(jpg|png|gif|ico))$') {
my ( $self, $c ) = @_;
my ($file, $extension) = @{ $c->req->snippets };
$c->req->path( "img/$file" );
$c->serve_static;
}


>>Answering my own question:
>>
>>Mike McKay wrote:
>>
>>>So I am trying to serve all static pages out of the root directory.
>>>Should be simple, but I am having trouble. It seems that the regex
>>>matches, but then the handler doesn't execute:
>>>
>>># match anything after the slash that is not a slash
>>>sub files : Regex('/*([^/]*\.(css|js|html))$') {
>>
>><snip/>
>>
>>>code in the files method it never shows it. What is happening?
>>
>> From the documentation:
>>---
>>If you want to pass variable arguments at the end of a URL, you must
>>use regex actions keys with '^' and '$' anchors, and the arguments must
>>be separated with forward slashes (/) in the URL
>>---
>>
>>So boys and girls, today Mike learned that all RegEx URL handlers need
>>to start with a caret.
>>
>>Cheers,
>>Mike
>>
>>
>>_______________________________________________
>>Catalyst mailing list
>>Catalyst [at] lists
>>http://lists.rawmode.org/mailman/listinfo/catalyst
>>
>>
>
>
>
> _______________________________________________
> Catalyst mailing list
> Catalyst [at] lists
> http://lists.rawmode.org/mailman/listinfo/catalyst


dstorrsatdstorrs.com

Jun 29, 2005, 4:10 PM

Post #5 of 6 (445 views)
Permalink
Help with static and RegEx [In reply to]

On Jun 29, 2005, at 5:38 AM, Mike McKay wrote:

> I need to to make it ignore case, like m/foo/i - but I don't now
> how to do it with the Regex notation - can anyone help?

/foo/i is equivalent to
/(?i)foo/

--Dks


mikeatvdomck.org

Jun 30, 2005, 7:43 AM

Post #6 of 6 (455 views)
Permalink
Help with static and RegEx [In reply to]

David Storrs wrote:
> /foo/i is equivalent to
> /(?i)foo/

Excellent. Attached is a patch that would include the following in the
Catalyst::Plugin::Static documentation.

# use a Regex to match static content to be served
# want to match foo/image.JPG and image.jpg
# ignore case match anything after the last slash (or no slash) that is
not a slash
sub images : Regex('(?i)^.*?/*([^/]*\.(jpg|png|gif|ico))$') {
my ( $self, $c ) = @_;
my ($file, $extension) = @{ $c->req->snippets };
$c->req->path( "img/$file" );
$c->serve_static;
}

Is this is right way to submit patches?

Mike
-------------- next part --------------
--- Static.pm 2005-06-30 06:26:37.000000000 +0100
+++ Static.new 2005-06-30 06:29:14.000000000 +0100
@@ -23,6 +23,17 @@

# or specify explicitly if you know better
$c->serve_static('text/css');
+
+ # use a Regex to match static content to be served
+ # want to match foo/image.JPG and image.jpg
+ # ignore case match anything after the last slash (or no slash) that is not a slash
+ sub images : Regex('(?i)^.*?/*([^/]*\.(jpg|png|gif|ico))$') {
+ my ( $self, $c ) = @_;
+ my ($file, $extension) = @{ $c->req->snippets };
+ $c->req->path( "img/$file" );
+ $c->serve_static;
+ }
+

=head1 DESCRIPTION

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.