summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Woodward2010-07-15 00:46:01 -0400
committerJason Woodward2010-07-15 00:46:01 -0400
commit458ee1e79075eabd047954e84e6a51dfbdfc3df4 (patch)
treec9d400486a9a82f72caa7f0e543e1d7dd38675ac
parent49f0fa328a2c6f52016c76caed6a97b21fb48b64 (diff)
downloadJaos-WebApp-458ee1e79075eabd047954e84e6a51dfbdfc3df4.tar.gz
added ::Plugin::Static
-rw-r--r--MANIFEST19
-rw-r--r--lib/Jaos/WebApp/Plugin/Static.pm124
2 files changed, 137 insertions, 6 deletions
diff --git a/MANIFEST b/MANIFEST
index e3a6cfa..c35a381 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -1,15 +1,22 @@
COPYING
lib/Jaos/WebApp.pm
+lib/Jaos/WebApp/Session/Simple.pm
+lib/Jaos/WebApp/Route.pm
lib/Jaos/WebApp/Controller.pm
-lib/Jaos/WebApp/Dispatcher.pm
+lib/Jaos/WebApp/Plugin/Static.pm
+lib/Jaos/WebApp/Plugin/CleanParams.pm
+lib/Jaos/WebApp/Plugin/RequestTimer.pm
lib/Jaos/WebApp/Render.pm
-lib/Jaos/WebApp/Render/JSON.pm
-lib/Jaos/WebApp/Render/RSS.pm
+lib/Jaos/WebApp/Route/HeaderConstraint.pm
+lib/Jaos/WebApp/Route/ParamConstraint.pm
+lib/Jaos/WebApp/Route/MethodConstraint.pm
+lib/Jaos/WebApp/Log.pm
lib/Jaos/WebApp/Render/Text.pm
+lib/Jaos/WebApp/Render/Data.pm
lib/Jaos/WebApp/Render/TT.pm
-lib/Jaos/WebApp/Route.pm
-lib/Jaos/WebApp/Route/MethodConstraint.pm
-lib/Jaos/WebApp/Route/ParamConstraint.pm
+lib/Jaos/WebApp/Render/JSON.pm
+lib/Jaos/WebApp/Render/RSS.pm
+lib/Jaos/WebApp/Dispatcher.pm
Makefile.PL
README
t/01use.t
diff --git a/lib/Jaos/WebApp/Plugin/Static.pm b/lib/Jaos/WebApp/Plugin/Static.pm
new file mode 100644
index 0000000..2d5e715
--- /dev/null
+++ b/lib/Jaos/WebApp/Plugin/Static.pm
@@ -0,0 +1,124 @@
+package Jaos::WebApp::Plugin::Static;
+use strict;
+use warnings;
+use base qw/Jaos::WebApp::Controller/;
+use Plack::MIME;
+
+sub register
+{
+ my ($self, $app, $mapping) = @_;
+
+ for my $url ( keys %$mapping ) {
+ my $path = $mapping->{$url};
+ $url =~ s|/$||;
+ $url = '/'.$url if ($url !~ m|^/|);
+ if ($path !~ m/^\//) { $path = join('/',$app->appdir, $path); }
+
+ $app->{__static_path}->{$url} = $path;
+ $app->connect("$url/:file", controller => __PACKAGE__, action => 'serve');
+ $app->connect("$url/", controller => __PACKAGE__, action => 'list');
+ }
+}
+
+sub list
+{
+ my ($self) = @_;
+ (my $url = $self->app->req->path) =~ s|^(/[^/]+)/|$1|;
+ my $path = $self->app->{__static_path}->{$url};
+
+ opendir(my $dir, $path) or do {
+ $self->app->res->status('404');
+ return;
+ };
+
+ my @files = grep { -f "$path/$_" } grep { ! m/^[\.]+$/ } readdir($dir);
+ closedir($dir);
+
+ my $listing = '<html><body><ul>';
+ $listing .= join ("\n", map { "<li><a href='$_'>$_</a></li>" } @files);
+ $listing .= '</ul></body></html>';
+
+ $self->app->res->status(200);
+ $self->app->res->header('Content-Type' => 'text/html');
+ $self->app->res->body($listing);
+}
+
+sub serve
+{
+ my ($self) = @_;
+ my $file = $self->param('file');
+ (my $url = $self->app->req->path) =~ s|^(/[^/]+)/.*|$1|;
+ my $path = $self->app->{__static_path}->{$url};
+ my $filename = join('/', $path, $file);
+
+ open my $fh, '<:raw', $filename or do {
+ $self->app->res->status('404');
+ return;
+ };
+
+ my @stat = stat $filename;
+ my $mime = Plack::MIME->mime_type($file);
+
+ $self->app->res->status(200);
+ $self->app->res->header('Content-Type' => $mime || 'application/octect-stream');
+ $self->app->res->header('Content-Length' => $stat[7]);
+ $self->app->res->body($fh);
+ # this is closed for us
+ #close($fh);
+}
+
+=head1 NAME
+
+Jaos::WebApp::Plugin::Static - Serve static files
+
+=head1 SYNOPSIS
+
+ package MyApp;
+
+ sub startup
+ {
+ my $app = shift;
+ $app->load_plugin('Jaos::WebApp::Plugin::Static', {
+ '/url' => 'local_path',
+ '/url2' => '/abs_path'
+ });
+ }
+
+ 1;
+
+=head1 DESCRIPTION
+
+Jaos::WebApp::Plugin::Static is a simple plugin that serves static files and
+simple directory listings.
+
+=head1 METHODS
+
+=head2 register
+
+register connects routes for the given urls to paths.
+
+=head1 AUTHOR
+
+Jason Woodward <woodwardj@jaos.org>
+
+=head1 LICENSE
+
+Copyright (C) 2010 Jason Woodward
+All rights reserved
+
+This library is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this library. If not, see <http://www.gnu.org/licenses/>.
+
+=cut
+
+1;