/usr/share/perl5/Plack/Builder/Conditionals.pm is in libplack-builder-conditionals-perl 0.05-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 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 | package Plack::Builder::Conditionals;
use strict;
use warnings;
use Net::CIDR::Lite;
use List::MoreUtils qw//;
use Plack::Util;
use Plack::Middleware::Conditional;
our $VERSION = '0.05';
sub import {
my $class = shift;
my $caller = caller;
my %args = @_;
my @EXPORT = qw/match_if addr path method header browser any all/;
no strict 'refs';
my $i=0;
for my $sub (@EXPORT) {
my $sub_name = $args{'-prefix'} ? $args{'-prefix'} . '_' . $sub : $sub;
*{"$caller\::$sub_name"} = \&$sub;
}
}
sub match_if {
my $condition = shift;
my ( $mw, @args ) = @_;
if (ref $mw ne 'CODE') {
my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware');
$mw = sub { $mw_class->wrap($_[0], @args) };
}
return sub {
Plack::Middleware::Conditional->wrap(
$_[0],
condition => $condition,
builder => $mw
);
}
}
sub addr {
my $not;
my $ip;
if ( @_ == 1 ) {
$ip = $_[0];
}
else {
$not = $_[0];
$ip = $_[1];
}
my @ip = ref $ip ? @$ip : ($ip);
my $cidr4 = Net::CIDR::Lite->new();
my $cidr6 = Net::CIDR::Lite->new();
for my $ip ( @ip ) {
if ( $ip =~ m!:! ) {
$cidr6->add_any($ip);
}
else {
$cidr4->add_any($ip);
}
}
return sub {
my $env = shift;
my $find_ip;
if ( $env->{REMOTE_ADDR} =~ m!:! ) {
$find_ip = $cidr6->find($env->{REMOTE_ADDR});
}
else {
$find_ip = $cidr4->find($env->{REMOTE_ADDR});
}
return (defined $not && $not eq '!') ? !$find_ip : $find_ip;
};
}
sub _match {
my $key = shift;
my $not;
my $val;
if ( @_ == 1 ) {
$val = $_[0];
}
else {
$not = $_[0];
$val = $_[1];
}
return sub {
my $env = shift;
my $ret;
if ( ref $val && ref $val eq 'Regexp' ) {
$ret = exists $env->{$key} && $env->{$key} =~ m!$val!;
}
elsif ( defined $val ) {
$ret = exists $env->{$key} && $env->{$key} eq $val;
}
else {
$ret = exists $env->{$key};
}
return ( defined $not && $not eq '!' ) ? !$ret : $ret;
};
}
sub path {
_match( 'PATH_INFO', @_ );
}
sub method {
my $not;
my $method;
if ( defined $_[0] && $_[0] eq '!' ) {
$not = shift;
}
return sub { 1 } unless @_;
if ( @_ == 1 ) {
$method = $_[0];
}
else {
my $alternatives = join '|', map { quotemeta($_) } @_;
$method = qr/^(?:$alternatives)$/i;
}
if ( defined $method && ! ref $method ) {
$method = uc $method;
}
_match( 'REQUEST_METHOD', grep { defined } $not, $method );
}
sub header {
my $header = shift;
$header =~ s/-/_/g;
$header = 'HTTP_' . uc($header);
_match( $header, @_ );
}
sub browser {
_match( "HTTP_USER_AGENT", @_ );
}
sub any {
my @match = @_;
return sub {
my $env = shift;
List::MoreUtils::any { $_->($env) } @match;
};
}
sub all {
my @match = @_;
return sub {
my $env = shift;
List::MoreUtils::all { $_->($env) } @match;
};
}
1;
__END__
=head1 NAME
Plack::Builder::Conditionals - Plack::Builder extension
=head1 SYNOPSIS
use Plack::Builder;
use Plack::Builder::Conditionals;
# exports "match_if, addr, path, method, header, browser, any, all"
builder {
enable match_if addr(['192.168.0.0/24','127.0.0.1']),
"Plack::Middleware::ReverseProxy";
enable match_if all( path(qr!^/private!), addr( '!', [qw!127.0.0.1 ::1!] ) ),
"Plack::Middleware::Auth::Basic", authenticator => \&authen_cb;
enable match_if sub { my $env = shift; $env->{HTTP_X_ENABLE_FRAMEWORK} },
"Plack::Middleware::XFramework", framework => 'Test';
$app;
};
use Plack::Builder::Conditionals -prefx => 'c';
# exports "c_match_if, c_addr, c_path, c_method, c_header, c_any, c_all"
=head1 DESCRIPTION
Plack::Builder::Conditionals is..
=head1 FUNCTIONS
=over 4
=item match_if
enable match_if addr('127.0.0.1'), "Plack::Middleware::ReverseProxy";
enable match_if sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' }, "Plack::Middleware::ReverseProxy";
As like Plack::Builder's enable_if enable middleware if given conditions return true
=item addr
addr('127.0.0.1');
addr([qw!192.168.0.0/24 127.0.0.1 ::1!]);
addr('!','127.0.0.1');
return true if REMOTE_ADDR is found in the CIDR range. If first argument is '!', return the opposite result.
This function supports IPv6 addresses
=item path
path('/')
path(qr!^/(\w+)/!)
path('!', qr!^/private!)
matching PATH_INFO
=item method
method('GET')
method(qr/^(get|head)$/i)
method(qw(GET HEAD))
method('!','GET')
method('!', qr/^(post|put)$/i)
method('!', qw(POST PUT))
=item header
header('User-Agent',qr/iphone/)
header('If-Modified-Since') #exists check
header('!', 'User-Agent',qr/MSIE/)
=item browser
browser(qr/\bMSIE (7|8)/)
browser('!',qr!^Mozilla/4!);
Shortcut for header('User-Agent')
=item all
all( method('GET'), path(qr!^/static!) )
return true if all conditions are return true
=item any
any( path(qr!^/static!), path('/favicon.ico') )
return true if any condition return true
=back
=head1 EXPORT
use Plack::Builder::Conditionals -prefx => 'c';
# exports "c_match_if, c_addr, c_path, c_method, c_header, c_any, c_all"
you can add selected prefix to export functions
=head1 AUTHOR
Masahiro Nagano E<lt>kazeburo {at} gmail.comE<gt>
=head1 SEE ALSO
L<Plack::Builder>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|