| 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.09'; | |||||||||||||||||
| 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 | 17 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
register: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 17 | my $app = LWP::Protocol::PSGI::App->new(@_); | |||||||||||||||||
| 20 | 17 | unshift @apps, $app; | |||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | # register this guy (as well as saving original code) once | ||||||||||||||||||
| 23 | 17 | ▶ | if (! scalar keys %orig) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | 2 | for my $proto (@protocols) { | |||||||||||||||||
| 25 | 4 | ▶ | if (my $orig = LWP::Protocol::implementor($proto)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 30 | } | ||||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | } | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | 17 | ▶ | if (defined wantarray) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | return LWP::Protocol::PSGI::Guard->new(sub { | ||||||||||||||||||
| 36 | 17 | ▶ | $class->unregister_app($app); | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 37 | 17 | }); | |||||||||||||||||
| 38 | } | ||||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | sub unregister_app { | ||||||||||||||||||
| 42 | 17 | ▶ | my ($class, $app) = @_; | ||||||||||||||||
|
Subroutine
unregister_app: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 17 | my $i = 0; | |||||||||||||||||
| 45 | 17 | foreach my $stored_app (@apps) { | |||||||||||||||||
| 46 | 17 | ▶ | if ($app == $stored_app) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 47 | 17 | splice @apps, $i, 1; | |||||||||||||||||
| 48 | 17 | 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
| |||||||||||||||||||
| 59 | 0 | LWP::Protocol::implementor($proto, $orig{$proto}); | |||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | } | ||||||||||||||||||
| 62 | 0 | @apps = (); | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | sub request { | ||||||||||||||||||
| 66 | 10 | ▶ | my($self, $request) = @_; | ||||||||||||||||
|
Subroutine
request: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 10 | ▶ | if (my $app = $self->handles($request)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 10 | my $env = req_to_psgi $request; | |||||||||||||||||
| 70 | 10 | res_from_psgi $app->app->($env); | |||||||||||||||||
| 71 | } else { | ||||||||||||||||||
| 72 | 0 | $orig{$self->{scheme}}->new($self->{scheme}, $self->{ua})->request($request); | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | } | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | # for testing | ||||||||||||||||||
| 77 | sub create { | ||||||||||||||||||
| 78 | 0 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 79 | 0 | push @apps, LWP::Protocol::PSGI::App->new(@_); | |||||||||||||||||
| 80 | 0 | $class->new; | |||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | sub handles { | ||||||||||||||||||
| 84 | 21 | ▶ | my($self, $request) = @_; | ||||||||||||||||
|
Subroutine
handles: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 21 | foreach my $app (@apps) { | |||||||||||||||||
| 87 | 22 | ▶ | if ($app->match($request)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 88 | 18 | return $app; | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | package | ||||||||||||||||||
| 94 | LWP::Protocol::PSGI::Guard; | ||||||||||||||||||
| 95 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | sub new { | ||||||||||||||||||
| 98 | 17 | ▶ | my($class, $code) = @_; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 99 | 17 | bless $code, $class; | |||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | sub DESTROY { | ||||||||||||||||||
| 103 | 17 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 104 | 17 | $self->(); | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | package | ||||||||||||||||||
| 108 | LWP::Protocol::PSGI::App; | ||||||||||||||||||
| 109 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | sub new { | ||||||||||||||||||
| 112 | 17 | ▶ | my ($class, $app, %options) = @_; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 113 | 17 | bless { app => $app, options => \%options }, $class; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 10 | ▶ | sub app { $_[0]->{app} } | ||||||||||||||||
|
Subroutine
app: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 117 | 22 | ▶ | sub options { $_[0]->{options} } | ||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 118 | sub match { | ||||||||||||||||||
| 119 | 22 | ▶ | my ($self, $request) = @_; | ||||||||||||||||
|
Subroutine
match: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 120 | 22 | my $options = $self->options; | |||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 22 | ▶ | if ($options->{host}) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 123 | 12 | my $matcher = $self->_matcher($options->{host}); | |||||||||||||||||
| 124 | 12 | ▶ | $matcher->($request->uri->host) || $matcher->($request->uri->host_port); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 125 | } elsif ($options->{uri}) { | ||||||||||||||||||
| 126 | 4 | $self->_matcher($options->{uri})->($request->uri); | |||||||||||||||||
| 127 | } else { | ||||||||||||||||||
| 128 | 6 | 1; | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | } | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | sub _matcher { | ||||||||||||||||||
| 133 | 16 | ▶ | my($self, $stuff) = @_; | ||||||||||||||||
|
Subroutine
_matcher: called CC=4 SCAR=14.0
| |||||||||||||||||||
| 134 | 16 | ▶ | if (ref $stuff eq 'Regexp') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 135 | 3 | ▶ | sub { $_[0] =~ $stuff }; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 136 | } elsif (ref $stuff eq 'CODE') { | ||||||||||||||||||
| 137 | 3 | $stuff; | |||||||||||||||||
| 138 | } elsif (!ref $stuff) { | ||||||||||||||||||
| 139 | 10 | ▶ | sub { $_[0] eq $stuff }; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 140 | } else { | ||||||||||||||||||
| 141 | 0 | Carp::croak("Don't know how to match: ", ref $stuff); | |||||||||||||||||
| 142 | } | ||||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | 3 | 1; | |||||||||||||||||
| 146 | __END__ | ||||||||||||||||||