blib/lib/LWP/Protocol/PSGI.pm

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