/usr/share/perl5/Plack/App/Cascade.pm is in libplack-perl 1.0039-1.
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 | package Plack::App::Cascade;
use strict;
use base qw(Plack::Component);
use Plack::Util;
use Plack::Util::Accessor qw(apps catch codes);
sub add {
my $self = shift;
$self->apps([]) unless $self->apps;
push @{$self->apps}, @_;
}
sub prepare_app {
my $self = shift;
my %codes = map { $_ => 1 } @{ $self->catch || [ 404 ] };
$self->codes(\%codes);
}
sub call {
my($self, $env) = @_;
return sub {
my $respond = shift;
my $done;
my $respond_wrapper = sub {
my $res = shift;
if ($self->codes->{$res->[0]}) {
# suppress output by giving the app an
# output spool which drops everything on the floor
return Plack::Util::inline_object
write => sub { }, close => sub { };
} else {
$done = 1;
return $respond->($res);
}
};
my @try = @{$self->apps || []};
my $tries_left = 0 + @try;
if (not $tries_left) {
return $respond->([ 404, [ 'Content-Type' => 'text/html' ], [ '404 Not Found' ] ])
}
for my $app (@try) {
my $res = $app->($env);
if ($tries_left-- == 1) {
$respond_wrapper = sub { $respond->(shift) };
}
if (ref $res eq 'CODE') {
$res->($respond_wrapper);
} else {
$respond_wrapper->($res);
}
return if $done;
}
};
}
1;
__END__
=head1 NAME
Plack::App::Cascade - Cascadable compound application
=head1 SYNOPSIS
use Plack::App::Cascade;
use Plack::App::URLMap;
use Plack::App::File;
# Serve static files from multiple search paths
my $cascade = Plack::App::Cascade->new;
$cascade->add( Plack::App::File->new(root => "/www/example.com/foo")->to_app );
$cascade->add( Plack::App::File->new(root => "/www/example.com/bar")->to_app );
my $app = Plack::App::URLMap->new;
$app->map("/static", $cascade);
$app->to_app;
=head1 DESCRIPTION
Plack::App::Cascade is a Plack middleware component that compounds
several apps and tries them to return the first response that is not
404.
=head1 METHODS
=over 4
=item new
$app = Plack::App::Cascade->new(apps => [ $app1, $app2 ]);
Creates a new Cascade application.
=item add
$app->add($app1);
$app->add($app2, $app3);
Appends a new application to the list of apps to try. You can pass the
multiple apps to the one C<add> call.
=item catch
$app->catch([ 403, 404 ]);
Sets which error codes to catch and process onwards. Defaults to C<404>.
=back
=head1 AUTHOR
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<Plack::App::URLMap> Rack::Cascade
=cut
|