| 1 | package HTTP::Tiny::Plugin; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 4 | 1 | our $DATE = '2021-06-08'; # DATE | |||||||||||||||||||||||||||
| 5 | 1 | our $DIST = 'HTTP-Tiny-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 'HTTP::Tiny'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | if ($ENV{HTTP_TINY_PLUGINS}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | require JSON::PP; | |||||||||||||||||||||||||||
| 17 | __PACKAGE__->set_plugins(@{ | ||||||||||||||||||||||||||||
| 18 | 0 | JSON::PP::decode_json($ENV{HTTP_TINY_PLUGINS}) | |||||||||||||||||||||||||||
| 19 | 0 | }); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $r = {http=>undef, ua=>undef, argv=>[@_]}; | |||||||||||||||||||||||||||
| 26 | 0 | $class->_run_hooks('before_instantiate', {all=>1}, $r); | |||||||||||||||||||||||||||
| 27 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 28 | 0 | $r->{http} = $r->{ua} = $self; | |||||||||||||||||||||||||||
| 29 | 0 | $self->_run_hooks('after_instantiate', {all=>1}, $r); | |||||||||||||||||||||||||||
| 30 | 0 | $self; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 34 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | $class->set_plugins(@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my @plugins; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub set_plugins { | ||||||||||||||||||||||||||
|
Subroutine
set_plugins: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my @old_plugins = @plugins; | |||||||||||||||||||||||||||
| 43 | 0 | @plugins = (); | |||||||||||||||||||||||||||
| 44 | 0 | while (1) { | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | last unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | my $arg = shift; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $class = ref $class eq 'ARRAY' ? $arg->[0] : $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $class = "HTTP::Tiny::Plugin::$class" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | unless $class =~ /\AHTTP::Tiny::Plugin::/; | ||||||||||||||||||||||||||||
| 50 | 0 | (my $class_pm = "$class.pm") =~ s!::!/!g; | |||||||||||||||||||||||||||
| 51 | 0 | require $class_pm; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $config = ref $arg eq 'ARRAY' ? $arg->[1] : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | ref($_[0]) eq 'HASH' ? shift : {}; | ||||||||||||||||||||||||||||
| 54 | 0 | push @plugins, [$class, $config]; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | @old_plugins; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _run_hooks { | ||||||||||||||||||||||||||
|
Subroutine
_run_hooks: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 60 | 0 | my ($self, $hook, $opts, $r) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $status; | |||||||||||||||||||||||||||
| 63 | 0 | for my $p (@plugins) { | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | next unless $p->[0]->can($hook); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | local $r->{config} = $p->[1]; | |||||||||||||||||||||||||||
| 66 | 0 | local $r->{hook} = $hook; | |||||||||||||||||||||||||||
| 67 | 0 | $status = $p->[0]->$hook($r); | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | unless ($opts->{all}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | last unless $status == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 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
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $status // -1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$status // -1
MC/DC0%0/1
$status // -1
$status
Truth table0%0/2
A
A= $status
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $r = {http=>$_[0], ua=>$_[0], argv=>[@_]}; | |||||||||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | goto RETURN_RESPONSE | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if $self->_run_hooks('before_request_once', {all=>1}, $r) == 99; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | while (1) { | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | $r->{response} = $self->SUPER::request(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | unless $self->_run_hooks('before_request', {all=>1}, $r) == 99; | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | last unless $self->_run_hooks('after_request', {all=>1}, $r) == 98; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | RETURN_RESPONSE: | ||||||||||||||||||||||||||||
| 90 | 0 | $r->{response}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | 1; | |||||||||||||||||||||||||||
| 94 | # ABSTRACT: HTTP::Tiny with plugins | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||