/usr/share/perl5/Magpie/Matcher.pm is in libmagpie-perl 1.141660-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 | package Magpie::Matcher;
#ABSTRACT: Multi-purpose Dispatcher Magic
$Magpie::Matcher::VERSION = '1.141660';
use Moose;
use Scalar::Util qw(reftype);
use HTTP::Negotiate;
use Data::Printer;
has plack_request => (
is => 'ro',
isa => 'Plack::Request',
required => 1,
trigger => \&choose_variant,
);
has match_candidates => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[ArrayRef]',
default => sub { [] },
handles => {
add_candidates => 'push',
},
);
has evaluation_map => (
traits => ['Hash'],
is => 'rw',
isa => 'HashRef[ArrayRef]',
handles => {
has_candidate => 'exists',
get_candidate => 'get',
},
lazy_build => 1,
);
sub _build_evaluation_map {
my $self = shift;
my $candidates = $self->match_candidates;
my $evaled = {};
my @submatches = ();
my @to_skip;
# first, eval the frames to see what matches.
foreach my $frame (@{$candidates}) {
my @components = ();
my $machine_token = $frame->[3],
my $match_token = $frame->[4];
$evaled->{$machine_token} ||= [];
my $added = $self->eval_match($frame);
if (scalar @{$added} > 0) {
my @subs_here = grep {/__MATCH__/} @{$added};
push @submatches, @subs_here;
push @{$evaled->{$machine_token}}, [$match_token, $added];
}
else {
my @subs_here = grep {/__MATCH__/} @{$frame->[2]};
push @to_skip, @subs_here;
}
}
# now that we know what matches, flatten the pipelines by resolving the
# match tokens
my $out = {};
foreach my $machine (keys( %{$evaled} )) {
$out->{$machine} = [];
foreach my $slot (@{$evaled->{$machine}}) {
next if scalar grep { $_ eq $slot->[0] } (@submatches, @to_skip);
my $components = resolve($evaled->{$machine}, $slot->[1]);
foreach my $component (@{$components}) {
if ($component eq '__RESET__') {
$out->{$machine} = [];
next;
}
push @{$out->{$machine}}, $component;
}
#push @{$out->{$machine}}, @{$components};
}
}
return $out;
}
has accept_matrix => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[ArrayRef]',
default => sub { [] },
);
has accept_variant => (
is => 'rw',
isa => 'Str|Undef',
#lazy_build => 1,
);
sub choose_variant {
my $self = shift;
my $plack_request = shift;
my $accept_matrix = $self->accept_matrix;
if ($accept_matrix) {
my $variant = HTTP::Negotiate::choose($accept_matrix, $plack_request->headers);
$self->accept_variant($variant);
}
}
sub _build_accept_variant {
my $self = shift;
my $ret = undef;
my $accept_matrix = $self->accept_matrix;
if ($accept_matrix) {
$ret = HTTP::Negotiate::choose($accept_matrix, $self->plack_request->headers);
}
return $ret;
}
sub resolve {
my $machine = shift;
my $component_list = shift;
my $stack = shift || [];
foreach my $component (@{$component_list}) {
if ($component =~ /__MATCH__/) {
my @new_list = ();
foreach my $thing (@{$machine}) {
if ($thing->[0] eq $component) {
push @new_list, @{$thing->[1]};
}
}
my $resolved = resolve($machine, \@new_list);
push @{$stack}, @{$resolved} if $resolved;
}
else {
push @{$stack}, $component;
}
}
return $stack;
}
sub eval_match {
my $self = shift;
my $frame = shift;
my $req = $self->plack_request;
my $env = $req->env;
my $path = $req->path_info;
my $accept_variant = $self->accept_variant;
my @out = ();
my $match_type = $frame->[0];
if ($match_type eq 'STRING') {
push @out, @{$frame->[2]} if $frame->[1] eq $path;
}
elsif ($match_type eq 'REGEXP' || ($match_type eq 'SCALAR' && re::is_regexp($frame->[0]) == 1 )) {
push @out, @{$frame->[2]} if $path =~ /$frame->[1]/;
}
elsif ($match_type eq 'CODE') {
my $temp = $frame->[1]->($env);
push @out, @{$temp};
}
elsif ($match_type eq 'HASH') {
my $rules = $frame->[1];
my $matched = 0;
foreach my $k (keys %{$rules} ) {
last unless defined $env->{$k};
my $val = $rules->{$k};
my $val_type = reftype $val;
if ($val_type &&
( $val_type eq 'REGEXP' || ($val_type eq 'SCALAR' && re::is_regexp($val) == 1 ))
) {
$matched++ if $env->{$k} =~ m/$val/;
}
else {
$matched++ if qq($env->{$k}) eq qq($val);
}
}
push @out, @{$frame->[2]} if $matched == scalar keys %{$rules};
}
elsif ($match_type eq 'AUTO') {
push @out, @{$frame->[2]};
}
elsif ($match_type eq 'ACCEPT') {
push @out, @{$frame->[2]} if length $accept_variant && $frame->[1] eq $accept_variant;
}
elsif ($match_type eq 'RESET') {
push @out, '__RESET__';
}
else {
warn"I don't know how to match '$match_type', skipping.\n"
}
return \@out;
}
sub machine_token_lookup {
return shift->token_lookup(@_, 3);
}
sub match_token_lookup {
return shift->token_lookup(@_, 4);
}
sub token_lookup {
my $self = shift;
my $token = shift || '__default__';
my $index = shift || 4;
my $candidates = $self->match_candidates;
my $ret = [];
foreach my $frame (@{$candidates}) {
if ($frame->[$index] eq $token) {
push @{$ret}, $frame;
}
}
return $ret;
}
sub construct_pipeline {
my $self = shift;
my $tokenized = shift;
unless ($tokenized) {
$tokenized = ['__default__'];
}
my @new = ();
my $map = $self->evaluation_map;
my @tokens = keys( %{$map} );
foreach my $step ( @{$tokenized} ) {
if ( grep { $_ eq $step } @tokens ) {
push @new, @{$map->{$step}};
}
else {
push @new, $step;
}
}
return \@new;
}
#SEEALSO: Magpie
1;
__END__
=pod
=head1 NAME
Magpie::Matcher - Multi-purpose Dispatcher Magic
=head1 VERSION
version 1.141660
=head1 AUTHORS
=over 4
=item *
Kip Hampton <kip.hampton@tamarou.com>
=item *
Chris Prather <chris.prather@tamarou.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Tamarou, LLC.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|