| 1 | package Plient::Handler::LWP; | ||||||||||||||||||||||||||||||||||||||||||
| 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 ( $LWP, %all_protocol, %protocol, %method ); | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 5 | %all_protocol = map { $_ => undef } | |||||||||||||||||||||||||||||||||||||||||
| 10 | qw/http https ftp news gopher file mailto cpan data ldap nntp/; | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub all_protocol { return \%all_protocol } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_protocol: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub protocol { return \%protocol } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
protocol: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub method { return \%method } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
method: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | my $inited; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | return if $inited; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | $inited = 1; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | eval { require LWP::UserAgent } or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | undef $protocol{http}; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ( eval { require Crypt::SSLeay } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | undef $protocol{https}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $method{http_get} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # XXX TODO tweak the new arguments | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $ua->env_proxy; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | add_headers( $ua, $uri, $args ); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $res = $ua->get($uri); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return $res->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | warn "failed to get $uri with lwp: " . $res->status_line; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $method{http_post} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my ( $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # XXX TODO tweak the new arguments | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $ua->env_proxy; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | add_headers( $ua, $uri, $args ); | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $content = []; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $is_form_data; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( $args->{body} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$args->{"body"} and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args->{"body"} and $args->{"content_type"}
MC/DC0%0/3
$args->{"body"} and $args->{"content_type"} and $args->{"content_type"} =~ /form-data/
$args->{"body"}
$args->{"content_type"}
$args->{"content_type"} =~ /form-data/
Truth table0%0/4
A and B and C
A= $args->{"body"}B= $args->{"content_type"}C= $args->{"content_type"} =~ /form-data/
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | && $args->{content_type} | ||||||||||||||||||||||||||||||||||||||||||
| 56 | && $args->{content_type} =~ /form-data/ ) | ||||||||||||||||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $is_form_data = 1; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ( $args->{body_array} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $body = $args->{body_array}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | for ( my $i = 0 ; $i < $#$body ; $i += 2 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $key = $body->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $value = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | defined $body->[ $i + 1 ] ? $body->[ $i + 1 ] : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # file upload | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | push @$content, $key, [ $value->{file} ]; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | push @$content, $key, $value; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $content = $args->{body_array}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | my $res = $ua->post( | ||||||||||||||||||||||||||||||||||||||||||
| 82 | $uri, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 84 | $is_form_data | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ? ( Content_Type => 'form−data' ) | ||||||||||||||||||||||||||||||||||||||||||
| 86 | : () | ||||||||||||||||||||||||||||||||||||||||||
| 87 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $args->{body} ? ( Content => $content ) : (), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return $res->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | warn "failed to get $uri with lwp: " . $res->status_line; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # XXX there is no official way to get the *origin* header output :/ | ||||||||||||||||||||||||||||||||||||||||||
| 101 | # $res->headers->as_string isn't exactly the same head output | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # e.g. it adds Client-... headers, and lacking the first line: | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # HTTP/1.0 200 OK | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # | ||||||||||||||||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # $method{http_head} = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | # my ( $uri, $args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # my $ua = LWP::UserAgent->new; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # my $res = $ua->head($uri); | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # if ( $res->is_success ) { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # return $res->headers->as_string; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # else { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # warn "failed to get head of $uri with lwp: " . $res->status_line; | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # return; | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # }; | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( exists $protocol{https} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | # have you seen https is available while http is not? | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $method{https_get} = $method{http_get}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $method{https_post} = $method{http_post}; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub add_headers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_headers: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my ( $ua, $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | for my $k ( keys %$headers ) { | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | $ua->default_header( $k, $headers->{$k} ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( $method eq 'basic' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | require MIME::Base64; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $ua->default_header( | |||||||||||||||||||||||||||||||||||||||||
| 140 | "Authorization", | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 'Basic ' | ||||||||||||||||||||||||||||||||||||||||||
| 142 | . MIME::Base64::encode_base64( | ||||||||||||||||||||||||||||||||||||||||||
| 143 | "$args->{user}:$args->{password}", '' | ||||||||||||||||||||||||||||||||||||||||||
| 144 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 145 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | die "aborting: unsupported auth method: $method"; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 5 | ▶ | __PACKAGE__->_add_to_plient if $Plient::bundle_mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||||||||||||||||