[Jifty-commit] r559 - in jifty/trunk: lib/Jifty lib/Jifty/Script lib/Jifty/Web

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Feb 8 19:15:54 EST 2006


Author: alexmv
Date: Wed Feb  8 19:15:51 2006
New Revision: 559

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/META.yml
   jifty/trunk/Makefile.PL
   jifty/trunk/lib/Jifty/Config.pm
   jifty/trunk/lib/Jifty/Handler.pm
   jifty/trunk/lib/Jifty/MasonHandler.pm
   jifty/trunk/lib/Jifty/Script/FastCGI.pm
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/lib/Jifty/Web/PageRegion.pm

Log:
 r8982 at zoq-fot-pik:  chmrr | 2006-02-08 19:14:39 -0500
  * Update deps
  * Move more Mason config (and defaults) into Jifty::Config or
    Jifty::MasonHandler as appropriate
  * Doc Jifty::MasonHandler more
  * $m->redirect now calls Jifty->web->redirect, so redirect code is
    only in one place


Modified: jifty/trunk/META.yml
==============================================================================
--- jifty/trunk/META.yml	(original)
+++ jifty/trunk/META.yml	Wed Feb  8 19:15:51 2006
@@ -1,5 +1,5 @@
 distribution_type: module
-generated_by: Module::Install version 0.540
+generated_by: Module::Install version 0.550
 license: unknown
 name: Jifty
 no_index:
@@ -14,6 +14,7 @@
   Cache::Cache: 0
   Carp: 0
   Class::Accessor: 0
+  Class::Container: 0
   Clone: 0
   Cwd: 0
   DBD::SQLite: 0
@@ -48,6 +49,7 @@
   Module::Install::Admin: 0.50
   Module::Pluggable: 2.95
   Module::Refresh: 0
+  Params::Validate: 0
   Pod::Simple: 0
   Pod::Usage: 0
   Scalar::Util: 0

Modified: jifty/trunk/Makefile.PL
==============================================================================
--- jifty/trunk/Makefile.PL	(original)
+++ jifty/trunk/Makefile.PL	Wed Feb  8 19:15:51 2006
@@ -10,6 +10,7 @@
 requires('Cache::Cache'); #Cache::FileCache
 requires('Carp');
 requires('Class::Accessor');
+requires('Class::Container');
 requires('Clone');
 requires('Cwd');
 requires('DBD::SQLite');
@@ -30,7 +31,7 @@
 requires('FindBin');
 requires('Getopt::Long');
 requires('HTML::Entities');
-requires('HTML::Mason' => 1.3101);           # HTML::Mason::CGIHandler HTML::Mason::Interp HTML::Mason::Plugin HTML::Mason::Request::CGI
+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::Server::Simple' => '0.10');  # HTTP::Server::Simple::CGI
@@ -44,6 +45,7 @@
 requires('Module::Install::Admin' => '0.50');
 requires('Module::Pluggable' => '2.95');
 requires('Module::Refresh');
+requires('Params::Validate');
 requires('Pod::Simple'); #Pod::Simple::Text
 requires('Pod::Usage');
 requires('Scalar::Util');

Modified: jifty/trunk/lib/Jifty/Config.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Config.pm	(original)
+++ jifty/trunk/lib/Jifty/Config.pm	Wed Feb  8 19:15:51 2006
@@ -199,7 +199,6 @@
     if (@_) {
         $app_name = shift;
     } elsif ($self->stash->{framework}->{ApplicationName}) {
-
         $app_name =  $self->stash->{framework}->{ApplicationName};
     } else {
         $app_name =  Jifty::Util->default_app_name;
@@ -228,10 +227,15 @@
             MailerArgs => [],
             Web        => {
                 DefaultStaticRoot => Jifty::Util->share_root . '/web/static',
-                DefaultTemplateRoot => Jifty::Util->share_root
-                    . '/web/templates',
+                DefaultTemplateRoot => Jifty::Util->share_root . '/web/templates',
                 StaticRoot   => "web/static",
                 TemplateRoot => "web/templates",
+                Autoflush    => 0,
+                ErrorMode    => 'fatal',
+                ErrorFormat  => 'text',
+                DefaultEscapeFlags => 'h',
+                Plugins      => ['Jifty::Mason::Halo'],
+                Globals      => [],
             }
         }
     };

Modified: jifty/trunk/lib/Jifty/Handler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Handler.pm	(original)
+++ jifty/trunk/lib/Jifty/Handler.pm	Wed Feb  8 19:15:51 2006
@@ -12,7 +12,7 @@
   use Jifty;
   Jifty->new();
 
-  my $cgihandler = HTML::Mason::CGIHandler->new( Jifty->handler->mason_config );
+  my $handler = Jifty::Handler->handle_request( cgi => $cgi );
 
   # after each request is handled
   Jifty::Handler->cleanup_request;
