blib/lib/Perinci/Access/HTTP/Client.pm

filter: Statementstmt 17.9% 30 / 167 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 13.2% 42 / 316 CC 52 SCAR 60.0
CC
52
Cov
11%
request
831.0
new
182.0
__ANON__
59.5
CRAP 403.7
↑ Summary
1 package Perinci::Access::HTTP::Client;
2  
3 1 our $DATE = '2017-07-03'; # DATE
4 1 our $VERSION = '0.24'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Perinci::AccessUtil qw(strip_riap_stuffs_from_res);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use parent qw(Perinci::Access::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 my @logging_levels = keys %Log::ger::Levels;
18  
19 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
Pod documented
20 0 my $class = shift;
21  
22 0 my $self = $class->SUPER::new(@_);
23  
24 # attributes
25 0 ▶ $self->{retries} //= 2;
Condition0%0/2
$self->{"retries"} //= 2
l!l
00
MC/DC0%0/1
$self->{"retries"} //= 2
$self->{"retries"}
Truth table0%0/2
A
A= $self->{"retries"}
Aresult
11
00
26 0 ▶ $self->{retry_delay} //= 3;
Condition0%0/2
$self->{"retry_delay"} //= 3
l!l
00
MC/DC0%0/1
$self->{"retry_delay"} //= 3
$self->{"retry_delay"}
Truth table0%0/2
A
A= $self->{"retry_delay"}
Aresult
11
00
27 0 ▶ unless (defined $self->{log_level}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"log_level"}) 0 0 0
28 $self->{log_level} =
29 $ENV{TRACE} ? 6 :
30 $ENV{DEBUG} ? 5 :
31 $ENV{VERBOSE} ? 4 :
32 0 ▶ $ENV{QUIET} ? 2 :
Branch: 4 branches
BranchTrueFalseTotal
$ENV{'TRACE'} ? : 0 0 0
$ENV{'DEBUG'} ? : 0 0 0
$ENV{'VERBOSE'} ? : 0 0 0
$ENV{'QUIET'} ? : 0 0 0
33 0;
34 }
35 0 ▶ $self->{log_callback} //= undef;
Condition0%0/2
$self->{"log_callback"} //= undef
l!l
00
MC/DC0%0/1
$self->{"log_callback"} //= undef
$self->{"log_callback"}
Truth table0%0/2
A
A= $self->{"log_callback"}
Aresult
11
00
36 0 ▶ $self->{ssl_cert_file} //= $ENV{SSL_CERT_FILE};
Condition0%0/3
$self->{"ssl_cert_file"} //= $ENV{"SSL_CERT_FILE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ssl_cert_file"} //= $ENV{"SSL_CERT_FILE"}
$self->{"ssl_cert_file"} $ENV{"SSL_CERT_FILE"}
Truth table0%0/3
A //= B
A= $self->{"ssl_cert_file"}B= $ENV{"SSL_CERT_FILE"}
ABresult
1X1
011
000
37 0 ▶ $self->{ssl_ca_file} //= $ENV{SSL_CA_FILE};
Condition0%0/3
$self->{"ssl_ca_file"} //= $ENV{"SSL_CA_FILE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ssl_ca_file"} //= $ENV{"SSL_CA_FILE"}
$self->{"ssl_ca_file"} $ENV{"SSL_CA_FILE"}
Truth table0%0/3
A //= B
A= $self->{"ssl_ca_file"}B= $ENV{"SSL_CA_FILE"}
ABresult
1X1
011
000
38 0 ▶ $self->{user} //= $ENV{PERINCI_HTTP_USER};
Condition0%0/3
$self->{"user"} //= $ENV{"PERINCI_HTTP_USER"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"user"} //= $ENV{"PERINCI_HTTP_USER"}
$self->{"user"} $ENV{"PERINCI_HTTP_USER"}
Truth table0%0/3
A //= B
A= $self->{"user"}B= $ENV{"PERINCI_HTTP_USER"}
ABresult
1X1
011
000
39 0 ▶ $self->{password} //= $ENV{PERINCI_HTTP_PASSWORD};
Condition0%0/3
$self->{"password"} //= $ENV{"PERINCI_HTTP_PASSWORD"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"password"} //= $ENV{"PERINCI_HTTP_PASSWORD"}
$self->{"password"} $ENV{"PERINCI_HTTP_PASSWORD"}
Truth table0%0/3
A //= B
A= $self->{"password"}B= $ENV{"PERINCI_HTTP_PASSWORD"}
ABresult
1X1
011
000
40  
41 0 $self;
42 }
43  
44 # for older Perinci::Access::Base 0.28-, to remove later
45 0 ▶sub _init {}
Subroutine _init: not called CC=1 SCAR=0.0
46  
47 0 ▶sub request {
Subroutine request: not called CC=29 SCAR=67.2
Pod documented
48 0 my ($self, $action, $server_url, $extra, $copts) = @_;
49 0 ▶ $extra //= {};
Condition0%0/2
$extra //= {}
l!l
00
MC/DC0%0/1
$extra //= {}
$extra
Truth table0%0/2
A
A= $extra
Aresult
11
00
50 0 ▶ $copts //= {};
Condition0%0/2
$copts //= {}
l!l
00
MC/DC0%0/1
$copts //= {}
$copts
Truth table0%0/2
A
A= $copts
Aresult
11
00
51 0 log_trace(
52 "=> %s\::request(action=%s, server_url=%s, extra=%s)",
53 __PACKAGE__, $action, $server_url, $extra);
54 0 ▶ return [400, "Please specify server_url"] unless $server_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $server_url 0 0 0
55 my $rreq = { v=>$self->{riap_version},
56 0 ▶ action=>$action,
Condition0%0/2
$Perinci::Access::HTTP::Client::VERSION // "?"
l!l
00
MC/DC0%0/1
$Perinci::Access::HTTP::Client::VERSION // "?"
$Perinci::Access::HTTP::Client::VERSION
Truth table0%0/2
A
A= $Perinci::Access::HTTP::Client::VERSION
Aresult
11
00
57 ua=>"Perinci/".($Perinci::Access::HTTP::Client::VERSION//"?"),
58 %$extra };
59 0 my $res = $self->check_request($rreq);
60 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
61  
62 0 state $json = do {
63 0 require JSON::MaybeXS;
64 0 JSON::MaybeXS->new->allow_nonref;
65 };
66  
67 0 state $ua;
68 0 ▶ state $callback = sub {
Subroutine __ANON__: not called CC=8 SCAR=40.9
69 0 my ($resp, $ua, $h, $data) = @_;
70  
71 # we collect HTTP response body into __buffer first. if __mark is set
72 # then we need to separate each log message and response part.
73 # otherwise, everything just needs to go to __body.
74  
75 #$log->tracef("got resp: %s (%d bytes)", $data, length($data));
76 #say sprintf("D:got resp: %s (%d bytes)", $data, length($data));
77  
78 0 ▶ if ($ua->{__mark}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua->{'__mark'}) { } 0 0 0
79 0 $ua->{__buffer} .= $data;
80 0 ▶ if ($ua->{__buffer} =~ /\A([lr])(\d+) /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ua->{'__buffer'} =~ /\A([lr])(\d+) /) { } 0 0 0
81 0 my ($chtype, $chlen) = ($1, $2);
82 # not enough data yet
83 0 my $hlen = 1+length($chlen)+1;
84 0 ▶ return 1 unless length($ua->{__buffer}) >= $hlen + $chlen;
Branch: 1 branch
BranchTrueFalseTotal
unless length $ua->{"__buffer"} >= $hlen + $chlen 0 0 0
85 0 my $chdata = substr($ua->{__buffer}, $hlen, $chlen);
86 0 substr($ua->{__buffer}, 0, $hlen+$chlen) = "";
87 0 ▶ if ($chtype eq 'l') {
Branch: 2 branches
BranchTrueFalseTotal
if ($chtype eq 'l') { } 0 0 0
elsif ($chtype eq 'r') { } 0 0 0
88 0 ▶ if ($self->{log_callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_callback'}) { } 0 0 0
89 0 $self->{log_callback}->($chdata);
90 } else {
91 0 $chdata =~ s/^\[(\w+)\]//;
92 0 my $level = $1;
93 0 ▶ $level = "error" unless $level ~~ @logging_levels;
Branch: 1 branch
BranchTrueFalseTotal
unless $level ~~ \@logging_levels 0 0 0
94 0 my $logger_name = "log_$level";
95 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 0 &{$logger_name}("[$server_url] $chdata");
97 }
98 0 return 1;
99 } elsif ($chtype eq 'r') {
100 0 $ua->{__body} .= $chdata;
101 } else {
102 0 $ua->{__body} = "[500,\"Unknown chunk type $chtype".
103 0 "try updating ${\(__PACKAGE__)} version\"]";
104 0 return 0;
105 }
106 } else {
107 0 $ua->{__body} = "[500,\"Invalid response from server,".
108 " server is probably using older version of ".
109 "Riap::HTTP server library\"]";
110 0 return 0;
111 }
112 } else {
113 0 $ua->{__body} .= $data;
114 }
115 0 };
116  
117 0 ▶ if (!$ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 0 0 0
118 0 require LWP::UserAgent;
119 $ua = LWP::UserAgent->new(
120 ssl_opts => {
121 SSL_cert_file => $self->{ssl_cert_file},
122 SSL_ca_file => $self->{ssl_ca_file},
123 },
124 0 );
125 0 $ua->env_proxy;
126 $ua->set_my_handler(
127 0 ▶ "request_send", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 my ($req, $ua, $h) = @_;
129 0 $ua->{__buffer} = "";
130 0 $ua->{__body} = "";
131 0 });
132 $ua->set_my_handler(
133 0 ▶ "response_header", sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
134 0 my ($resp, $ua, $h) = @_;
135 0 ▶ if ($resp->header('x-riap-logging')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->header('x-riap-logging')) { } 0 0 0
136 0 $ua->{__mark} = 1;
137 } else {
138 0 $ua->{__log_level} = 0;
139 }
140 0 });
141 0 $ua->set_my_handler(
142 "response_data", $callback);
143 }
144  
145 0 ▶ my $authuser = $copts->{user} // $self->{user};
Condition0%0/3
$copts->{"user"} // $self->{"user"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$copts->{"user"} // $self->{"user"}
$copts->{"user"} $self->{"user"}
Truth table0%0/3
A // B
A= $copts->{"user"}B= $self->{"user"}
ABresult
1X1
011
000
146 0 ▶ my $authpass = $copts->{password} // $self->{password};
Condition0%0/3
$copts->{"password"} // $self->{"password"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$copts->{"password"} // $self->{"password"}
$copts->{"password"} $self->{"password"}
Truth table0%0/3
A // B
A= $copts->{"password"}B= $self->{"password"}
ABresult
1X1
011
000
147 0 ▶ if (defined $authuser) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authuser) 0 0 0
148 0 require URI;
149 0 my $suri = URI->new($server_url);
150 0 my $host = $suri->host;
151 0 my $port = $suri->port;
152 $ua->credentials(
153 "$host:$port",
154 0 ▶ $self->{realm} // "restricted area",
Condition0%0/2
$self->{"realm"} // "restricted area"
l!l
00
MC/DC0%0/1
$self->{"realm"} // "restricted area"
$self->{"realm"}
Truth table0%0/2
A
A= $self->{"realm"}
Aresult
11
00
155 $authuser,
156 $authpass,
157 );
158 }
159  
160 0 my $http_req = HTTP::Request->new(POST => $server_url);
161 0 for (keys %$rreq) {
162 0 ▶ next if /\A(?:args|fmt|loglevel|_.*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A(?:args|fmt|loglevel|_.*)\z/ 0 0 0
163 0 my $hk = "x-riap-$_";
164 0 my $hv = $rreq->{$_};
165 0 ▶ if (!defined($hv) || ref($hv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $hv or ref $hv) 0 0 0
Condition0%0/3
not defined $hv or ref $hv
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $hv or ref $hv
not defined $hv ref $hv
Truth table0%0/3
not(A) or B
A= not defined $hvB= ref $hv
ABresult
1X1
011
000
166 0 $hk = "$hk-j-";
167 0 $hv = $json->encode($hv);
168 }
169 0 $http_req->header($hk => $hv);
170 }
171 0 $ua->{__log_level} = $self->{log_level};
172 0 $http_req->header('x-riap-loglevel' => $ua->{__log_level});
173 0 $http_req->header('x-riap-fmt' => 'json');
174  
175 0 my %args;
176 0 ▶ if ($rreq->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rreq->{"args"}) 0 0 0
177 0 for (keys %{$rreq->{args}}) {
178 0 $args{$_} = $rreq->{args}{$_};
179 }
180 }
181 0 my $args_s = $json->encode(\%args);
182 0 $http_req->header('Content-Type' => 'application/json');
183 0 $http_req->header('Content-Length' => length($args_s));
184 0 $http_req->content($args_s);
185  
186 #use Data::Dump; dd $http_req;
187  
188 0 my $custom_lwp_imp;
189 0 ▶ if ($server_url =~ m!\Ahttps?:/[^/]!i) { # XXX we don't support https, rite?
Branch: 1 branch
BranchTrueFalseTotal
if ($server_url =~ m[\Ahttps?:/[^/]]i) 0 0 0
190 0 require LWP::Protocol::http::SocketUnixAlt;
191 0 $custom_lwp_imp = "LWP::Protocol::http::SocketUnixAlt";
192 }
193  
194 0 my $attempts = 0;
195 0 my $do_retry;
196 my $http_res;
197 0 while (1) {
198 0 $do_retry = 0;
199  
200 0 my $old_imp;
201 0 ▶ if ($custom_lwp_imp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_lwp_imp) 0 0 0
202 0 $old_imp = LWP::Protocol::implementor("http");
203 0 LWP::Protocol::implementor("http", $custom_lwp_imp);
204 }
205  
206 0 eval { $http_res = $ua->request($http_req) };
207 0 my $eval_err = $@;
208  
209 0 ▶ if ($old_imp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_imp) 0 0 0
210 0 LWP::Protocol::implementor("http", $old_imp);
211 }
212  
213 0 ▶ return [500, "Client died: $eval_err"] if $eval_err;
Branch: 1 branch
BranchTrueFalseTotal
if $eval_err 0 0 0
214  
215 0 ▶ if ($http_res->code >= 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_res->code >= 500) 0 0 0
216 0 log_warn("Network failure (%d - %s), retrying ...",
217 $http_res->code, $http_res->message);
218 0 $do_retry++;
219 }
220  
221 0 ▶ if ($do_retry && $attempts++ < $self->{retries}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_retry and $attempts++ < $self->{'retries'}) { } 0 0 0
Condition0%0/3
$do_retry and $attempts++ < $self->{"retries"}
!ll&&!rl&&r
000
MC/DC0%0/2
$do_retry and $attempts++ < $self->{"retries"}
$do_retry $attempts++ < $self->{"retries"}
Truth table0%0/3
A and B
A= $do_retryB= $attempts++ < $self->{"retries"}
ABresult
0X0
100
111
222 0 sleep $self->{retry_delay};
223 } else {
224 0 last;
225 }
226 }
227  
228 0 ▶ return [500, "Network failure: ".$http_res->code." - ".$http_res->message]
Branch: 1 branch
BranchTrueFalseTotal
unless $http_res->is_success 0 0 0
229 unless $http_res->is_success;
230  
231 # empty __buffer
232 0 ▶ $callback->($http_res, $ua, undef, "") if length($ua->{__buffer});
Branch: 1 branch
BranchTrueFalseTotal
if length $ua->{"__buffer"} 0 0 0
233  
234 0 ▶ return [500, "Empty response from server (1)"]
Branch: 1 branch
BranchTrueFalseTotal
unless length $http_res->content 0 0 0
235 if !length($http_res->content);
236 return [500, "Empty response from server (2)"]
237 0 ▶ unless length($ua->{__body});
Branch: 1 branch
BranchTrueFalseTotal
unless length $ua->{"__body"} 0 0 0
238  
239 0 eval {
240 #say "D:body=$ua->{__body}";
241 0 log_trace("body: %s", $ua->{__body});
242 0 $res = $json->decode($ua->{__body});
243 };
244 0 my $eval_err = $@;
245 0 ▶ return [500, "Invalid JSON from server: $eval_err"] if $eval_err;
Branch: 1 branch
BranchTrueFalseTotal
if $eval_err 0 0 0
246  
247 #use Data::Dump; dd $res;
248 0 strip_riap_stuffs_from_res($res);
249 }
250  
251 0 ▶sub parse_url {
Subroutine parse_url: not called CC=4 SCAR=30.0
Pod documented
252 0 require URI::Split;
253  
254 0 my ($self, $uri, $copts) = @_;
255 0 ▶ die "Please specify url" unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
256  
257 0 my $res = $self->request(info => $uri, {}, $copts);
258 0 ▶ die "Can't 'info' on $uri: $res->[0] - $res->[1]" unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
259  
260 0 my $resuri = $res->[2]{uri};
261 0 my ($sch, $auth, $path) = URI::Split::uri_split($resuri);
262 0 ▶ $sch //= "pl";
Condition0%0/2
$sch //= "pl"
l!l
00
MC/DC0%0/1
$sch //= "pl"
$sch
Truth table0%0/2
A
A= $sch
Aresult
11
00
263  
264 0 {proto=>$sch, path=>$path};
265 }
266  
267 1 1;
268 # ABSTRACT: Riap::HTTP client
269  
270 __END__
↑ Summary