blib/lib/Plack/Middleware/Magpie.pm

filter: Statementstmt 90.0% 162 / 180 Branchbran 76.6% 23 / 30 Conditioncond 67.8% 19 / 28 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 90.0% 27 / 30 Podpod 10.0% 1 / 10 totaltotal 81.2% 239 / 294 CC 30 SCAR 23.4
CC
30
Cov
86%
call
24.6
match_env
4.5
match
4.2
CRAP 10.4
« blib/lib/Magpie/Transformer/XSP.pm ↑ Summary lib/Magpie/Transformer/JSON.pm »
1 package Plack::Middleware::Magpie;
2 45 $Plack::Middleware::Magpie::VERSION = '1.163200';
3 # ABSTRACT: Plack Middleware Interface For Pipelined Magpie Applications
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use parent qw( Exporter Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 45 ▶use Plack::Util::Accessor qw(pipeline resource assets context conf accept_matrix config_cache config_assets plugins matcher_class debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 45 our @EXPORT = qw( machine match match_env match_accept match_template reset_pipeline);
11 45 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use Magpie::Machine;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #use Magpie::Matcher;
14 45 ▶use Magpie::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 45 ▶use Magpie::Plugin::URITemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use Magpie::ConfigReader::XML;
17 45 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 45 ▶use HTTP::Throwable::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 45 ▶use Data::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 45 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 45 my @STACK = ();
22 45 my $MTOKEN = undef;
23 45 my $IDX = 0;
24 29 ▶my $_add_frame = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 29 my $frame = shift;
26 29 push @$frame, ++$IDX;
27 29 push @STACK, $frame;
28 45 };
29  
30 13 ▶sub make_machine_token {
Subroutine make_machine_token: called CC=1 SCAR=0.0
Pod undocumented
31 13 return '__MTOKEN__' . int( rand(100000) );
32 }
33  
34 29 ▶sub make_match_token {
Subroutine make_match_token: called CC=1 SCAR=0.0
Pod undocumented
35 29 return '__MATCH__' . int( rand(100000) );
36 }
37  
38 13 ▶sub machine (&) {
Subroutine machine: called CC=1 SCAR=0.0
Pod undocumented
39 13 my $block = shift;
40 13 $MTOKEN = make_machine_token();
41 13 $block->();
42 13 return $MTOKEN;
43 }
44  
45 16 ▶sub match ($$) {
Subroutine match: called CC=4 SCAR=14.5
Pod undocumented
46 16 my $to_match = shift;
47 16 my $input = shift;
48 16 my $match_token = make_match_token;
49 16 ▶ my $match_type = reftype $to_match || 'STRING';
Condition100%2/2
builtin::reftype($to_match) || 'STRING'
l!l
142
MC/DC100%1/1
builtin::reftype($to_match) || 'STRING'
builtin::reftype($to_match)
Truth table100%2/2
A
A= builtin::reftype($to_match)
Aresult
11
00
50 16 ▶ if ( $match_type eq 'SCALAR' && re::is_regexp($to_match) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_type eq "SCALAR" and &re::is_regexp($to_match) == 1) 0 16 16
Condition33%1/3
$match_type eq "SCALAR" and &re::is_regexp($to_match) == 1
!ll&&!rl&&r
1600
MC/DC0%0/2
$match_type eq "SCALAR" and &re::is_regexp($to_match) == 1
$match_type eq "SCALAR" &re::is_regexp($to_match) == 1
Truth table33%1/3
A and B
A= $match_type eq "SCALAR"B= &re::is_regexp($to_match) == 1
ABresult
0X0
100
111
51 0 $match_type = 'REGEXP';
52 }
53 16 my $frame = [ $match_type, $to_match, $input, $MTOKEN, $match_token];
54 16 $_add_frame->($frame);
55 16 return $match_token;
56 }
57  
58 5 ▶sub match_env {
Subroutine match_env: called CC=4 SCAR=15.0
Pod undocumented
59 5 my $to_match = shift;
60 5 my $input = shift;
61 5 ▶ my $match_type = reftype $to_match || 'STRING';
Condition50%1/2
builtin::reftype($to_match) || 'STRING'
l!l
50
MC/DC0%0/1
builtin::reftype($to_match) || 'STRING'
builtin::reftype($to_match)
Truth table50%1/2
A
A= builtin::reftype($to_match)
Aresult
11
00
62 5 my $match_token = make_match_token;
63 5 ▶ if ( $match_type eq 'SCALAR' && re::is_regexp($to_match) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_type eq "SCALAR" and &re::is_regexp($to_match) == 1) 0 5 5
Condition33%1/3
$match_type eq "SCALAR" and &re::is_regexp($to_match) == 1
!ll&&!rl&&r
500
MC/DC0%0/2
$match_type eq "SCALAR" and &re::is_regexp($to_match) == 1
$match_type eq "SCALAR" &re::is_regexp($to_match) == 1
Truth table33%1/3
A and B
A= $match_type eq "SCALAR"B= &re::is_regexp($to_match) == 1
ABresult
0X0
100
111
64 0 $match_type = 'REGEXP';
65 }
66 5 my $frame = [ $match_type, $to_match, $input, $MTOKEN, $match_token ];
67 5 $_add_frame->($frame);
68 5 return $match_token;
69 }
70  
71 4 ▶sub match_accept {
Subroutine match_accept: called CC=1 SCAR=0.0
Pod undocumented
72 4 my $to_match = shift;
73 4 my $input = shift;
74 4 my $match_token = make_match_token;
75 4 my $frame = [ 'ACCEPT', $to_match, $input, $MTOKEN, $match_token ];
76 4 $_add_frame->($frame);
77 4 return $match_token;
78 }
79  
80 1 ▶sub reset_pipeline {
Subroutine reset_pipeline: called CC=1 SCAR=0.0
Pod undocumented
81 1 return '__RESET__';
82 }
83  
84  
85 # NOTE: We do this here (and the config file processor(s)) instead
86 # of in Matcher.pm so we don't have to reparse the template on every request.
87 # Consider benchmarking a fork that *does* do it in the Matcher at some point.
88 4 ▶sub match_template {
Subroutine match_template: called CC=3 SCAR=11.1
Pod undocumented
89 4 my $to_match = shift;
90 4 my $input = shift;
91 #warn "IN " . Dumper($input);
92 4 my $match_token = make_match_token;
93 4 my ($re, $names) = Magpie::Plugin::URITemplate::process_template($to_match);
94 4 my @tuples = Magpie::Util::make_tuples(@{$input});
95 4 my @new_input = ();
96 4 foreach my $pair (@tuples) {
97 6 ▶ if (defined $pair->[1]->{traits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pair->[1]{'traits'}) { } 0 6 6
98 0 push @{$pair->[1]->{traits}}, '+Magpie::Plugin::URITemplate';
99 }
100 else {
101 6 $pair->[1]->{traits} = ['+Magpie::Plugin::URITemplate'];
102 }
103  
104 6 $pair->[1]->{uri_template} = $to_match;
105 6 push @new_input, @{$pair};
106 }
107 4 my $frame = [ 'REGEXP', $re, \@new_input, $MTOKEN, $match_token ];
108 #warn "NEW FRAME" . Dumper($frame);
109 4 $_add_frame->($frame);
110 4 return $match_token;
111 }
112  
113 30 ▶sub has_config_cache {
Subroutine has_config_cache: called CC=1 SCAR=0.0
Pod undocumented
114 30 return defined shift->config_cache;
115 }
116  
117 115 ▶sub call {
Subroutine call: called CC=22 SCAR=32.0
Pod documented
118 115 my ( $self, $env ) = @_;
119  
120 #warn Dumper( $env );
121 115 my $app = $self->app;
122  
123 115 my @resource_handlers = ();
124 115 ▶ my $req = try { Plack::Request->new($env); }
Subroutine __ANON__: called CC=1 SCAR=0.0
125 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 warn "Error creating request object: '$_'\n";
127 0 HTTP::Throwable::Factory->throw( 400, { message => $_ } );
128 115 };
129  
130 115 ▶ my $pipeline = $self->pipeline || [];
Condition100%2/2
$self->pipeline || []
l!l
7837
MC/DC100%1/1
$self->pipeline || []
$self->pipeline
Truth table100%2/2
A
A= $self->pipeline
Aresult
11
00
131  
132 115 my $conf_file = $self->conf;
133 115 ▶ if ($conf_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf_file) 30 85 115
134  
135 # only XML for now, more options later?
136 30 my $file_meta = stat($conf_file);
137 30 ▶ if ( $self->has_config_cache
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_config_cache and $self->config_cache->{'mtime'} == $file_meta->mtime) { } 18 12 30
Condition66%2/3
$self->has_config_cache and $self->config_cache->{"mtime"} == $file_meta->mtime
!ll&&!rl&&r
12018
MC/DC50%1/2
$self->has_config_cache and $self->config_cache->{"mtime"} == $file_meta->mtime
$self->has_config_cache $self->config_cache->{"mtime"} == $file_meta->mtime
Truth table66%2/3
A and B
A= $self->has_config_cacheB= $self->config_cache->{"mtime"} == $file_meta->mtime
ABresult
0X0
100
111
138 && $self->config_cache->{mtime} == $file_meta->mtime )
139 {
140 18 my $cache = $self->config_cache;
141  
142 # XXX STACK is already global so caching a second time
143 # has shitty side effects.
144 #my @cached_stack = @{$cache->{match_stack}};
145 #unshift @STACK, @cached_stack;
146  
147 18 unshift @{$pipeline}, $cache->{token};
148 18 $self->accept_matrix( $cache->{accept_matrix} );
149 }
150 else {
151 12 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 12 Plack::Util::load_class("Magpie::ConfigReader::XML");
153 }
154 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 my $error = "Error loading ConfigReader: $_";
156 0 warn $error . "\n";
157 0 HTTP::Throwable::Factory->throw({ status_code => 500, reason => $error } );
158 12 };
159 12 my $reader = Magpie::ConfigReader::XML->new;
160 12 $reader->process($conf_file);
161  
162 12 my $token = $reader->make_token;
163  
164 # config-based handlers are added to the front
165 # of the stack so there can be a base reusable conf
166 # file with dynamic additions in the building class.
167 12 $self->config_cache(
168 {
169  
170 #match_stack => $reader->match_stack, #see the above XXX
171 token => $token,
172 accept_matrix => $reader->accept_matrix,
173 mtime => $file_meta->mtime,
174 assets => $reader->assets,
175 }
176 );
177  
178 12 unshift @STACK, @{ $reader->match_stack };
179 12 unshift @{$pipeline}, $reader->make_token;
180 12 $self->accept_matrix( $reader->accept_matrix );
181 12 $self->config_assets( $reader->assets );
182 }
183 }
184  
185 115 ▶ my $matcher_class = $self->matcher_class || 'Magpie::Matcher';
Condition100%2/2
$self->matcher_class || 'Magpie::Matcher'
l!l
1114
MC/DC100%1/1
$self->matcher_class || 'Magpie::Matcher'
$self->matcher_class
Truth table100%2/2
A
A= $self->matcher_class
Aresult
11
00
186  
187 115 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
188 115 Plack::Util::load_class($matcher_class);
189 }
190 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 my $error = "Error loading Matcher Class '$matcher_class': $_";
192 0 warn $error . "\n";
193 0 HTTP::Throwable::Factory->throw({ status_code => 500, reason => $error } );
194 115 };
195  
196 115 ▶ my $matcher = $matcher_class->new(
Condition100%2/2
$self->accept_matrix || []
l!l
4669
MC/DC100%1/1
$self->accept_matrix || []
$self->accept_matrix
Truth table100%2/2
A
A= $self->accept_matrix
Aresult
11
00
197 plack_request => $req,
198 accept_matrix => $self->accept_matrix || [],
199 match_candidates => \@STACK,
200 );
201  
202 115 $pipeline = $matcher->construct_pipeline($pipeline);
203  
204 115 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 115 115
205 0 Plack::Util::load_class('Data::Printer');
206 0 my $message = 'PIPELINE: ' . Data::Printer::p($pipeline);
207 0 warn $message . "\n";
208 0 $req->logger({ level => 'debug', message => $message, });
209 }
210  
211 115 my $m = Magpie::Machine->new( plack_request => $req, );
212  
213 115 my $resource = $self->resource;
214  
215 115 ▶ if ($resource) {
Branch: 2 branches
BranchTrueFalseTotal
if ($resource) { } 6 109 115
elsif (not grep({builtin::blessed($_) and $_->isa('Magpie::Resource');} @{$pipeline;})) { } 109 0 109
216 6 ▶ if ( ref($resource) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $resource eq 'HASH') { } 2 4 6
217  
218 # dont clobber the actual data
219 2 my %copy = %{$resource};
220 2 my $class = delete $copy{class};
221 2 push @resource_handlers, ( $class, \%copy );
222 }
223 else {
224 4 push @resource_handlers, $resource;
225 }
226 }
227 396 ▶ elsif ( !grep { blessed($_) && $_->isa('Magpie::Resource') } @{$pipeline} ) {
Condition33%1/3
builtin::blessed($_) && $_->isa('Magpie::Resource')
!ll&&!rl&&r
39600
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Magpie::Resource')
builtin::blessed($_) $_->isa('Magpie::Resource')
Truth table33%1/3
A && B
A= builtin::blessed($_)B= $_->isa('Magpie::Resource')
ABresult
0X0
100
111
228  
229 # If there is no Resource and nothing in the Pipeline then, really,
230 # we haven't found any way to prcoess the request. 404 is what most
231 # people would expect, I think.
232 109 ▶ unless ( scalar @{$pipeline} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$pipeline;}) 2 107 109
233 2 my $err = HTTP::Throwable::Factory->new_exception('NotFound');
234 2 return $err->();
235 }
236 107 push @resource_handlers, 'Magpie::Resource::Abstract';
237 }
238  
239 113 ▶ if ( my $assets = $self->assets ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $assets = $self->assets) 3 110 113
240 3 $m->assets($assets);
241 }
242  
243 113 ▶ my $config_assets = $self->config_assets || [];
Condition100%2/2
$self->config_assets || []
l!l
3083
MC/DC100%1/1
$self->config_assets || []
$self->config_assets
Truth table100%2/2
A
A= $self->config_assets
Aresult
11
00
244 113 foreach my $asset ( @{$config_assets} ) {
245 #warn "Adding asset" . Dumper($asset);
246 11 $m->add_asset( $asset );
247 }
248  
249 113 $m->pipeline( @resource_handlers, @{$pipeline} );
250  
251 # if we have upstream MW, pass it along
252 113 ▶ if ( ref($app) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app) 0 113 113
253 0 my $r = $app->($env);
254 0 $m->plack_response( Plack::Response->new(@$r) );
255 }
256  
257 113 ▶ $m->run( $self->context || {} );
Condition100%2/2
$self->context || {}
l!l
8033
MC/DC100%1/1
$self->context || {}
$self->context
Truth table100%2/2
A
A= $self->context
Aresult
11
00
258  
259 113 ▶ if ( $m->has_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->has_error) 5 108 113
260 5 my $subref = $m->error();
261 5 return $subref->();
262 }
263  
264 45 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 # XXX: Real Accept-* based serialization will go here eventually.
266 #if ($m->resource->has_data) {
267 108 ▶ if ( my $data = $m->resource->data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $m->resource->data) 36 72 108
268  
269 #my $data = $m->resource->data;
270 #warn "got data $data\n";
271 #if (!utf8::is_utf8($data)) {
272 # $data = decode('UTF-8', $data);
273 #}
274 36 ▶ my $content_length = length $data || 0;
Condition50%1/2
length $data || 0
l!l
360
MC/DC0%0/1
length $data || 0
length $data
Truth table50%1/2
A
A= length $data
Aresult
11
00
275 36 ▶ if ($content_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_length) 36 0 36
276 36 $m->response->content_length($content_length);
277 36 $m->plack_response->body($data);
278 }
279 }
280  
281 108 return $m->plack_response->finalize;
282 }
283  
284 # SEEALSO: Magpie, Plack, Plack::Middleware
285  
286 45 1;
287  
288 __END__
« blib/lib/Magpie/Transformer/XSP.pm ↑ Summary lib/Magpie/Transformer/JSON.pm »