/usr/share/perl5/Plack/Middleware/Conditional.pm is in libplack-perl 1.0047-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 | package Plack::Middleware::Conditional;
use strict;
use parent qw(Plack::Middleware);
use Plack::Util::Accessor qw( condition middleware builder );
sub prepare_app {
my $self = shift;
$self->middleware( $self->builder->($self->app) );
}
sub call {
my($self, $env) = @_;
my $app = $self->condition->($env) ? $self->middleware : $self->app;
return $app->($env);
}
1;
__END__
=head1 NAME
Plack::Middleware::Conditional - Conditional wrapper for Plack middleware
=head1 SYNOPSIS
use Plack::Builder;
builder {
enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } 'StackTrace', force => 1;
$app;
};
# or using the OO interface:
$app = Plack::Middleware::Conditional->wrap(
$app,
condition => sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' },
builder => sub { Plack::Middleware::StackTrace->wrap($_[0], force => 1) },
);
=head1 DESCRIPTION
Plack::Middleware::Conditional is a piece of meta-middleware, to run a
specific middleware component under runtime conditions. The goal of
this middleware is to avoid baking runtime configuration options in
individual middleware components, and rather share them as another
middleware component.
=head1 EXAMPLES
Note that some of the middleware component names are just made up for
the explanation and might not exist.
# Minify JavaScript if the browser is Firefox
enable_if { $_[0]->{HTTP_USER_AGENT} =~ /Firefox/ } 'JavaScriptMinifier';
# Enable Stacktrace when being accessed from the local network
enable_if { $_[0]->{REMOTE_ADDR} =~ /^10\.0\.1\.*/ } 'StackTrace';
# Work with other conditional setter middleware:
# Transcode Jpeg on the fly for mobile clients
builder {
enable 'MobileDetector';
enable_if { $_[0]->{'plack.mobile_detected'} }
'TranscodeJpeg', max_size => 30_000;
$app;
};
Note that in the last example I<MobileDetector> should come first
because the conditional check runs in I<pre-run> conditions, which is
from outer to inner: that is, from the top to the bottom in the
Builder DSL code.
=head1 AUTHOR
Tatsuhiko Miyagawa
Steve Cook
=head1 SEE ALSO
L<Plack::Builder>
=cut
|