| 1 | package Plient::Handler::curl; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | require Plient::Util unless $Plient::bundle_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 9 | 5 | Plient::Util->import; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | my ( $curl, $curl_config, %all_protocol, %protocol, %method ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | %all_protocol = | ||||||||||||||||||||||||||||
| 14 | 5 | map { $_ => undef } qw/http https ftp ftps file telnet ldap dict tftp/; | |||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub all_protocol { return \%all_protocol } | ||||||||||||||||||||||||||
|
Subroutine
all_protocol: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub protocol { return \%protocol } | ||||||||||||||||||||||||||
|
Subroutine
protocol: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub method { return \%method } | ||||||||||||||||||||||||||
|
Subroutine
method: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | my $inited; | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | return if $inited; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | $inited = 1; | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | $curl = $ENV{PLIENT_CURL} || which('curl'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'PLIENT_CURL'} || which('curl')
MC/DC0%0/2
$ENV{'PLIENT_CURL'} || which('curl')
$ENV{'PLIENT_CURL'}
which('curl')
Truth table0%0/3
A || B
A= $ENV{'PLIENT_CURL'}B= which('curl')
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | $curl_config = $ENV{PLIENT_CURL_CONFIG} || which('curl-config'); | ||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'PLIENT_CURL_CONFIG'} || which('curl-config')
MC/DC0%0/2
$ENV{'PLIENT_CURL_CONFIG'} || which('curl-config')
$ENV{'PLIENT_CURL_CONFIG'}
which('curl-config')
Truth table0%0/3
A || B
A= $ENV{'PLIENT_CURL_CONFIG'}B= which('curl-config')
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return unless $curl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ($curl_config) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ( my $out = `$curl_config --protocols` ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | @protocol{ map { lc } split /\r?\n/, $out } = (); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | else { | ||||||||||||||||||||||||||||
| 31 | 0 | warn $!; | |||||||||||||||||||||||||||
| 32 | 0 | return; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||
| 36 | # by default, curl should support http | ||||||||||||||||||||||||||||
| 37 | 0 | %protocol = ( http => undef ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ( exists $protocol{http} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $method{http_get} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 42 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||
| 43 | 0 | my $headers = translate_headers( $args ); | |||||||||||||||||||||||||||
| 44 | 0 | my $auth = translate_auth($args); | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | warn "$curl -k -s -L $headers $auth $uri\n" if $ENV{PLIENT_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( open my $fh, "$curl -k -s -L $headers $auth '$uri' |" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | local $/; | |||||||||||||||||||||||||||
| 48 | 0 | <$fh>; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | else { | ||||||||||||||||||||||||||||
| 51 | 0 | warn "failed to get $uri with curl: $!"; | |||||||||||||||||||||||||||
| 52 | 0 | return; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $method{http_post} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 57 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $headers = translate_headers($args); | |||||||||||||||||||||||||||
| 61 | 0 | my $auth = translate_auth($args); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $data = ''; | |||||||||||||||||||||||||||
| 64 | 0 | my $post_opt; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $args->{content_type} && $args->{content_type} =~ /form-data/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"content_type"} and $args->{"content_type"} =~ /form-data/
MC/DC0%0/2
$args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args->{"content_type"}
$args->{"content_type"} =~ /form-data/
Truth table0%0/3
A and B
A= $args->{"content_type"}B= $args->{"content_type"} =~ /form-data/
| |||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 0 | $post_opt = '-F'; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 0 | $post_opt = '-d'; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $args->{body_array} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | my $body = $args->{body_array}; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | for ( my $i = 0 ; $i < $#$body ; $i += 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | my $key = $body->[$i]; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $value = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : ''; | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( ref $value eq 'HASH' && $value->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $value eq "HASH" and $value->{"file"}
MC/DC0%0/2
ref $value eq "HASH" and $value->{"file"}
ref $value eq "HASH"
$value->{"file"}
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= $value->{"file"}
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # file upload | ||||||||||||||||||||||||||||
| 83 | 0 | $data .= " $post_opt '$key=\@$value->{file}'"; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 0 | $data .= " $post_opt '$key=$value'"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | warn "$curl -s -L $data $headers $auth $uri\n" if $ENV{PLIENT_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( open my $fh, "$curl -s -L $data $headers $auth '$uri' |" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | local $/; | |||||||||||||||||||||||||||
| 94 | 0 | <$fh>; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | warn "failed to post $uri with curl: $!"; | |||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | }; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | $method{http_head} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 102 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||
| 103 | 0 | my $headers = translate_headers( $args ); | |||||||||||||||||||||||||||
| 104 | 0 | my $auth = translate_auth($args); | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | warn "$curl -s -I -L $headers $auth $uri\n" if $ENV{PLIENT_DEBUG}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ( open my $fh, "$curl -s -I -L $headers $auth '$uri' |" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | local $/; | |||||||||||||||||||||||||||
| 108 | 0 | my $head = <$fh>; | |||||||||||||||||||||||||||
| 109 | 0 | $head =~ s/\r\n$//; | |||||||||||||||||||||||||||
| 110 | 0 | return $head; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | 0 | warn "failed to get head of $uri with curl: $!"; | |||||||||||||||||||||||||||
| 114 | 0 | return; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 0 | }; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( exists $protocol{https} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | for my $m (qw/get post head put/) { | |||||||||||||||||||||||||||
| 121 | $method{"https_$m"} = $method{"http_$m"} | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if exists $method{"http_$m"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | return 1; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub translate_headers { | ||||||||||||||||||||||||||
|
Subroutine
translate_headers: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 130 | 0 | my $headers = $args->{headers}; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | return '' unless $headers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | my $str; | |||||||||||||||||||||||||||
| 133 | 0 | for my $k ( keys %$headers ) { | |||||||||||||||||||||||||||
| 134 | 0 | $str .= " -H '$k:$headers->{$k}'"; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | return $str; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub translate_auth { | ||||||||||||||||||||||||||
|
Subroutine
translate_auth: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 142 | 0 | my $auth = ''; | |||||||||||||||||||||||||||
| 143 | 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"}
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $method = lc( $args->{auth_method} || 'basic' ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'auth_method'} || 'basic'
MC/DC0%0/1
$args->{'auth_method'} || 'basic'
$args->{'auth_method'}
Truth table0%0/2
A
A= $args->{'auth_method'}
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ( $method eq 'basic' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | $auth = " -u '$args->{user}:$args->{password}'"; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||
| 149 | 0 | die "aborting: unsupported auth method: $method"; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 0 | return $auth; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 5 | ▶ | __PACKAGE__->_add_to_plient if $Plient::bundle_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 5 | 1; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||