[Jifty-commit] jifty branch, master, updated. b84a8b0ff5817dbc165568a58cc9bf90286fa307

Jifty commits jifty-commit at lists.jifty.org
Tue Apr 13 23:33:11 EDT 2010


The branch, master has been updated
       via  b84a8b0ff5817dbc165568a58cc9bf90286fa307 (commit)
      from  3a07880894ea8ab1ebd162e0d34813bc2c59058b (commit)

Summary of changes:
 Makefile.PL                                       |    2 +-
 lib/Jifty/CAS.pm                                  |   28 ++++++----
 lib/Jifty/Plugin/CompressedCSSandJS.pm            |   40 +++++++++++++--
 lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm |   59 ---------------------
 4 files changed, 54 insertions(+), 75 deletions(-)
 delete mode 100644 lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm

- Log -----------------------------------------------------------------
commit b84a8b0ff5817dbc165568a58cc9bf90286fa307
Author: Chia-liang Kao <clkao at clkao.org>
Date:   Wed Apr 14 11:26:29 2010 +0800

    Make Jifty::CAS serve over pure PSGI. and Make Plugin::CompressedCSSandJS as middleware rather than jifty dispacher.

diff --git a/Makefile.PL b/Makefile.PL
index b11d5c8..643b0ca 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -70,7 +70,7 @@ requires('Module::Refresh');
 requires('Module::ScanDeps');
 requires('Object::Declare' => '0.13');
 requires('Params::Validate');
-requires('Plack' => '0.9910'); # Plack::Loader Plack::Test Plack::Util Plack::Builder Plack::Request Plack::Handler::FCGI HTTP::Message::PSGI
+requires('Plack' => '0.9910'); # Plack::Loader Plack::Test Plack::Util Plack::Builder Plack::Request Plack::Handler::FCGI HTTP::Message::PSGI Plack::Response
 requires('Plack::Middleware::Deflater');
 requires('Scalar::Defer' => '0.12');
 requires('Shell::Command');
diff --git a/lib/Jifty/CAS.pm b/lib/Jifty/CAS.pm
index e41c6da..5a4b55d 100644
--- a/lib/Jifty/CAS.pm
+++ b/lib/Jifty/CAS.pm
@@ -3,6 +3,8 @@ use warnings;
 
 package Jifty::CAS;
 use base 'Jifty::CAS::Store';
+use Plack::Request;
+use Plack::Response;
 
 =head1 NAME
 
