Jifty::View::Mason::Handler - Handler for Mason requests inside of Jifty


Jifty documentation Contained in the Jifty distribution.

Index


Code Index:

NAME

Top

Jifty::View::Mason::Handler - Handler for Mason requests inside of Jifty

SUMMARY

Top

Jifty controls all of the input and output from the Mason templating engine; this means that we cannot use the Mason's standard HTML::Mason::CGIHandler interface to interact with it.

new PARAMHASH

Takes a number of key-value parameters; see HTML::Mason::Params. Defaults the out_method to appending to buffer in Jifty::Handler and the request_class to Jifty::View::Mason::Request (below). Finally, adds h and u escapes, which map to escape_uri and escape_utf8 respectively.

config

Returns our Mason config. We use the component root specified in the Web/TemplateRoot framework configuration variable (or html by default). Additionally, we set up a jifty component root, as specified by the Web/DefaultTemplateRoot configuration. All interpolations are HTML-escaped by default, and we use the fatal error mode.

escape_utf8 SCALARREF

Does a css-busting but minimalist escaping of whatever html you're passing in.

escape_uri SCALARREF

Escapes in-place URI component according to RFC2396. Takes a reference to perl string.

*Note* that octets would be treated as latin1 encoded sequence and converted to UTF-8 encoding and then escaped. So this sub always provide UTF-8 escaped string. See also Encode for more info about converting.

template_exists COMPONENT

Checks if the COMPONENT exists, or if COMPONENT/index.html exists, and returns which one did. If neither did, it searches for dhandler components which could match, returning COMPONENT if it finds one. Finally, if it finds no possible component matches, returns undef.

Note that this algorithm does not actually decisively return if Mason will handle a given component; the dhandlers could defer handling, for instance.

show COMPONENT

Takes a component path to render. Deals with setting up a global HTML::Mason::FakeApache and Request object, and calling the component.

handle_comp

A synonym for show

request_args

The official source for request arguments is from the current Jifty::Request object.

create_cache_directories

Attempts to create our application's mason cache directory.


Jifty documentation Contained in the Jifty distribution.
use strict;
use warnings;

package Jifty::View::Mason::Handler;

use HTML::Mason;
use HTML::Mason::Utils;
use Params::Validate qw(:all);
use HTML::Mason::Exceptions;
use HTML::Mason::FakeApache;
use Encode qw();
use Jifty::View::Mason::Request;

use Class::Container;
use base qw(Jifty::View Class::Container);

use HTML::Mason::MethodMaker
    ( read_write => [ qw( interp ) ] );

use vars qw($VERSION);

__PACKAGE__->valid_params
    (
     interp => { isa => 'HTML::Mason::Interp' },
    );

__PACKAGE__->contained_objects
    (
     interp => 'HTML::Mason::Interp',
    );


sub new {
    my $package = shift;

    $package->create_cache_directories;

    my %p = @_ || $package->config;
    my $self = $package->SUPER::new( request_class => 'Jifty::View::Mason::Request',
                                     out_method => sub {Carp::cluck("Mason output skipped Jifty's output stack!") if grep {defined and length} @_},
                                     %p );
    $self->interp->set_escape( h => \&escape_utf8 );
    $self->interp->set_escape( u => \&escape_uri );

    return $self;
}


sub config {
    my $self = shift;

    my %config = (
        static_source => 1,
        use_object_files => 1,
        preprocess => sub {
            # Force UTF-8 semantics on all our components by
            # prepending this block to all components as Mason
            # components defaults to parse the text as Latin-1
            ${$_[0]} =~ s!^!<\%INIT>use utf8;</\%INIT>\n!;
        },
        data_dir =>  Jifty::Util->absolute_path( Jifty->config->framework('Web')->{'DataDir'} ),
        allow_globals => [
            qw[ $JiftyWeb ],
            @{Jifty->config->framework('Web')->{'Globals'} || []},
        ],
        comp_root     => [ 
                          [application =>  Jifty::Util->absolute_path( Jifty->config->framework('Web')->{'TemplateRoot'} )],
                         ],
        %{ Jifty->config->framework('Web')->{'MasonConfig'} },
    );

    my $root_serial = 0;
    my %seen; $seen{$_} = 1 for map Jifty->config->framework('Web')->{$_}, qw/TemplateRoot DefaultTemplateRoot/;
    for my $plugin (Jifty->plugins) {
        my $comp_root = $plugin->template_root;
        next unless ( defined $comp_root and -d $comp_root and not $seen{$comp_root}++);
        $plugin->log->debug( "Plugin @{[ref($plugin)]} mason component root added: (@{[$comp_root ||'']})");
        push @{ $config{comp_root} }, [ ref($plugin)."-". $root_serial++ => $comp_root ];
    }
    push @{$config{comp_root}}, [jifty => Jifty::Util->absolute_path( Jifty->config->framework('Web')->{'DefaultTemplateRoot'})];

    # In developer mode, we want halos, refreshing and all that other good stuff. 
    if (Jifty->config->framework('DevelMode') ) {
        push @{$config{'plugins'}}, 'Jifty::View::Mason::Halo'
            unless Jifty->config->framework('HideHalos');
        $config{static_source}    = 0;
        $config{use_object_files} = 0;
    }

    # We require autoflush now.
    $config{autoflush} = 1;

    return %config;
}

sub escape_utf8 {
    my $ref = shift;
    no warnings 'uninitialized';
    $$ref =~ s/&/&#38;/g;
    $$ref =~ s/</&lt;/g;
    $$ref =~ s/>/&gt;/g;
    $$ref =~ s/\(/&#40;/g;
    $$ref =~ s/\)/&#41;/g;
    $$ref =~ s/"/&#34;/g;
    $$ref =~ s/'/&#39;/g;
}

sub escape_uri {
    my $ref = shift;
    $$ref = Encode::encode_utf8($$ref);
    $$ref =~ s/([^a-zA-Z0-9_.!~*'()-])/uc sprintf("%%%02X", ord($1))/eg;
}


sub template_exists {
    my $self = shift;
    my ($component) = @_;
    $component =~ s{^/*}{/};
    return $component if $self->interp->comp_exists($component);
    return "$component/index.html" if $self->interp->comp_exists("$component/index.html");

    my $dhandler = $self->interp->dhandler_name;
    $dhandler = "dhandler" unless defined $dhandler;
    return if defined $dhandler and not length $dhandler;
    return $component if $self->interp->find_comp_upwards($component, $dhandler);
    return undef;
}


sub show {
    shift->handle_comp(@_);
}

sub _comp_setup {
    my ($self, $comp, $args) = @_;

    # XXX FIXME This is a kludge to get use_mason_wrapper to work
    $self->interp->set_global('$jifty_internal_request', 0);
    $self->interp->set_global('$jifty_internal_request', 1) if defined $args;

    return $args ? %$args : $self->request_args;
}

sub handle_comp {
    my $self = shift;
    my ($comp) = @_;

    my %args = $self->_comp_setup(@_);
    $self->interp->exec($comp, %args);
}

sub request_args {
    return %{Jifty->web->request->arguments}, %{Jifty->web->request->template_arguments || {}};
}


sub create_cache_directories {
    for ( Jifty->config->framework('Web')->{'DataDir'} ) {
        Jifty::Util->make_path( Jifty::Util->absolute_path($_) );
    }
}

1;