/usr/share/perl5/Jifty/Handler.pm is in libjifty-perl 1.10518+dfsg-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | use strict;
use warnings;
package Jifty::Handler;
=head1 NAME
Jifty::Handler - Methods related to the finding and returning content
=head1 SYNOPSIS
use Jifty;
Jifty->new();
my $handler = Jifty::Handler->new;
$handler->handle_request( $env );
=head1 DESCRIPTION
L<Jifty::Handler> provides methods required to find and return content
to the browser. L</handle_request>, for instance, is the main entry
point for HTTP requests.
=cut
use base qw/Class::Accessor::Fast Jifty::Object/;
use Jifty::View::Declare::Handler ();
use Class::Trigger;
use String::BufferStack;
use Plack::Builder;
use Plack::Request;
__PACKAGE__->mk_accessors(qw(dispatcher _view_handlers stash buffer));
=head2 new
Create a new Jifty::Handler object. Generally, Jifty.pm does this only once at startup.
=cut
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->dispatcher( Jifty->app_class( "Dispatcher" ) );
Jifty::Util->require( $self->dispatcher );
$self->dispatcher->import_plugins;
eval { Jifty::Plugin::DumpDispatcher->dump_rules };
$self->buffer(String::BufferStack->new( out_method => \&Jifty::View::out_method ));
{
my $buffer = $self->buffer;
no warnings 'redefine';
*Jifty::Web::out = sub {shift;unshift @_,$buffer;goto \&String::BufferStack::append};
}
return $self;
}
=head2 view_handlers
Returns a list of modules implementing view for your Jifty application.
You can override this by specifying:
framework:
View:
Handlers:
- Jifty::View::Something::Handler
- Jifty::View::SomethingElse::Handler
=cut
sub view_handlers {
my @default = @{Jifty->config->framework('View')->{'Handlers'}};
# If there's a (deprecated) fallback handler, and it's not already
# in our set of handlers, tack it on the end
my $fallback = Jifty->config->framework('View')->{'FallbackHandler'};
push @default, $fallback if defined $fallback and not grep {$_ eq $fallback} @default;
return @default;
}
=head2 setup_view_handlers
Initialize all of our view handlers.
=cut
sub setup_view_handlers {
my $self = shift;
return if $self->_view_handlers;
$self->_view_handlers({});
foreach my $class ($self->view_handlers()) {
$self->_view_handlers->{$class} = $class->new();
}
}
=head2 view ClassName
Returns the Jifty view handler for C<ClassName>.
=cut
sub view {
my $self = shift;
my $class = shift;
$self->setup_view_handlers;
return $self->_view_handlers->{$class};
}
=head2 psgi_app_static
Returns a closure for L<PSGI> application that handles all static
content, including plugins.
=cut
sub psgi_app_static {
my $self = shift;
# XXX: this is no longer needed, however TestApp-Mason is having a
# static::handler-less config
my $view_handler = $self->view('Jifty::View::Static::Handler')
or return;;
require Plack::App::Cascade;
require Plack::App::File;
my $static = Plack::App::Cascade->new;
my $app_class = Jifty->app_class;
$static->add( $app_class->psgi_app_static )
if $app_class->can('psgi_app_static');
$static->add(
Plack::App::File->new(
root => Jifty::Util->absolute_path(
Jifty->config->framework('Web')->{StaticRoot}
)
)->to_app
);
for ( grep { defined $_ } map { $_->psgi_app_static } Jifty->plugins ) {
$static->add( $_ );
}
$static->add( Plack::App::File->new
( root => Jifty->config->framework('Web')->{DefaultStaticRoot} )->to_app );
# the buffering and unsetting of psgi.streaming is to vivify the
# responded res from the $static cascade app.
builder {
enable 'Plack::Middleware::ConditionalGET';
enable
sub { my $app = shift;
sub { my $env = shift;
$env->{'psgi.streaming'} = 0;
my $res = $app->($env);
# skip streamy response
return $res unless ref($res) eq 'ARRAY' && $res->[2];
my $h = Plack::Util::headers($res->[1]);;
$h->set( 'Cache-Control' => 'max-age=31536000, public' );
$h->set( 'Expires' => HTTP::Date::time2str( time() + 31536000 ) );
$res;
};
};
enable 'Plack::Middleware::BufferedStreaming';
$static;
};
}
=head2 psgi_app
Returns a closure for L<PSGI> application.
=cut
sub psgi_app {
my $self = shift;
my $app = sub { $self->handle_request(@_) };
my $static = $self->psgi_app_static;
$app = builder {
mount '/static' => $static;
mount '/' => $app
}
if Jifty->config->framework("Web")->{PSGIStatic} && $static;
# allow plugin to wrap $app
for ( Jifty->plugins ) {
$app = $_->wrap($app);
}
return $app;
}
=head2 handle_request
When your server process (be it Jifty-internal, FastCGI or anything
else) wants to handle a request coming in from the outside world, you
should call C<handle_request>.
=cut
sub handle_request {
my ($self, $env) = @_;
my $req = Plack::Request->new($env);
my $response;
$self->setup_view_handlers;
$self->call_trigger('before_request', $req);
# Simple ensure stdout is not writable in next major release
use IO::Handle::Util qw(io_prototype io_to_glob);
my $trapio= io_prototype
print => sub {
use Carp::Clan qw(^(Jifty::Handler|Carp::|IO::Handle::));
carp "printing to STDOUT is deprecated. Use outs, outs_raw or Jifty->web->response->body() instead";
my $self = shift;
Jifty->handler->buffer->out_method->(shift);
};
local *STDOUT = io_to_glob($trapio);
# this is scoped deeper because we want to make sure everything is cleaned
# up for the LeakDetector plugin. I tried putting the triggers in the
# method (Jifty::Server::handle_request) that calls this, but Jifty::Server
# isn't being loaded in time
{
# Build a new stash for the life of this request
$self->stash( {} );
local $Jifty::WEB = Jifty::Web->new();
if ( Jifty->config->framework('DevelMode') ) {
require Module::Refresh;
Module::Refresh->refresh;
Jifty::I18N->refresh;
}
Jifty->web->request( Jifty::Request->promote( $req ) );
Jifty->web->response( Jifty::Response->new );
$self->call_trigger('have_request');
Jifty->api->reset;
for ( Jifty->plugins ) {
$_->new_request;
}
$self->log->info( Jifty->web->request->method . " request for " . Jifty->web->request->path );
Jifty->web->setup_session;
Jifty->web->session->set_cookie;
Jifty::I18N->get_language_handle;
# Return from the continuation if need be
unless (Jifty->web->request->return_from_continuation) {
$self->buffer->out_method(\&Jifty::View::out_method);
my $ret = $self->dispatcher->handle_request();
return $ret if $ret; # if dispatcher returns a coderef,
# it's a streamy response
}
$self->call_trigger('before_cleanup', $req);
$self->cleanup_request();
$response = Jifty->web->response;
}
$self->call_trigger('after_request', $req);
return $response->finalize;
}
=head2 cleanup_request
Dispatchers should call this at the end of each request, as a class method.
It flushes the session to disk, as well as flushing L<Jifty::DBI>'s cache.
=cut
sub cleanup_request {
my $self = shift;
# Clean out the cache. the performance impact should be marginal.
# Consistency is improved, too.
Jifty->web->session->unload();
Jifty::Record->flush_cache if Jifty::Record->can('flush_cache');
$self->stash(undef);
$self->buffer->pop for 1 .. $self->buffer->depth;
$self->buffer->clear;
}
1;
|