Router::PathInfo::Static - static routing
Class to describe the routing of statics. Allows us to describe the statics as follows:
- Specify the starting segment of the URI
- Specify a directory on disk, which will host the search for static
Statics is divided into two parts:
- allready - already exists (the "classic") static
allready
- on_demand - created on demand
on_demand
Case allready it's different css, js, images. on_demand it's archives and another. If the file to on_demand not found, match return undef - a signal that makes sense to continue search of Router::PathInfo::Controller routing.
match
If successful, returns hashref:
{ type => 'static', mime_type => $mime_type, file_name => '/path/to/found.static', };
This ensures that the file exists, has size, and is readable.
If static is not found (for allready) an error is returned:
{ type => 'error', code => 404, desc => sprintf('not found static for PATH_INFO = %s', $env->{PATH_INFO}) }
If PATH_INFO contains illegal characters (such as /../ or /.file)an error is returned:
/../
/.file
{ type => 'error', code => 403, desc => sprintf('forbidden for PATH_INFO = %s', $env->{PATH_INFO}) }
Return undef means that it makes sense to continue search of Router::PathInfo::Controller routing.
undef
my $s = Router::PathInfo::Static->new( # describe simple static allready => { path => $allready_path, first_uri_segment => 'static' }, # describe on demand created static on_demand => { path => $on_demand_path, first_uri_segment => 'archives', } ); my $env = {PATH_INFO => '/static/some.jpg'}; my @segment = split '/', $env->{PATH_INFO}, -1; shift @segment; $env->{'psgix.tmp.RouterPathInfo'} = { segments => [@segment], depth => scalar @segment }; my $res = $s->match($env); # $res = { # type => 'static', # file => $path_to_some_jpg, # mime => 'image/jpeg' # }
The constructor accepts the description of the statics (allready) and/or static generated on demand (on_demand). Each description is a hashref with the keys 'path' (directory path) and 'first_uri_segment' (the first segment of a PATH_INFO, which defines namespace for designated purposes).
'path'
'first_uri_segment'
All arguments are optional. If no arguments are given, the object is not created.
Objects method. Receives a uri and return:
For on_demand created static, return undef if file not found.
Plack::MIME, File::MimeInfo::Magic
Router::PathInfo, Router::PathInfo::Controller
mr.Rico <catamoose at yandex.ru>
To install Router::PathInfo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Router::PathInfo
CPAN shell
perl -MCPAN -e shell install Router::PathInfo
For more information on module installation, please visit the detailed CPAN module installation guide.