@@ -74,16 +76,18 @@ code set by this method (possibly for your use in the dispatcher).
 =cut
 
 sub serve_by_name {
-    my ($class, $domain, $name, $incoming_key) = @_;
+    my ($class, $domain, $name, $incoming_key, $env) = @_;
     my $key = Jifty::CAS->key($domain, $name);
 
     return $class->_serve_404( $domain, $name, "Unable to lookup key." )
         if not defined $key;
 
-    if ( Jifty->web->request->header('If-Modified-Since')  and $incoming_key eq $key ) {
+    my $res = Plack::Response->new(200);
+    my $req = Plack::Request->new($env);
+    if ( $req->header('If-Modified-Since') and $incoming_key eq $key ) {
         Jifty->log->debug("Returning 304 for CAS cached $domain:$name ($key)");
-        Jifty->web->response->header( Status => 304 );
-        return 304;
+        $res->status(304);
+        return $res->finalize;
     }
 
     my $obj = Jifty::CAS->retrieve($domain, $key);
@@ -91,22 +95,24 @@ sub serve_by_name {
     return $class->_serve_404( $domain, $name, "Unable to retrieve blob." )
         if not defined $obj;
 
-    Jifty->web->response->content_type($obj->metadata->{content_type});
-    Jifty::View::Static::Handler->send_http_header('', length($obj->content),
-                                                   $obj->metadata->{time});
+    $res->content_type($obj->metadata->{content_type});
+    $res->header( 'Cache-Control' => 'max-age=31536000, public' );
+    $res->header( 'Expires' => HTTP::Date::time2str( time() + 31536000 ) );
+    $res->header( 'Content-Length' => length($obj->content) );
+    $res->header(
+      'Last-Modified' => HTTP::Date::time2str( $obj->metadata->{time} ) );
 
     Jifty->log->debug("Sending squished $domain:$name ($key) from CAS");
-    Jifty->web->out($obj->content);
+    $res->body($obj->content);
 
-    return 200;
+    return $res->finalize;
 }
 
 sub _serve_404 {
     my ($class, $domain, $name, $msg) = @_;
     $msg ||= '';
     Jifty->log->error("Returning 404 for CAS cached $domain:$name.  $msg");
-    Jifty->web->response->header( Status => 404 );
-    return 404;
+    return Plack::Response->new(404)->finalize;
 }
 
 1;
diff --git a/lib/Jifty/Plugin/CompressedCSSandJS.pm b/lib/Jifty/Plugin/CompressedCSSandJS.pm
index f0c3f8b..627ba6f 100644
--- a/lib/Jifty/Plugin/CompressedCSSandJS.pm
+++ b/lib/Jifty/Plugin/CompressedCSSandJS.pm
@@ -9,6 +9,7 @@ use IO::Handle ();
 use Plack::Util;
 use HTTP::Message::PSGI;
 use HTTP::Request;
+use Plack::Response;
 
 =head1 NAME
 
@@ -78,7 +79,7 @@ sub init {
             callback  => sub { $self->_include_javascript(@_) },
             abortable => 1,
         );
-        Jifty->add_trigger( post_init => sub { $self->_generate_javascript })
+        Jifty->add_trigger( post_init => sub { $self->generate_javascript })
             if $self->generate_early;
     }
 
@@ -118,7 +119,7 @@ sub css_enabled {
 sub _include_javascript {
     my $self = shift;
 
-    $self->_generate_javascript;
+    $self->generate_javascript;
     Jifty->web->out(
         qq[<script type="text/javascript" src="@{[ $self->cdn ]}/__jifty/js/]
           . Jifty::CAS->key( 'ccjs', 'js-all' )
@@ -171,14 +172,14 @@ sub generate_css {
 
 
 
-=head3 _generate_javascript
+=head3 generate_javascript
 
 Checks if the compressed JS is generated, and if it isn't, generates
 and caches it.
 
 =cut
 
-sub _generate_javascript {
+sub generate_javascript {
     my $self = shift;
 
     return if Jifty::CAS->key('ccjs', 'js-all') && !Jifty->config->framework('DevelMode');
@@ -268,5 +269,36 @@ sub _js_is_skipped {
     return grep { $file eq $_ } @{ $self->skipped_js };
 }
 
+=head2 wrap
+
+psgi app wrapper to serve url controlled by us
+
+=cut
+
+sub wrap {
+    my ($self, $app) = @_;
+
+    sub {
+        my $env = shift;
+        if (my ($mode, $arg) = $env->{PATH_INFO} =~ m{/__jifty/(css|js)/(.*)}) {
+            if ( $arg !~ /^[0-9a-f]{32}\.$mode$/ ) {
+                # This doesn't look like a real request for squished JS or CSS,
+                # so redirect to a more failsafe place
+                my $res = Plack::Response->new;
+                $res->redirect( "/static/$mode/$arg" );
+                return $res->finalize;
+            }
+
+            my $method = "generate_".($mode eq 'js' ? 'javascript' : 'css');
+            $self->can($method)->($self);
+            $arg =~ s/\.$mode//;
+            return Jifty::CAS->serve_by_name( 'ccjs', $mode.'-all', $arg, $env );
+        }
+        else {
+            return $app->($env);
+        }
+    };
+}
+
 
 1;
diff --git a/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm b/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
deleted file mode 100644
index 603cada..0000000
--- a/lib/Jifty/Plugin/CompressedCSSandJS/Dispatcher.pm
+++ /dev/null
@@ -1,59 +0,0 @@
-use warnings;
-use strict;
-
-package Jifty::Plugin::CompressedCSSandJS::Dispatcher;
-
-=head1 NAME
-
-Jifty::Plugin::CompressedCSSandJS::Dispatcher - Dispatcher for css and js
-compression
-
-=head1 DESCRIPTION
-
-Adds dispatcher rules for C</__jifty/js/*> and C</__jifty/css/*/>,
-which serve out compiled and compressed CSS and Javascript rules.
-
-=cut
-use HTTP::Date ();
-
-use Jifty::Dispatcher -base;
-
-on '/__jifty/js/*' => run {
-    my $arg = $1;
-    if ( $arg !~ /^[0-9a-f]{32}\.js$/ ) {
-
-        # This doesn't look like a real request for squished JS,
-        # so redirect to a more failsafe place
-        Jifty->web->redirect( "/static/js/" . $arg );
-    }
-
-    my ($ccjs) = Jifty->find_plugin('Jifty::Plugin::CompressedCSSandJS')
-        or Jifty->web->redirect( "/static/js/" . $arg );
-
-    $ccjs->_generate_javascript;
-
-    $arg =~ s/\.js$//;
-    my $status = Jifty::CAS->serve_by_name( 'ccjs', 'js-all', $arg );
-    abort $status if $status != 200;
-    abort;
-};
-
-on '/__jifty/css/*' => run {
-    my $arg = $1;
-    my ($ccjs) = Jifty->find_plugin('Jifty::Plugin::CompressedCSSandJS');
-    if ( $arg !~ /^[0-9a-f]{32}\.css$/ || !$ccjs) {
-
-        # This doesn't look like a real request for squished CSS,
-        # so redirect to a more failsafe place
-        Jifty->web->redirect( "/static/css/" . $arg );
-    }
-
-    $ccjs->generate_css;
-
-    $arg =~ s/\.css$//;
-    my $status = Jifty::CAS->serve_by_name( 'ccjs', 'css-all', $arg );
-    abort $status if $status != 200;
-    abort;
-};
-
-1;

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list