/usr/share/perl5/Magpie/Matcher.pm is in libmagpie-perl 1.140280-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 | package Magpie::Matcher;
{
$Magpie::Matcher::VERSION = '1.140280';
}
#ABSTRACT: Multi-purpose Dispatcher Magic
use Moose;
use Scalar::Util qw(reftype);
use HTTP::Negotiate;
has plack_request => (
is => 'ro',
isa => 'Plack::Request',
required => 1,
);
has match_candidates => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[ArrayRef]',
default => sub { [] },
handles => {
add_candidates => 'push',
},
);
has accept_matrix => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[ArrayRef]',
default => sub { [] },
);
sub make_map {
my $self = shift;
my $candidates = $self->match_candidates;
my $req = $self->plack_request;
my $env = $req->env;
my $path = $req->path_info;
my $out = {};
# this is expensive, so only do it once
my $accept_variant = HTTP::Negotiate::choose($self->accept_matrix, $req->headers);
foreach my $frame (@{ $candidates }) {
#warn "frame " . Dumper($frame);
my $match_type = $frame->[0];
my $token = $frame->[3] || '__default__';
$out->{$token} ||= [];
if ($match_type eq 'STRING') {
push @{$out->{$token}}, @{$frame->[2]} if $frame->[1] eq $path;
}
elsif ($match_type eq 'REGEXP' || ($match_type eq 'SCALAR' && re::is_regexp($frame->[0]) == 1 )) {
push @{$out->{$token}}, @{$frame->[2]} if $path =~ /$frame->[1]/;
}
elsif ($match_type eq 'CODE') {
my $temp = $frame->[1]->($env);
push @{$out->{$token}}, @{$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->{$token}}, @{$frame->[2]} if $matched == scalar keys %{$rules};
}
elsif ($match_type eq 'AUTO') {
push @{$out->{$token}}, @{$frame->[2]};
}
elsif ($match_type eq 'ACCEPT') {
push @{$out->{$token}}, @{$frame->[2]} if length $accept_variant && $frame->[1] eq $accept_variant;
}
else {
warn "I don't know how to match '$match_type', skipping.\n"
}
}
return $out;
}
sub construct_pipeline {
my $self = shift;
my $tokenized = shift;
unless ($tokenized) {
$tokenized = ['__default__'];
}
my @new = ();
my $map = $self->make_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.140280
=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
|