@@ -54,16 +54,16 @@
 
 sub mason_config {
     return (
-        allow_globals => [qw[$JiftyWeb]],
+        allow_globals => [qw[$JiftyWeb], @{Jifty->config->framework('Web')->{'Globals'} || []}],
         comp_root     => [ 
-                            [application =>  Jifty::Util->absolute_path( Jifty->config->framework('Web')->{'TemplateRoot'} || "html")],
-                            [jifty => Jifty->config->framework('Web')->{'DefaultTemplateRoot'}
-                                ]],
-        error_mode => 'fatal',
-        error_format => 'text',
-        default_escape_flags => 'h',
-        autoflush => 0,
-       # plugins => ['Jifty::Mason::Halo']
+                          [application =>  Jifty::Util->absolute_path( Jifty->config->framework('Web')->{'TemplateRoot'} )],
+                          [jifty => Jifty->config->framework('Web')->{'DefaultTemplateRoot'}],
+                         ],
+        default_escape_flags => Jifty->config->framework('Web')->{'DefaultEscapeFlags'},
+        autoflush    => Jifty->config->framework('Web')->{'Autoflush'},
+        error_mode   => Jifty->config->framework('Web')->{'ErrorMode'},
+        error_format => Jifty->config->framework('Web')->{'ErrorFormat'},
+        plugins      => Jifty->config->framework('Web')->{'Plugins'},
     );
 }
 
@@ -99,33 +99,7 @@
     $self->mason(Jifty::MasonHandler->new(
         $self->mason_config,
         cgi => $args{cgi},
-        out_method => sub {
-            my $m = HTML::Mason::Request->instance;
-            my $r = $m->cgi_request;
-
-            $r->content_type || $r->content_type('text/html; charset=utf-8'); # Set up a default
-
-            if ($r->content_type =~ /charset=([\w-]+)$/ ) {
-                my $enc = $1;
-                binmode *STDOUT, ":encoding($enc)";
-            }
-
-            unless ($r->http_header_sent or not $m->auto_send_headers) {
-                $r->send_http_header();
-            }
-
-            # We could perhaps install a new, faster out_method here that
-            # wouldn't have to keep checking whether headers have been
-            # sent and what the $r->method is.  That would require
-            # additions to the Request interface, though.
-            print STDOUT grep {defined} @_;
-        },
     ));
-    $self->mason->interp->set_escape(
-        h => \&Jifty::Handler::escape_utf8 );
-    $self->mason->interp->set_escape(
-        u => \&Jifty::Handler::escape_uri );
-
 
     $self->dispatcher(Jifty->config->framework('ApplicationClass')."::Dispatcher");
     $self->dispatcher->require;
@@ -135,43 +109,6 @@
 
 }
 
