[Jifty-commit] r707 - jifty/trunk jifty/trunk/lib/Jifty/Handler
jifty/trunk/share/web/templates
jifty/trunk/share/web/templates/_elements
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sun Mar 19 19:37:04 EST 2006
Author: jesse
Date: Sun Mar 19 19:36:57 2006
New Revision: 707
Added:
jifty/trunk/lib/Jifty/Handler/
jifty/trunk/lib/Jifty/Handler/Static.pm
Removed:
jifty/trunk/share/web/templates/favicon.ico
Modified:
/ (props changed)
jifty/trunk/Makefile.PL
jifty/trunk/lib/Jifty/Config.pm
jifty/trunk/lib/Jifty/Everything.pm
jifty/trunk/lib/Jifty/Handler.pm
jifty/trunk/share/web/templates/_elements/header
jifty/trunk/share/web/templates/_elements/javascript
jifty/trunk/share/web/templates/dhandler
jifty/trunk/share/web/templates/index.html
Log:
r30191 at truegrounds: jesse | 2006-03-19 19:35:42 -0500
* First pass at the new static server
Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL (original)
+++ jifty/trunk/Makefile.PL Sun Mar 19 19:36:57 2006
@@ -19,6 +19,7 @@
requires('Email::Simple');
requires('Email::Simple::Creator');
requires('Encode');
+requires('File::MMagic');
requires('File::ShareDir');
requires('HTML::Entities');
requires('HTML::Mason' => 1.3101); # HTML::Mason::Exceptions HTML::Mason::FakeApache HTML::Mason::MethodMaker HTML::Mason::Request HTML::Mason::Utils
@@ -32,6 +33,7 @@
requires('Locale::Maketext::Simple');
requires('Log::Log4perl');
requires('LWP::UserAgent');
+requires('MIME::Types');
requires('Module::CoreList');
requires('Module::Install::Admin' => '0.50');
requires('Module::Pluggable' => '2.95');
Modified: jifty/trunk/lib/Jifty/Config.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Config.pm (original)
+++ jifty/trunk/lib/Jifty/Config.pm Sun Mar 19 19:36:57 2006
@@ -233,6 +233,7 @@
DataDir => "var/mason",
StaticRoot => "web/static",
TemplateRoot => "web/templates",
+ ServeStaticFiles => 1,
MasonConfig => {
autoflush => 0,
error_mode => 'fatal',
Modified: jifty/trunk/lib/Jifty/Everything.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Everything.pm (original)
+++ jifty/trunk/lib/Jifty/Everything.pm Sun Mar 19 19:36:57 2006
@@ -32,6 +32,7 @@
use Jifty::Logger ();
use Jifty::Handler ();
+use Jifty::Handler::Static ();
use Jifty::MasonHandler ();
use Jifty::Model::Schema ();
Modified: jifty/trunk/lib/Jifty/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Handler.pm (original)
+++ jifty/trunk/lib/Jifty/Handler.pm Sun Mar 19 19:36:57 2006
@@ -26,7 +26,7 @@
use base qw/Class::Accessor/;
use Hook::LexWrap;
-__PACKAGE__->mk_accessors(qw(mason dispatcher cgi apache));
+__PACKAGE__->mk_accessors(qw(mason dispatcher static_handler cgi apache));
=head2 new
@@ -52,6 +52,9 @@
Jifty->config->framework('ApplicationClass') . "::Dispatcher" );
Jifty::Util->require( $self->dispatcher );
$self->mason( Jifty::MasonHandler->new( $self->mason_config ) );
+
+ $self->static_handler(Jifty::Handler::Static->new());
+
return $self;
}
@@ -138,24 +141,29 @@
sub handle_request {
my $self = shift;
my %args = (
- cgi => undef,
+ cgi => undef,
@_
);
- Module::Refresh->refresh if (Jifty->config->framework('DevelMode') );
- $self->cgi($args{cgi});
- $self->apache(HTML::Mason::FakeApache->new(cgi => $self->cgi));
+ Module::Refresh->refresh if ( Jifty->config->framework('DevelMode') );
+ $self->cgi( $args{cgi} );
+ $self->apache( HTML::Mason::FakeApache->new( cgi => $self->cgi ) );
local $HTML::Mason::Commands::JiftyWeb = Jifty::Web->new();
- Jifty->web->request(Jifty::Request->new()->fill($self->cgi));
+ Jifty->web->request( Jifty::Request->new()->fill( $self->cgi ) );
Jifty->web->response( Jifty::Response->new );
Jifty->web->setup_session;
Jifty->web->session->set_cookie;
- Jifty->log->debug("Received request for ".Jifty->web->request->path);
-
- $self->dispatcher->handle_request();
+ Jifty->log->debug( "Received request for " . Jifty->web->request->path );
+
+ my $sent_response = 0;
+ $sent_response
+ = $self->static_handler->handle_request( Jifty->web->request->path )
+ if ( Jifty->config->framework('Web')->{'ServeStaticFiles'} );
+
+ $self->dispatcher->handle_request() unless ($sent_response);
$self->cleanup_request();
Added: jifty/trunk/lib/Jifty/Handler/Static.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Handler/Static.pm Sun Mar 19 19:36:57 2006
@@ -0,0 +1,142 @@
+use warnings;
+use strict;
+use File::MMagic;
+use MIME::Types;
+
+package Jifty::Handler::Static;
+
+use base qw/Jifty::Object/;
+
+our $mime = MIME::Types->new();
+our $magic = File::MMagic->new();
+
+=head1 NAME
+
+Jifty::Handler::Static
+
+head1 DESCRIPTION
+
+This class takes care of serving out static files for a Jifty application.
+
+When fully operational, it will use an algorithm along the lines of the following:
+
+* Static files are served out of a separate root
+* If static files go through the dispatcher:
+ * return "True" (304) for if-modified-since unless develmode.
+ * if the browser accepts gzipped data,
+ see if we have a cached gzipped copy
+ if so, send it
+ see if we have a marker indicating that gzip is a lose
+ if so, send uncompressed
+
+ gzip the content
+ send the gzipped content
+ * if the browser doesn't accept gzipped content
+ send the content uncompressed
+
+
+=cut
+
+sub new {
+ my $class = shift;
+ my $self = {};
+ bless $self, $class;
+}
+
+sub handle_request {
+ my $self = shift;
+ my $path = shift;
+
+ #if ( Jifty->web->request->header('If-Modified-Since') and not Jifty->config->framework('DevelMode') ) { $self->respond_not_modified(); }
+
+ my $local_path = $self->file_path($path);
+ unless ($local_path) {
+ return undef;
+ }
+ if ( $self->client_accepts_gzipped_content ) {
+ return $self->send_gzipped($local_path);
+ } else {
+ return $self->send_uncompressed($local_path);
+ }
+
+}
+
+sub client_accepts_gzipped_content {0}
+
+sub file_path {
+ my $self = shift;
+ my $file = shift;
+ my @options = (qw(StaticRoot DefaultStaticRoot));
+
+ # Chomp a leading "/static" - should this be configurable?
+ $file =~ s/^\/*?static//;
+
+ foreach my $path (@options) {
+
+ my $abspath = Jifty::Util->absolute_path(
+ Jifty->config->framework('Web')->{$path} . "/" . $file );
+
+ return $abspath if ( -f $abspath && -r $abspath );
+
+ }
+ return undef;
+
+}
+
+sub mime_type {
+ my $self = shift;
+ my $local_path = shift;
+
+ # XXX TODO: make this a pkg global
+ my $mimeobj = $mime->mimeTypeOf($local_path);
+ my $mime_type = (
+ $mimeobj
+ ? $mimeobj->type
+ : $magic->checktype_filename($local_path)
+ );
+
+ return ($mime_type);
+}
+
+
+sub send_gzipped { die 'unimplemented' }
+
+sub send_uncompressed {
+ my $self = shift;
+ my $local_path = shift;
+
+ my $mime_type = $self->mime_type($local_path);
+
+ my $fh = IO::File->new( $local_path, 'r' );
+ if ( defined $fh ) {
+ binmode $fh;
+
+ # This is designed to work under CGI or FastCGI; will need an
+ # abstraction for mod_perl
+
+ # Clear out the mason output, if any
+ Jifty->web->mason->clear_buffer if Jifty->web->mason;
+
+ my $apache = Jifty->handler->apache;
+
+ $apache->content_type($mime_type);
+ #$apache->header_out( 'Cache-Control' => 'max-age=3600, must-revalidate' );
+ $apache->header_out( Status => 200 );
+ $apache->send_http_header();
+ $apache->send_fd($fh);
+ close($fh);
+ return 1;
+ } else {
+ return undef;
+ }
+}
+
+sub missing_file {
+ # not currently used
+ my $self = shift;
+ my $file = shift;
+ Jifty->log->error("404: user tried to get to ".$file);
+ Jifty->web->redirect('/__jifty/error/file_not_found');
+}
+
+1;
Modified: jifty/trunk/share/web/templates/_elements/header
==============================================================================
--- jifty/trunk/share/web/templates/_elements/header (original)
+++ jifty/trunk/share/web/templates/_elements/header Sun Mar 19 19:36:57 2006
@@ -6,7 +6,7 @@
<title><% $title %></title>
- <link rel="stylesheet" type="text/css" href="/css/main.css" media="all" />
+ <link rel="stylesheet" type="text/css" href="/static/css/main.css" media="all" />
<& javascript &>
</head>
<%args>
Modified: jifty/trunk/share/web/templates/_elements/javascript
==============================================================================
--- jifty/trunk/share/web/templates/_elements/javascript (original)
+++ jifty/trunk/share/web/templates/_elements/javascript Sun Mar 19 19:36:57 2006
@@ -1,12 +1,22 @@
- <script type="text/javascript" src="/js/json.js"></script>
- <script type="text/javascript" src="/js/prototype.js"></script>
- <script type="text/javascript" src="/js/behaviour.js"></script>
- <script type="text/javascript" src="/js/scriptaculous/scriptaculous.js"></script>
- <script type="text/javascript" src="/js/jifty.js"></script>
- <script type="text/javascript" src="/js/dom-drag.js"></script>
- <script type="text/javascript" src="/js/halo.js"></script>
- <script type="text/javascript" src="/js/combobox.js"></script>
- <script type="text/javascript" src="/js/key_bindings.js"></script>
- <script type="text/javascript" src="/js/bps_util.js"></script>
- <script type="text/javascript" src="/js/rico.js"></script>
- <script type="text/javascript" src="/js/app_behaviour.js"></script>
+<%init>
+# XXX TODO: stick this into libs
+
+my @libs = qw(
+ /static/js/json.js
+ /static/js/prototype.js
+ /static/js/behaviour.js
+ /static/js/scriptaculous/scriptaculous.js
+ /static/js/jifty.js
+ /static/js/dom-drag.js
+ /static/js/halo.js
+ /static/js/combobox.js
+ /static/js/key_bindings.js
+ /static/js/bps_util.js
+ /static/js/rico.js
+ /static/js/app_behaviour.js
+);
+
+</%init>
+% for (@libs) {
+ <script type="text/javascript" src="<%$_%>"></script>
+% }
Modified: jifty/trunk/share/web/templates/dhandler
==============================================================================
--- jifty/trunk/share/web/templates/dhandler (original)
+++ jifty/trunk/share/web/templates/dhandler Sun Mar 19 19:36:57 2006
@@ -13,51 +13,6 @@
Used as a poor man's 404 handler
</%doc>
<%init>
-
-# XXX TODO: Use MIME Magic to serve the right mime types
-# XXX TODO: move into a library
-# This code loads up any static file and displays it if it would 404 from dynamic content. Failing that, actually 404
-my $file = $m->dhandler_arg;
-my $type = "application/octet-stream";
-if ( $file =~ /\.(gif|png|jpe?g)$/i ) {
- $type = "image/$1";
- $type =~ s/jpg/jpeg/gi;
-} elsif ( $file =~ /\.css$/i ) {
- $type = 'text/css';
-} elsif ( $file =~ /\.js$/i ) {
- $type = 'application/x-javascript';
-} elsif ($file =~ /\.ico$/i){
- $type = 'image/x-icon';
-}
-
-my @options = (
-
- Jifty::Util->absolute_path(
- Jifty->config->framework('Web')->{'StaticRoot'} || "static"
- )
- . "/"
- . $file,
-
- Jifty->config->framework('Web')->{'DefaultStaticRoot'} . "/" . $file
-);
-
-my $image;
-
-foreach $image (@options) {
-
- if ( ( -f $image && -r $image ) ) {
- $r->header_out( 'Cache-Control' => 'max-age=3600, must-revalidate' );
- $r->content_type($type);
- open( FILE, "<$image" ) || die;
- {
- local $/ = \16384;
- $m->out($_) while (<FILE>);
- close(FILE);
- }
- return;
- }
-}
-
Jifty->log->error("404: user tried to get to ".$m->dhandler_arg);
$r->header_out( Status => '404');
</%init>
Modified: jifty/trunk/share/web/templates/index.html
==============================================================================
--- jifty/trunk/share/web/templates/index.html (original)
+++ jifty/trunk/share/web/templates/index.html Sun Mar 19 19:36:57 2006
@@ -1,3 +1,3 @@
<&|/_elements/wrapper, title => 'Welcome to your new Jifty application' &>
-<img src="/images/pony.jpg" alt="You said you wanted a pony. (Source http://hdl.loc.gov/loc.pnp/cph.3c13461)" />
+<img src="/static/images/pony.jpg" alt="You said you wanted a pony. (Source http://hdl.loc.gov/loc.pnp/cph.3c13461)" />
</&>
More information about the Jifty-commit
mailing list