blib/lib/LWP/Protocol/PSGI.pm

filter: Statementstmt 91.9% 80 / 87 Branchbran 75.0% 21 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.6% 22 / 23 Podpod 66.6% 4 / 6 totaltotal 88.1% 127 / 144 CC 19 SCAR 10.6
CC
19
Cov
88%
register
6.3
_matcher
4.0
match
4.0
CRAP 2.9
↑ Summary
1 package LWP::Protocol::PSGI;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = '0.11';
6  
7 4 ▶use parent qw(LWP::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use HTTP::Message::PSGI qw( req_to_psgi res_from_psgi );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 my @protocols = qw( http https );
12 4 my %orig;
13  
14 my @apps;
15  
16 20 ▶sub register {
Subroutine register: called CC=6 SCAR=18.4
Pod documented
17 20 my $class = shift;
18  
19 20 my $app = LWP::Protocol::PSGI::App->new(@_);
20 20 unshift @apps, $app;
21  
22 # register this guy (as well as saving original code) once
23 20 ▶ if (! scalar keys %orig) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %orig) 4 16 20
24 4 for my $proto (@protocols) {
25 8 ▶ if (my $orig = LWP::Protocol::implementor($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig = LWP::Protocol::implementor($proto)) { } 8 0 8
26 8 $orig{$proto} = $orig;
27 8 LWP::Protocol::implementor($proto, $class);
28 } else {
29 0 ▶ Carp::carp("LWP::Protocol::$proto is unavailable. Skip registering overrides for it.") if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
30 }
31 }
32 }
33  
34 20 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 18 2 20
35 18 ▶ return LWP::Protocol::PSGI::Guard->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 18 $class->unregister_app($app);
37 18 });
38 }
39 }
40  
41 18 ▶sub unregister_app {
Subroutine unregister_app: called CC=3 SCAR=11.3
Pod undocumented
42 18 my ($class, $app) = @_;
43  
44 18 my $i = 0;
45 18 foreach my $stored_app (@apps) {
46 18 ▶ if ($app == $stored_app) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app == $stored_app) 18 0 18
47 18 splice @apps, $i, 1;
48 18 return;
49 }
50 0 $i++;
51 }
52 }
53
54  
55 1 ▶sub unregister {
Subroutine unregister: called CC=3 SCAR=11.0
Pod documented
56 1 my $class = shift;
57 1 for my $proto (@protocols) {
58 2 ▶ if ($orig{$proto}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig{$proto}) 2 0 2
59 2 LWP::Protocol::implementor($proto, $orig{$proto});
60 }
61 }
62 1 @apps = ();
63 1 %orig = ();
64 }
65  
66 15 ▶sub request {
Subroutine request: called CC=2 SCAR=7.1
Pod documented
67 15 my($self, $request, $proxy, $arg, @rest) = @_;
68  
69 15 ▶ if (my $app = $self->handles($request)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $app = $self->handles($request)) { } 15 0 15
70 15 my $env = req_to_psgi $request;
71 15 my $response = res_from_psgi $app->app->($env);
72 15 my $content = $response->content;
73 15 $response->content('');
74 15 $self->collect_once($arg, $response, $content);
75 } else {
76 0 $orig{$self->{scheme}}->new($self->{scheme}, $self->{ua})->request($request, $proxy, $arg, @rest);
77 }
78 }
79  
80 # for testing
81 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
82 0 my $class = shift;
83 0 push @apps, LWP::Protocol::PSGI::App->new(@_);
84 0 $class->new;
85 }
86  
87 26 ▶sub handles {
Subroutine handles: called CC=3 SCAR=11.0
Pod undocumented
88 26 my($self, $request) = @_;
89  
90 26 foreach my $app (@apps) {
91 27 ▶ if ($app->match($request)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->match($request)) 23 4 27
92 23 return $app;
93 }
94 }
95 }
96  
97 package
98 LWP::Protocol::PSGI::Guard;
99 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 18 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
102 18 my($class, $code) = @_;
103 18 bless $code, $class;
104 }
105  
106 18 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
107 18 my $self = shift;
108 18 $self->();
109 }
110  
111 package
112 LWP::Protocol::PSGI::App;
113 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
116 20 my ($class, $app, %options) = @_;
117 20 bless { app => $app, options => \%options }, $class;
118 }
119  
120 15 ▶sub app { $_[0]->{app} }
Subroutine app: called CC=1 SCAR=0.0
121 27 ▶sub options { $_[0]->{options} }
Subroutine options: called CC=1 SCAR=0.0
122 27 ▶sub match {
Subroutine match: called CC=4 SCAR=13.9
123 27 my ($self, $request) = @_;
124 27 my $options = $self->options;
125  
126 27 ▶ if ($options->{host}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'host'}) { } 13 14 27
elsif ($options->{'uri'}) { } 4 10 14
127 13 my $matcher = $self->_matcher($options->{host});
128 13 ▶ $matcher->($request->uri->host) || $matcher->($request->uri->host_port);
Branch: 1 branch
BranchTrueFalseTotal
&$matcher($request->uri->host) or &$matcher($request->uri->host_port) 8 5 13
129 } elsif ($options->{uri}) {
130 4 $self->_matcher($options->{uri})->($request->uri);
131 } else {
132 10 1;
133 }
134 }
135  
136 17 ▶sub _matcher {
Subroutine _matcher: called CC=4 SCAR=14.0
137 17 my($self, $stuff) = @_;
138 17 ▶ if (ref $stuff eq 'Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $stuff eq 'Regexp') { } 3 14 17
elsif (ref $stuff eq 'CODE') { } 3 11 14
elsif (not ref $stuff) { } 11 0 11
139 3 ▶ sub { $_[0] =~ $stuff };
Subroutine __ANON__: called CC=1 SCAR=0.0
140 } elsif (ref $stuff eq 'CODE') {
141 3 $stuff;
142 } elsif (!ref $stuff) {
143 11 ▶ sub { $_[0] eq $stuff };
Subroutine __ANON__: called CC=1 SCAR=0.0
144 } else {
145 0 Carp::croak("Don't know how to match: ", ref $stuff);
146 }
147 }
148  
149 4 1;
150 __END__
↑ Summary