[Jifty-commit] r720 - jifty/trunk/lib/Jifty/Handler

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue Mar 21 01:30:07 EST 2006


Author: jesse
Date: Tue Mar 21 01:30:06 2006
New Revision: 720

Modified:
   /   (props changed)
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty/Handler/Static.pm

Log:
 r30254 at truegrounds:  jesse | 2006-03-21 01:28:58 -0500
 * Overhauled the static server to try really hard to force caching by clients.
 * Refactored a bit to unify some codepaths
 


Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Tue Mar 21 01:30:06 2006
@@ -10,6 +10,7 @@
 requires('Class::Accessor');
 requires('Class::Container');
 requires('Clone');
+requires('Compress::Zlib');
 requires('DBD::SQLite');
 requires('Data::Page');
 requires('Digest::MD5');
@@ -25,6 +26,7 @@
 requires('HTML::Mason' => 1.3101);           # HTML::Mason::Exceptions HTML::Mason::FakeApache HTML::Mason::MethodMaker HTML::Mason::Request HTML::Mason::Utils
 requires('HTML::Mason::Plugin');
 requires('HTTP::Cookies');
+requires('HTTP::Date');
 requires('HTTP::Server::Simple' => '0.10');  # HTTP::Server::Simple::CGI
 requires('HTTP::Server::Simple::Recorder');
 requires('Hash::Merge');

Modified: jifty/trunk/lib/Jifty/Handler/Static.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Handler/Static.pm	(original)
+++ jifty/trunk/lib/Jifty/Handler/Static.pm	Tue Mar 21 01:30:06 2006
@@ -1,7 +1,10 @@
 use warnings;
 use strict;
-use File::MMagic;
-use MIME::Types;
+use File::MMagic ();
+use MIME::Types ();
+use Compress::Zlib ();
+use HTTP::Date ();
+
 
 package Jifty::Handler::Static;
 
@@ -40,6 +43,12 @@
 
 =cut
 
+
+=head2 new
+
+Create a new static file handler. Likely, only the C<Jifty::Handler> needs to do this.
+
+=cut
 sub new {
     my $class = shift;
     my $self = {};
@@ -68,9 +77,9 @@
     my $mime_type = $self->mime_type($local_path);
     
     if ( $self->client_accepts_gzipped_content and $mime_type =~ m!^(text/|application/x-javascript)! ) {
-        return $self->send_gzipped($local_path, $mime_type);
+        return $self->send_file($local_path, $mime_type, 'gzip');
     } else {
-        return  $self->send_uncompressed($local_path, $mime_type);
+        return  $self->send_file($local_path, $mime_type, 'uncompressed');
     }
 
 }
@@ -139,83 +148,73 @@
 }
 
 
-=head2 send_gzipped $path $mimetype
+=head2 send_file $path $mimetype $compression
 
-Print a gzipped version of C<$path> to STDOUT (the client), identified with a mimetype of C<$mimetype>.
-Eventually, this will cache the file on disk. Right now, it just does the gzipping in memory.
+Print C<$path> to STDOUT (the client), identified with a mimetype of C<$mimetype>.
+
+If C<$compression> is C<gzip>, gzip the output stream.
 
 
 =cut
 
 
-sub send_gzipped {
+sub send_file {
     my $self       = shift;
     my $local_path = shift;
     my $mime_type  = shift;
+    my $compression = shift;
+
 
     my $fh = IO::File->new( $local_path, 'r' );
     if ( defined $fh ) {
         binmode $fh;
 
-        $self->log->debug("Sending gzip'd file $local_path");
         # 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 @file_info = stat($local_path);
         my $apache = Jifty->handler->apache;
 
-        $apache->content_type($mime_type);
-        $apache->header_out( "Content-Encoding" => "gzip");
         $apache->header_out( Status => 200 );
+        $apache->content_type($mime_type);
+        my $now = time();
+        $apache->header_out(Expires =>  HTTP::Date::time2str($now + 31536000));  # Expire in a year
+        $apache->header_out('Last-Modified' =>  HTTP::Date::time2str( $file_info[9]));
+        $apache->header_out('Content-Length' => $file_info[7]) unless ($compression eq 'gzip');  
+
+        $apache->header_out( "Content-Encoding" => "gzip") if ($compression eq 'gzip');
         $apache->send_http_header();
 
+        if ($compression eq 'gzip') {
         undef $/;
-        require Compress::Zlib;
         binmode STDOUT;
         # XXX TODO: Cache this
         print STDOUT Compress::Zlib::memGzip(<$fh>);
-        
+        } else{
+            $apache->send_fd($fh);
+        }
+        close($fh);
         return 1;
     } else {
         return undef;
     }
 }
 
-=head2 send_gzipped $path $mimetype
-
-Print an uncompressed version of C<$path> to STDOUT (the client), identified with a mimetype of C<$mimetype>.
 
-=cut
+=head2 send_not_modified
 
+Sends a "304 Not modified" response to the browser, telling it to use a cached copy.
 
-sub send_uncompressed {
-    my $self       = shift;
-    my $local_path = shift;
-    my $mime_type  = shift;
-
-    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;
+=cut
 
-        my $apache = Jifty->handler->apache;
+sub send_not_modified {
+    my $self = shift;
+    Jifty->handler->apache->header_out( Status => 304 );
+    return 1;
 
-        $apache->content_type($mime_type);
-        $apache->header_out( Status => 200 );
-        $apache->send_http_header();
-        $apache->send_fd($fh);
-        close($fh);
-        return 1;
-    } else {
-        return undef;
-    }
 }
 
 1;


More information about the Jifty-commit mailing list