blib/lib/APR/Emulate/PSGI.pm

filter: Statementstmt 73.3% 102 / 139 Branchbran 59.0% 26 / 44 Conditioncond 34.1% 14 / 41 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 64.8% 24 / 37 Podpod 100% 18 / 18 totaltotal 61.4% 185 / 301 CC 44 SCAR 15.7
CC
44
Cov
63%
parsed_uri
20.0
_send_http_headers
8.1
uri
6.0
CRAP 4.8
↑ Summary
1 package APR::Emulate::PSGI;
2  
3 =head1 NAME
4  
5 APR::Emulate::PSGI - Class that Emulates the mod_perl2 APR Object (Apache2::RequestRec, et al)
6  
7 =head1 SYNOPSIS
8  
9 use APR::Emulate::PSGI;
10 my $r = APR::Emulate::PSGI->new($psgi_env);
11  
12 # Or in a CGI environment:
13 my $r = APR::Emulate::PSGI->new();
14  
15 =head1 DESCRIPTION
16  
17 This class emulates the mod_perl2 APR object. It expects either a
18 PSGI environment hashref to be passed in, or to read HTTP environment
19 information from the global %ENV.
20  
21 Currently this module is little more than a proof of concept. There
22 are rough edges.
23  
24 Use at your own discretion. Contributions welcome.
25  
26 =cut
27  
28 3 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # APR::MyPool defined below this package.
36 # APR::MyTable defined below this package.
37  
38 3 our $VERSION = '0.03';
39  
40 # TODO Replace //= with something 5.6.0 appropriate.
41  
42 =head1 METHODS
43  
44 =over 4
45  
46 =item new
47  
48 Creates an object that emulates the mod_perl2 APR object.
49  
50 my $r = APR::Emulate::PSGI->new($psgi_env);
51  
52 HTTP environment information is read from the PSGI environment that is
53 passed in as a parameter. If no PSGI environment is supplied,
54 environment information is read from the global %ENV.
55  
56 =cut
57  
58 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
59 2 my ( $class, $env ) = @_;
60 2 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
defined $env ? : 1 1 2
61 'psgi_env' => $env,
62 'cgi_mode' => ( defined($env) ? 0 : 1 ),
63 }, $class;
64 2 return $self;
65 }
66  
67 =item psgi_status
68  
69 Returns the numeric HTTP response that should be used when building
70 a PSGI response.
71  
72 my $status = $r->psgi_status();
73  
74 The value is determined by looking at the current value of L</status_line>,
75 or if that is not set, the current value of L</status>, or if that is not
76 set, defaults to 200.
77  
78 =cut
79  
80 4 ▶sub psgi_status {
Subroutine psgi_status: called CC=3 SCAR=12.0
Pod documented
81 4 my ($self) = @_;
82 4 ▶ my $status = $self->status_line() || $self->status() || '200';
Condition40%2/5
$self->status_line || $self->status || '200'
l!l
04
$self->status_line or $self->status
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->status_line || $self->status || '200'
$self->status_line $self->status
Truth table33%1/3
A or B
A= $self->status_lineB= $self->status
ABresult
1X1
011
000
83 4 $status =~ s/\D//g;
84 4 return $status;
85 }
86  
87 =item psgi_headers
88  
89 Returns an arrayref of headers which can be used when building a PSGI
90 response.
91  
92 A Content-Length header is not included, and must be added in accordance
93 with the L<PSGI> specification, while building the PSGI response.
94  
95 my $headers_arrayref = $r->psgi_headers();
96  
97 =cut
98  
99 3 ▶sub psgi_headers {
Subroutine psgi_headers: called CC=5 SCAR=17.6
Pod documented
100 3 my ($self) = @_;
101 3 my @headers = ();
102  
103 3 my $status = $self->psgi_status();
104 3 ▶ if ($status eq '204' || $status eq '304' || $status =~ /^1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq '204' or $status eq '304' or $status =~ /^1/) { } 0 3 3
Condition33%2/6
$status eq 204 or $status eq 304 or $status =~ /^1/
l!l&&r!l&&!r
003
$status eq 204 or $status eq 304
l!l&&r!l&&!r
003
MC/DC0%0/3
$status eq 204 or $status eq 304 or $status =~ /^1/
$status eq 204 $status eq 304 $status =~ /^1/
Truth table25%1/4
A or B or C
A= $status eq 204B= $status eq 304C= $status =~ /^1/
ABCresult
1XX1
01X1
0011
0000
105 # Must not return Content-Type header, per PSGI spec.
106 }
107 else {
108 # Add Content-Type header.
109 push @headers, (
110 'Content-Type',
111 3 ▶ ($self->{'content_type'} || 'text/html'),
Condition50%1/2
$self->{'content_type'} || 'text/html'
l!l
30
MC/DC0%0/1
$self->{'content_type'} || 'text/html'
$self->{'content_type'}
Truth table50%1/2
A
A= $self->{'content_type'}
Aresult
11
00
112 );
113 }
114  
115 # Add other headers that have been set.
116 $self->headers_out()->do(
117 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 5 my ($key, $value) = @_;
119 5 push @headers, $key, $value;
120 }
121 3 );
122  
123 3 return \@headers;
124 };
125  
126 =back
127  
128 =head2 Request Methods
129  
130 =over 4
131  
132 =item headers_in
133  
134 Emulates L<Apache2::RequestRec/headers_in>.
135  
136 =cut
137  
138 6 ▶sub headers_in {
Subroutine headers_in: called CC=5 SCAR=16.1
Pod documented
139 6 my ($self) = @_;
140 6 ▶ return $self->{'headers_in'} if (defined($self->{'headers_in'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"headers_in"} 4 2 6
141  
142 my $environment = $self->{'cgi_mode'}
143 ? \%ENV
144 2 ▶ : $self->{'psgi_env'};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cgi_mode'} ? : 1 1 2
145  
146 my %headers = (
147 2 map { $_ => $environment->{$_} }
148 46 grep { $_ =~ /^HTTPS?_/ }
149 2 keys %{ $environment }
150 );
151  
152 2 foreach my $field ('CONTENT_TYPE', 'CONTENT_LENGTH') {
153 4 ▶ $headers{$field} = $environment->{$field} if (defined($environment->{$field}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $environment->{$field} 4 0 4
154 }
155  
156 2 return $self->{'headers_in'} = HTTP::Headers->new(%headers);
157 }
158  
159 =item method
160  
161 Emulates L<Apache2::RequestRec/method>.
162  
163 =cut
164  
165 2 ▶sub method {
Subroutine method: called CC=2 SCAR=6.9
Pod documented
166 2 my ($self) = @_;
167 2 ▶ if ($self->{'cgi_mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi_mode"}) 1 1 2
168 1 return $ENV{'REQUEST_METHOD'};
169 }
170 1 return $self->{'psgi_env'}{'REQUEST_METHOD'};
171 }
172  
173 =item uri
174  
175 Emulates L<Apache2::RequestRec/uri>.
176  
177 =cut
178  
179 0 ▶sub uri {
Subroutine uri: not called CC=2 SCAR=17.9
Pod documented
180 0 my ($self) = @_;
181 0 ▶ if ($self->{'cgi_mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi_mode"}) 0 0 0
182 0 return $ENV{'PATH_INFO'};
183 }
184 0 return $self->{'psgi_env'}{'PATH_INFO'};
185 }
186  
187 =item parsed_uri
188  
189 Emulates L<Apache2::URI/parsed_uri>.
190  
191 =cut
192  
193 0 ▶sub parsed_uri {
Subroutine parsed_uri: not called CC=4 SCAR=30.0
Pod documented
194 0 my ($self) = @_;
195 0 ▶ if ($self->{'cgi_mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi_mode"}) 0 0 0
196 0 ▶ return $self->{'uri'} //= URI->new($ENV{'REQUEST_URI'});
Condition0%0/3
$self->{"uri"} //= "URI"->new($ENV{"REQUEST_URI"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"uri"} //= "URI"->new($ENV{"REQUEST_URI"})
$self->{"uri"} "URI"->new($ENV{"REQUEST_URI"})
Truth table0%0/3
A //= B
A= $self->{"uri"}B= "URI"->new($ENV{"REQUEST_URI"})
ABresult
1X1
011
000
197 }
198 0 ▶ return $self->{'uri'} //= URI->new($self->{'psgi_env'}{'REQUEST_URI'});
Condition0%0/3
$self->{"uri"} //= "URI"->new($self->{"psgi_env"}{"REQUEST_URI"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"uri"} //= "URI"->new($self->{"psgi_env"}{"REQUEST_URI"})
$self->{"uri"} "URI"->new($self->{"psgi_env"}{"REQUEST_URI"})
Truth table0%0/3
A //= B
A= $self->{"uri"}B= "URI"->new($self->{"psgi_env"}{"REQUEST_URI"})
ABresult
1X1
011
000
199 }
200  
201 =item args
202  
203 Emulates L<Apache2::RequestRec/args>.
204  
205 =cut
206  
207 0 ▶sub args {
Subroutine args: not called CC=2 SCAR=17.9
Pod documented
208 0 my ($self) = @_;
209 0 ▶ if ($self->{'cgi_mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi_mode"}) 0 0 0
210 0 return $ENV{'QUERY_STRING'};
211 }
212 0 return $self->{'psgi_env'}{'QUERY_STRING'};
213 }
214  
215 =item read
216  
217 Emulates L<Apache2::RequestIO/read>.
218  
219 =cut
220  
221 2 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod documented
222 2 my ($self, $buffer, $length, $offset) = @_;
223 2 ▶ $offset ||= 0;
Condition50%1/2
$offset ||= 0
l!l
02
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
224 # We use $_[1] instead of $buffer, because we need to modify the original instead of a copy.
225 2 ▶ if ($self->{'cgi_mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi_mode"}) 1 1 2
226 1 return CORE::read(\*STDIN, $_[1], $length, $offset);
227 }
228 1 return $self->{'psgi_env'}{'psgi.input'}->read($_[1], $length, $offset);
229 }
230  
231 =item pool
232  
233 Emulates L<Apache2::RequestRec/pool>.
234  
235 =cut
236  
237 0 ▶sub pool {
Subroutine pool: not called CC=2 SCAR=17.9
Pod documented
238 0 my ($self) = @_;
239 0 ▶ return $self->{'pool'} //= APR::MyPool->new();
Condition0%0/3
$self->{"pool"} //= "APR::MyPool"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"pool"} //= "APR::MyPool"->new
$self->{"pool"} "APR::MyPool"->new
Truth table0%0/3
A //= B
A= $self->{"pool"}B= "APR::MyPool"->new
ABresult
1X1
011
000
240 }
241  
242 =back
243  
244 =head2 Response Methods
245  
246 =over 4
247  
248 =item headers_out
249  
250 Emulates L<Apache2::RequestRec/headers_out>.
251  
252 =cut
253  
254 9 ▶sub headers_out {
Subroutine headers_out: called CC=2 SCAR=7.1
Pod documented
255 9 my ($self) = @_;
256 9 ▶ return $self->{'headers_out'} //= APR::MyTable::make();
Condition66%2/3
$self->{"headers_out"} //= APR::MyTable::make()
l!l&&r!l&&!r
720
MC/DC0%0/2
$self->{"headers_out"} //= APR::MyTable::make()
$self->{"headers_out"} APR::MyTable::make()
Truth table66%2/3
A //= B
A= $self->{"headers_out"}B= APR::MyTable::make()
ABresult
1X1
011
000
257 }
258  
259 =item err_headers_out
260  
261 Emulates L<Apache2::RequestRec/err_headers_out>.
262  
263 =cut
264  
265 0 ▶sub err_headers_out {
Subroutine err_headers_out: not called CC=2 SCAR=17.9
Pod documented
266 0 my ($self) = @_;
267 0 ▶ return $self->{'err_headers_out'} //= APR::MyTable::make();
Condition0%0/3
$self->{"err_headers_out"} //= APR::MyTable::make()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"err_headers_out"} //= APR::MyTable::make()
$self->{"err_headers_out"} APR::MyTable::make()
Truth table0%0/3
A //= B
A= $self->{"err_headers_out"}B= APR::MyTable::make()
ABresult
1X1
011
000
268 }
269  
270 =item no_cache
271  
272 Emulates L<Apache2::RequestUtil/no_cache>.
273  
274 =cut
275  
276 2 ▶sub no_cache {
Subroutine no_cache: called CC=5 SCAR=16.1
Pod documented
277 2 my ($self, $value) = @_;
278 2 ▶ my $previous_value = $self->{'no_cache'} || 0;
Condition100%2/2
$self->{'no_cache'} || 0
l!l
11
MC/DC100%1/1
$self->{'no_cache'} || 0
$self->{'no_cache'}
Truth table100%2/2
A
A= $self->{'no_cache'}
Aresult
11
00
279 2 ▶ $self->{'no_cache'} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 1 1 2
280 2 ▶ return $previous_value if ($previous_value eq $self->{'no_cache'});
Branch: 1 branch
BranchTrueFalseTotal
if $previous_value eq $self->{"no_cache"} 0 2 2
281  
282 # Set headers.
283 2 ▶ if ($self->{'no_cache'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'no_cache'}) { } 1 1 2
284 1 $self->headers_out()->add('Pragma' => 'no-cache');
285 1 $self->headers_out()->add('Cache-control' => 'no-cache');
286 }
287 # Unset headers.
288 else {
289 1 $self->headers_out()->unset('Pragma', 'Cache-control');
290 }
291 2 return $previous_value;
292 }
293  
294 =item status
295  
296 Emulates L<Apache2::RequestRec/status>.
297  
298 =cut
299  
300 5 ▶sub status {
Subroutine status: called CC=2 SCAR=7.1
Pod documented
301 5 my ($self, @value) = @_;
302 5 ▶ $self->{'status'} = $value[0] if scalar(@value);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @value 0 5 5
303 5 return $self->{'status'};
304 }
305  
306 =item status_line
307  
308 Emulates L<Apache2::RequestRec/status_line>.
309  
310 =cut
311  
312 5 ▶sub status_line {
Subroutine status_line: called CC=2 SCAR=7.1
Pod documented
313 5 my ($self, @value) = @_;
314 5 ▶ $self->{'status_line'} = $value[0] if scalar(@value);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @value 0 5 5
315 5 return $self->{'status_line'};
316 }
317  
318 =item content_type
319  
320 Emulates L<Apache2::RequestRec/content_type>.
321  
322 If no PSGI enviroment is provided to L</new>, calling this
323 method with a parameter will cause HTTP headers to be sent.
324  
325 =cut
326  
327 2 ▶sub content_type {
Subroutine content_type: called CC=3 SCAR=11.0
Pod documented
328 2 my ($self, @value) = @_;
329 2 ▶ if (scalar(@value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 2 0 2
330 2 $self->{'content_type'} = $value[0];
331  
332 2 ▶ if ($self->{'cgi_mode'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cgi_mode"}) 1 1 2
333 1 $self->_send_http_headers();
334 }
335 }
336 2 return $self->{'content_type'};
337 }
338  
339 1 ▶sub _send_http_headers {
Subroutine _send_http_headers: called CC=7 SCAR=20.9
340 1 my ($self) = @_;
341 1 ▶ return if ($self->{'headers_sent'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"headers_sent"} 0 1 1
342 1 ▶ if (my $status = $self->status_line() || $self->status() || '200 OK') {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = $self->status_line || $self->status || "200 OK") 1 0 1
Condition40%2/5
$self->status_line || $self->status || '200 OK'
l!l
01
$self->status_line or $self->status
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->status_line || $self->status || '200 OK'
$self->status_line $self->status
Truth table33%1/3
A or B
A= $self->status_lineB= $self->status
ABresult
1X1
011
000
343 1 ▶ my $url_scheme = uc($self->{'psgi_env'}{'psgi.url_scheme'} || 'http');
Condition50%1/2
$self->{'psgi_env'}{'psgi.url_scheme'} || 'http'
l!l
01
MC/DC0%0/1
$self->{'psgi_env'}{'psgi.url_scheme'} || 'http'
$self->{'psgi_env'}{'psgi.url_scheme'}
Truth table50%1/2
A
A= $self->{'psgi_env'}{'psgi.url_scheme'}
Aresult
11
00
344 1 print $url_scheme . '/1.1 ' . $status . "\n";
345 }
346 1 ▶ print 'Content-Type: ' . ($self->{'content_type'} || 'text/html') . "\n";
Condition50%1/2
$self->{'content_type'} || 'text/html'
l!l
10
MC/DC0%0/1
$self->{'content_type'} || 'text/html'
$self->{'content_type'}
Truth table50%1/2
A
A= $self->{'content_type'}
Aresult
11
00
347 $self->headers_out()->do(
348 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
349 1 my ($key, $value) = @_;
350 1 print join(': ', $key, $value) . "\n";
351 }
352 1 );
353 1 print "\n\n";
354 1 $self->{'headers_sent'} = 1;
355 1 return 1;
356 }
357  
358 =item print
359  
360 Emulates L<Apache2::RequestIO/print>.
361  
362 =cut
363  
364 2 ▶sub print {
Subroutine print: called CC=2 SCAR=7.1
Pod documented
365 2 my ($self, @content) = @_;
366 2 my $success = CORE::print @content;
367 2 ▶ return $success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 2 0 2
368 ? length(join('', @content))
369 : 0;
370 }
371  
372 =item rflush
373  
374 Emulates L<Apache2::RequestIO/rflush>.
375  
376 =cut
377  
378 0 ▶sub rflush {}
Subroutine rflush: not called CC=1 SCAR=0.0
Pod documented
379  
380 =back
381  
382 =cut
383  
384 # See APR::Table in mod_perl 2 distribution.
385 package APR::MyTable;
386  
387 2 ▶sub make {
Subroutine make: called CC=1 SCAR=0.0
388 2 return bless {}, __PACKAGE__;
389 }
390  
391 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
392 0 my ($self) = @_;
393 0 my %copy = %$self;
394 0 return bless \%copy, ref($self);
395 }
396  
397 0 ▶sub clear {
Subroutine clear: not called CC=2 SCAR=17.9
398 0 my ($self) = @_;
399 0 my (@keys) = keys %$self;
400 0 foreach my $key (@keys) {
401 0 delete $self->{$key};
402 }
403 0 return 1;
404 }
405  
406 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
407 0 my ($self, @pairs) = @_;
408 0 ▶ while (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 0 0 0
409 0 my ($key, $value) = splice(@pairs, 0, 2);
410 0 $self->{$key} = $value;
411 }
412 0 return 1;
413 }
414  
415 1 ▶sub unset {
Subroutine unset: called CC=2 SCAR=6.9
416 1 my ($self, @keys) = @_;
417 1 foreach my $key (@keys) {
418 2 delete $self->{$key};
419 }
420 1 return 1;
421 }
422  
423 4 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
424 # TODO: When implemented properly, this should allow duplicate keys.
425 4 my ($self, $key, $value) = @_;
426 4 $self->{$key} = $value;
427 4 return 1;
428 }
429  
430 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
431 # TODO: When implemented properly, this should allow duplicate keys.
432 0 my ($self, $key) = @_;
433 0 return $self->{$key};
434 }
435  
436 0 ▶sub merge {
Subroutine merge: not called CC=1 SCAR=6.9
437 # TODO: Not yet implemented.
438 0 return undef;
439 }
440  
441 4 ▶sub do {
Subroutine do: called CC=3 SCAR=11.1
442 4 my ($self, $code, @keys) = @_;
443 4 ▶ @keys = keys %$self if (scalar(@keys) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @keys == 0 4 0 4
444 4 foreach my $key (@keys) {
445 6 $code->($key, $self->{$key});
446 }
447 4 return 1;
448 }
449  
450 package APR::MyPool;
451  
452 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
453 0 bless {}, $_[0];
454 }
455  
456 0 ▶sub cleanup_register {
Subroutine cleanup_register: not called CC=2 SCAR=17.9
457 0 my ($self, $code, @args) = @_;
458 0 foreach my $arg (@args) {
459 0 $code->($arg);
460 }
461 0 return 1;
462 }
463  
464 3 1;
465 __END__
↑ Summary