| 1 | package Plient::Handler::HTTPTiny; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | require Plient::Handler unless $Plient::bundle_mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | our @ISA = 'Plient::Handler'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | 5 | my ( $HTTPTiny, %all_protocol, %protocol, %method ); | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | %all_protocol = ( http => undef ); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub all_protocol { return \%all_protocol } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_protocol: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub protocol { return \%protocol } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
protocol: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub method { return \%method } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub support_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
support_method: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | my ( $method, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | if ( $args | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args and $args->{"content_type"}
MC/DC0%0/3
$args and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args
$args->{"content_type"}
$args->{"content_type"} =~ /form-data/
Truth table0%0/4
A and B and C
A= $argsB= $args->{"content_type"}C= $args->{"content_type"} =~ /form-data/
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | && $args->{content_type} | ||||||||||||||||||||||||||||||||||||||||||
| 19 | && $args->{content_type} =~ 'form-data' ) | ||||||||||||||||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ( $ENV{http_proxy} && $ENV{http_proxy} =~ /@/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ENV{"http_proxy"} and $ENV{"http_proxy"} =~ /@/
MC/DC0%0/2
$ENV{"http_proxy"} and $ENV{"http_proxy"} =~ /@/
$ENV{"http_proxy"}
$ENV{"http_proxy"} =~ /@/
Truth table0%0/3
A and B
A= $ENV{"http_proxy"}B= $ENV{"http_proxy"} =~ /@/
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # HTTPTiny doesn't support proxy auth | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | return $class->SUPER::support_method(@_); | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | my $inited; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return if $inited; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $inited = 1; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | eval { require HTTP::Tiny } or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | undef $protocol{http}; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $method{http_get} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $http = HTTP::Tiny->new; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | add_headers( $http, $uri, $args ); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $http->{proxy} = $ENV{http_proxy} if $ENV{http_proxy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $res = $http->get($uri); | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( $res->{success} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return $res->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | warn "failed to get $uri with HTTP::Tiny: " . $res; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $method{http_post} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $http = HTTP::Tiny->new; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $http->proxy( $ENV{http_proxy} ) if $ENV{http_proxy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | add_headers( $http, $uri, $args ); | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $body; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( $args->{body_hash} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | for my $key ( keys %{$args->{body_hash}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 65 | # TODO uri escape key and value | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $val = $args->{body_hash}{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $body .= $body ? "&$key=$val" : "$key=$val"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | $http->{default_headers}{'Content-Type'} = | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 'application/x-www-form-urlencoded' | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | unless $http->{default_headers}{'Content-Type'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $res = $http->request( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 'POST', $uri, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | defined $body | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ? ( content => $body ) | ||||||||||||||||||||||||||||||||||||||||||
| 80 | : () | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $res->{success} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $res->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | warn "failed to post $uri with HTTP::Tiny: " . $res; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub add_headers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_headers: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my ( $http, $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $headers = $args->{headers} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'headers'} || {}
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | for my $k ( keys %$headers ) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $http->{default_headers}{$k} = $headers->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $args->{user} && defined $args->{password} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"user"} and defined $args->{"password"}
MC/DC0%0/2
$args->{"user"} and defined $args->{"password"}
$args->{"user"}
defined $args->{"password"}
Truth table0%0/3
A and B
A= $args->{"user"}B= defined $args->{"password"}
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $method = lc $args->{auth_method} || 'basic'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
lc $args->{'auth_method'} || 'basic'
MC/DC0%0/1
lc $args->{'auth_method'} || 'basic'
lc $args->{'auth_method'}
Truth table0%0/2
A
A= lc $args->{'auth_method'}
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ( $method eq 'basic' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | require MIME::Base64; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $http->{default_headers}{"Authorization"} = 'Basic ' | |||||||||||||||||||||||||||||||||||||||||
| 108 | . MIME::Base64::encode_base64( "$args->{user}:$args->{password}", | ||||||||||||||||||||||||||||||||||||||||||
| 109 | '' ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | die "aborting: unsupported auth method: $method"; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 5 | ▶ | __PACKAGE__->_add_to_plient if $Plient::bundle_mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||||||||||