-=head2 escape_utf8 SCALARREF
-
-Does a css-busting but minimalist escaping of whatever html you're passing in.
-
-=cut
-
-sub escape_utf8 {
-    my $ref = shift;
-    my $val = $$ref;
-    use bytes;
-    $val =~ s/&/&/g;
-    $val =~ s/</&lt;/g;
-    $val =~ s/>/&gt;/g;
-    $val =~ s/\(/&#40;/g;
-    $val =~ s/\)/&#41;/g;
-    $val =~ s/"/&#34;/g;
-    $val =~ s/'/&#39;/g;
-    $$ref = $val;
-    Encode::_utf8_on($$ref);
-
-}
-
-=head2 escape_uri SCALARREF
-
-Escapes URI component according to RFC2396
-
-=cut
-
-use Encode qw();
-
-sub escape_uri {
-    my $ref = shift;
-    $$ref = Encode::encode_utf8($$ref);
-    $$ref =~ s/([^a-zA-Z0-9_.!~*'()-])/uc sprintf("%%%02X", ord($1))/eg;
-    Encode::_utf8_on($$ref);
-}
-
 =head2 cleanup_request
 
 Dispatchers should call this at the end of each request, as a class method.

Modified: jifty/trunk/lib/Jifty/MasonHandler.pm
==============================================================================
--- jifty/trunk/lib/Jifty/MasonHandler.pm	(original)
+++ jifty/trunk/lib/Jifty/MasonHandler.pm	Wed Feb  8 19:15:51 2006
@@ -1,17 +1,26 @@
+use strict;
+use warnings;
+
 package Jifty::MasonHandler;
 
-# XXX ALEX: CLEAN UP AND DOC THIS
+=head1 NAME
 
-use strict;
-use warnings;
+Jifty::MasonHandler - Handler for Mason requests inside of Jifty
+
+=head1 SUMMARY
+
+Jifty controls all of the input and output from the Mason templating
+engine; this means that we cannot use the Mason's standard
+L<HTML::Mason::CGIHandler> interface to interact with it.
+
+=cut
 
 use HTML::Mason;
 use HTML::Mason::Utils;
-use CGI;
-use File::Spec;
 use Params::Validate qw(:all);
 use HTML::Mason::Exceptions;
 use HTML::Mason::FakeApache;
+use Encode qw();
 
 use Class::Container;
 use base qw(Class::Container);
@@ -30,34 +39,118 @@
 __PACKAGE__->contained_objects
     (
      interp => 'HTML::Mason::Interp',
-     cgi_request => { class   => 'HTML::Mason::FakeApache', # $r
+     fake_apache => { class   => 'HTML::Mason::FakeApache', # $r
                       delayed => 1 },
     );
 
 
+=head2 new PARAMHASH
+
+Takes a number of key-value parameters; see L<HTML::Mason::Params>.
+Defaults the C<out_method> to L</out_method>, and the C<request_class>
+to L<HTML::MAson::request::Jifty> (below).  Finally, adds C<h> and
+C<u> escapes, which map to L</escape_uri> and L<escape_utf8>
+respectively.
+
+=cut
+
 sub new {
     my $package = shift;
 
     my %p = @_;
-    my $self = $package->SUPER::new(comp_root => $ENV{DOCUMENT_ROOT},
-                                    request_class => 'HTML::Mason::Request::Jifty',
-                                    error_mode => 'output',
-                                    error_format => 'html',
-                                    %p);
-
+    my $self = $package->SUPER::new( request_class => 'HTML::Mason::Request::Jifty',
+                                     out_method => \&out_method,
+                                     %p );
     $self->interp->compiler->add_allowed_globals('$r');
-    
+    $self->interp->set_escape( h => \&escape_utf8 );
+    $self->interp->set_escape( u => \&escape_uri );
+
     return $self;
 }
 
+
+=head2 out_method
+
+The default output method.  Sets the content-type to C<text/html;
+charset=utf-8> unless a content type has already been set, and then
+sends a header if need be.
+
+=cut
+
+sub out_method {
+    my $m = HTML::Mason::Request->instance;
+    my $r = $m->fake_apache;
+
+    $r->content_type || $r->content_type('text/html; charset=utf-8'); # Set up a default
+
+    if ($r->content_type =~ /charset=([\w-]+)$/ ) {
+        my $enc = $1;
+        binmode *STDOUT, ":encoding($enc)";
+    }
+
+    unless ($r->http_header_sent or not $m->auto_send_headers) {
+        $r->send_http_header();
+    }
+
+    # We could perhaps install a new, faster out_method here that
+    # wouldn't have to keep checking whether headers have been
+    # sent and what the $r->method is.  That would require
+    # additions to the Request interface, though.
+    print STDOUT grep {defined} @_;
+}
+
+
+=head2 escape_utf8 SCALARREF
+
+Does a css-busting but minimalist escaping of whatever html you're passing in.
+
+=cut
+
+sub escape_utf8 {
+    my $ref = shift;
+    my $val = $$ref;
+    use bytes;
+    $val =~ s/&/&#38;/g;
+    $val =~ s/</&lt;/g;
+    $val =~ s/>/&gt;/g;
+    $val =~ s/\(/&#40;/g;
+    $val =~ s/\)/&#41;/g;
+    $val =~ s/"/&#34;/g;
+    $val =~ s/'/&#39;/g;
+    $$ref = $val;
+    Encode::_utf8_on($$ref);
+}
+
+
+=head2 escape_uri SCALARREF
+
+Escapes URI component according to RFC2396
+
+=cut
+
+sub escape_uri {
+    my $ref = shift;
+    $$ref = Encode::encode_utf8($$ref);
+    $$ref =~ s/([^a-zA-Z0-9_.!~*'()-])/uc sprintf("%%%02X", ord($1))/eg;
+    Encode::_utf8_on($$ref);
+}
+
+
+=head2 handle_comp COMPONENT
+
+Takes a component path to render.  Deals with setting up a
+L<HTML::Mason::FakeApache> and Request object, and calling the
+component.
+
+=cut
+
 sub handle_comp {
     my ($self, $comp) = (shift, shift);
 
-    my $r = $self->create_delayed_object('cgi_request', cgi => $self->{cgi});
+    # Create a new HTML::Mason::FakeApache object.  But we don't really use it.
+    my $r = $self->create_delayed_object('fake_apache', cgi => $self->{cgi});
     $self->interp->set_global('$r', $r);
-
-    # hack for testing
-    $self->interp->delayed_object_params('request', cgi_request => $r);
+    $self->interp->delayed_object_params('request', fake_apache => $r);
 
     my %args = $self->request_args($r);
 
@@ -86,6 +179,13 @@
     return wantarray ? @result : defined wantarray ? $result[0] : undef;
 }
 
+=head2 request_args
+
+The official source for request arguments is from the current
+L<Jifty::Request> object.
+
+=cut
+
 sub request_args {
     return %{Jifty->web->request->arguments};
 }
@@ -95,6 +195,12 @@
 package HTML::Mason::Request::Jifty;
 # Subclass for HTML::Mason::Request object $m
 
+=head1 HTML::Mason::Request::Jifty
+
+Subclass of L<HTML::Mason::Request> which is customised for Jifty's use.
+
+=cut
+
 use HTML::Mason::Exceptions;
 use HTML::Mason::Request;
 use base qw(HTML::Mason::Request);
@@ -103,19 +209,33 @@
 Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
 
 __PACKAGE__->valid_params
-    ( cgi_request => { isa => 'HTML::Mason::FakeApache' } );
+    ( fake_apache => { isa => 'HTML::Mason::FakeApache' } );
 
 use HTML::Mason::MethodMaker
-    ( read_only  => [ 'cgi_request' ] );
+    ( read_only  => [ 'fake_apache' ] );
+
+=head2 auto_send_headers
+
+Doesn't send headers if this is a subrequest (according to the current
+L<Jifty::Request>).
+
+=cut
 
 sub auto_send_headers {
     return not Jifty->web->request->is_subrequest;
 }
 
+=head2 exec
+
+Actually runs the component; in case no headers have been sent after
+running the component, and we're supposed to send headers, sends them.
+
+=cut
+
 sub exec
 {
     my $self = shift;
-    my $r = $self->cgi_request;
+    my $r = $self->fake_apache;
     my $retval;
 
     eval { $retval = $self->SUPER::exec(@_) };
@@ -130,24 +250,24 @@
     # On a success code, send headers if they have not been sent and
     # if we are the top-level request. Since the out_method sends
     # headers, this will typically only apply after $m->abort.
-    if (!$self->is_subrequest
-        and $self->auto_send_headers
+    if ($self->auto_send_headers
+        and not $r->http_header_sent
         and (!$retval or $retval==200)) {
         $r->send_http_header();
     }
 }
 
+=head2 redirect
+
+Calls L<Jifty::Web/redirect>.
+
+=cut
+
 sub redirect {
     my $self = shift;
     my $url = shift;
-    my $status = shift || 302;
-
-    $self->clear_buffer;
-
-    $self->{cgi_request}->header_out( Location => $url );
-    $self->{cgi_request}->header_out( Status => $status );
 
-    $self->abort;
+    Jifty->web->redirect($url);
 }
 
 1;

Modified: jifty/trunk/lib/Jifty/Script/FastCGI.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Script/FastCGI.pm	(original)
+++ jifty/trunk/lib/Jifty/Script/FastCGI.pm	Wed Feb  8 19:15:51 2006
@@ -8,7 +8,6 @@
 use File::Basename;
 use CGI::Fast;
 use Module::Refresh;
-use HTML::Mason::CGIHandler;
 use Jifty::Everything;
 
 =head1 NAME

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Wed Feb  8 19:15:51 2006
@@ -609,17 +609,16 @@
     my $self = shift;
     my ($page) = @_;
 
-    if ($self->mason) {
-        $self->mason->redirect($page);
-    } else {
-        # This is designed to work under CGI or FastCGI; will need an
-        # abstraction for mod_perl
-        my $apache = HTML::Mason::FakeApache->new();
-        $apache->header_out( Location => $page );
-        $apache->header_out( Status => 302 );
-        $apache->send_http_header();
-        Jifty::Dispatcher::last_rule();
-    }
+    # This is designed to work under CGI or FastCGI; will need an
+    # abstraction for mod_perl
+    $self->mason->clear_buffer if $self->mason;
+    my $apache = $self->mason ? $self->mason->fake_apache : HTML::Mason::FakeApache->new();
+
+    $apache->header_out( Location => $page );
+    $apache->header_out( Status => 302 );
+    $apache->send_http_header();
+    $self->mason->abort if $self->mason;
+    Jifty::Dispatcher::last_rule();
 
 }
 

Modified: jifty/trunk/lib/Jifty/Web/PageRegion.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/PageRegion.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/PageRegion.pm	Wed Feb  8 19:15:51 2006
@@ -249,10 +249,7 @@
 
     # Convince Mason to tack its response onto a variable and not send
     # headers when it does so
-    Jifty->handler->mason->interp->out_method(sub {
-        HTML::Mason::Request->instance->auto_send_headers(0);
-        $result .= $_[0];
-    });
+    Jifty->handler->mason->interp->out_method( sub { $result .= $_[0]; });
 
     # Call into the dispatcher
     Jifty->dispatcher->handle_request;


More information about the Jifty-commit mailing list