| 1 | package LWP::UserAgent::Plugin; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 4 | 1 | our $DATE = '2020-08-13'; # DATE | |||||||||||||||||||||||||||
| 5 | 1 | our $DIST = 'LWP-UserAgent-Plugin'; # DIST | |||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.004'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use strict 'subs', 'vars'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use parent 'LWP::UserAgent'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | if ($ENV{LWP_USERAGENT_PLUGINS}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | require JSON::PP; | |||||||||||||||||||||||||||
| 17 | __PACKAGE__->set_plugins(@{ | ||||||||||||||||||||||||||||
| 18 | 0 | JSON::PP::decode_json($ENV{LWP_USERAGENT_PLUGINS}) | |||||||||||||||||||||||||||
| 19 | 0 | }); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 23 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 24 | 0 | ▶ | $class->set_plugins(@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my @plugins; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub set_plugins { | ||||||||||||||||||||||||||
|
Subroutine
set_plugins: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | my @old_plugins = @plugins; | |||||||||||||||||||||||||||
| 32 | 0 | @plugins = (); | |||||||||||||||||||||||||||
| 33 | 0 | while (1) { | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | last unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | my $arg = shift; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $class = ref $class eq 'ARRAY' ? $arg->[0] : $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $class = "LWP::UserAgent::Plugin::$class" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | unless $class =~ /\ALWP::UserAgent::Plugin::/; | ||||||||||||||||||||||||||||
| 39 | 0 | (my $class_pm = "$class.pm") =~ s!::!/!g; | |||||||||||||||||||||||||||
| 40 | 0 | require $class_pm; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $config = ref $arg eq 'ARRAY' ? $arg->[1] : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | ref($_[0]) eq 'HASH' ? shift : {}; | ||||||||||||||||||||||||||||
| 43 | 0 | push @plugins, [$class, $config]; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 0 | @old_plugins; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub _run_hooks { | ||||||||||||||||||||||||||
|
Subroutine
_run_hooks: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 49 | 0 | my ($self, $hook, $opts, $r) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | log_trace "[LWP::UserAgent::Plugin] -> running hook %s", $hook if $ENV{LWP_USERAGENT_PLUGIN_TRACE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $status; | |||||||||||||||||||||||||||
| 54 | 0 | for my $p (@plugins) { | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | next unless $p->[0]->can($hook); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | log_trace "[LWP::UserAgent::Plugin] -> handler from plugin %s", $p->[0] if $ENV{LWP_USERAGENT_PLUGIN_TRACE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | local $r->{config} = $p->[1]; | |||||||||||||||||||||||||||
| 58 | 0 | local $r->{hook} = $hook; | |||||||||||||||||||||||||||
| 59 | 0 | $status = $p->[0]->$hook($r); | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | unless ($opts->{all}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | last unless $status == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | last if $status == 98 || $status == 99; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$status == 98 or $status == 99
MC/DC0%0/2
$status == 98 or $status == 99
$status == 98
$status == 99
Truth table0%0/3
A or B
A= $status == 98B= $status == 99
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $status //= -1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$status //= -1
MC/DC0%0/1
$status //= -1
$status
Truth table0%0/2
A
A= $status
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | log_trace "[LWP::UserAgent::Plugin] <- return hook %s, status %d", $hook, $status if $ENV{LWP_USERAGENT_PLUGIN_TRACE}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $status; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my $r = {ua=>$_[0], argv=>[@_], method=>'request'}; | |||||||||||||||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | while (1) { | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | $r->{response} = $self->SUPER::request(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | unless $self->_run_hooks('before_request', {all=>1}, $r) == 99; | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | last unless $self->_run_hooks('after_request', {all=>1}, $r) == 98; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | $r->{response}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub mirror { | ||||||||||||||||||||||||||
|
Subroutine
mirror: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | my $r = {ua=>$_[0], argv=>[@_], method=>'mirror'}; | |||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | while (1) { | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | $r->{response} = $self->SUPER::mirror(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | unless $self->_run_hooks('before_mirror', {all=>1}, $r) == 99; | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | last unless $self->_run_hooks('after_mirror', {all=>1}, $r) == 98; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | $r->{response}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | 1; | |||||||||||||||||||||||||||
| 95 | # ABSTRACT: LWP::UserAgent with plugins | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||