MToken/Client.pm

filter: Statementstmt 16.6% 41 / 247 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 81 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 39.3% 13 / 33 Podpod 100% 18 / 18 totaltotal 14.3% 72 / 503 CC 73 SCAR 50.8
CC
73
Cov
10%
new
420.0
tx
272.0
request
156.0
CRAP 160.6
« MToken.pm ↑ Summary MToken/Config.pm »
1 package MToken::Client; # $Id: Client.pm 107 2021-10-10 20:04:42Z minus $
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use feature qw/say/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 MToken::Client - Client for interaction with MToken server
11  
12 =head1 VIRSION
13  
14 Version 1.02
15  
16 =head1 SYNOPSIS
17  
18 use MToken::Client;
19  
20 my $clinet = MToken::Client->new(
21 url => "https://localhost:8642",
22 username => "username", # optional
23 password => "password", # optional
24 pwcache => "/path/to/pwcache.tmp",
25 pwcache_ttl => 300, # 5 min. Default
26 max_redirects => 2, # Default: 10
27 connect_timeout => 3, # Default: 10 sec
28 inactivity_timeout => 5, # Default: 30 sec
29 request_timeout => 10, # Default: 5 min (300 sec)
30 );
31 my $status = $client->check();
32  
33 if ($status) {
34 print STDOUT $client->res->body;
35 } else {
36 print STDERR $clinet->error;
37 }
38  
39 =head1 DESCRIPTION
40  
41 Client for interaction with MToken server
42  
43 =head2 new
44  
45 my $clinet = MToken::Client->new(
46 url => "https://localhost:8642",
47 username => "username", # optional
48 password => "password", # optional
49 pwcache => "/path/to/pwcache.tmp",
50 pwcache_ttl => 300, # 5 min. Default
51 max_redirects => 2, # Default: 10
52 connect_timeout => 3, # Default: 10 sec
53 inactivity_timeout => 5, # Default: 30 sec
54 request_timeout => 10, # Default: 5 min (300 sec)
55 );
56  
57 Returns client
58  
59 =over 8
60  
61 =item B<max_redirects>
62  
63 Maximum number of redirects the user agent will follow before it fails. Default - 10
64  
65 =item B<password>
66  
67 Default password for basic authentication
68  
69 =item B<pwcache>
70  
71 Full path to file of password cache
72  
73 =item B<pwcache_ttl>
74  
75 Time to Live of pwcache file. Default - 300 sec
76  
77 =item B<*timeout>
78  
79 Timeout for connections, requests and inactivity periods in seconds.
80  
81 =item B<ua>
82  
83 The Mojo UserAgent object
84  
85 =item B<url>
86  
87 Full URL of the WEB Server
88  
89 =item B<username>
90  
91 Default username for basic authentication
92  
93 =back
94  
95 =head1 METHODS
96  
97 =head2 check
98  
99 my $status = $client->check;
100 my $status = $client->check(URL);
101  
102 Returns check-status of server. 0 - Error; 1 - Ok
103  
104 =head2 cleanup
105  
106 $client->cleanup;
107  
108 Cleanup all variable data in object and returns client object
109  
110 =head2 code
111  
112 my $code = $clinet->code;
113  
114 Returns HTTP code of the response
115  
116 =head2 credentials
117  
118 my $userinfo = $client->credentials($MOJO_URL_OBJECT, 1)
119  
120 Gets credentials for User Agent
121  
122 =head2 download
123  
124 my $status = $client->download(TOKEN_NAME => TARBALL_FILE_PATH);
125  
126 Request for download file from server by file path.
127 The method returns status of operation: 0 - Error; 1 - Ok
128  
129 =head2 error
130  
131 print $clinet->error;
132  
133 Returns error string
134  
135 =head2 info
136  
137 my $status = $clinet->info();
138 my $status = $clinet->info( TOKEN_NAME );
139  
140 Request for getting information about token storage or about list of stored token tarballs.
141  
142 =head2 remove
143  
144 my $status = $client->remove(TOKEN_NAME => TARBALL_FILE_NAME);
145  
146 Request for deleting of the file on server by filename.
147 The method returns status of operation: 0 - Error; 1 - Ok
148  
149 =head2 req
150  
151 my $request = $clinet->req;
152  
153 Returns Mojo::Message::Request object
154  
155 =head2 request
156  
157 my $json = $clinet->request("METHOD", "PATH", ...ATTRIBUTES...);
158  
159 Send request
160  
161 =head2 res
162  
163 my $response = $clinet->res;
164  
165 Returns Mojo::Message::Response object
166  
167 =head2 status
168  
169 my $status = $clinet->status;
170  
171 Returns object status value. 0 - Error; 1 - Ok
172  
173 =head2 trace
174  
175 my $trace = $client->trace;
176 print $client->trace("New trace record");
177  
178 Gets trace stack or pushes new trace record to trace stack
179  
180 =head2 tx
181  
182 my $status = $clinet->tx($tx);
183  
184 Works with Mojo::Transaction object, interface with it
185  
186 =head2 ua
187  
188 my $ua = $clinet->ua;
189  
190 Returns Mojo::UserAgent object
191  
192 =head2 upload
193  
194 my $status = $client->upload(TOKEN_NAME => TARBALL_FILE_PATH);
195  
196 Request for uploading of tarball on server.
197 The method returns status of operation: 0 - Error; 1 - Ok
198  
199 =head2 url
200  
201 my $url_object = $clinet->url;
202  
203 Returns Mojo::URL object
204  
205 =head1 HISTORY
206  
207 See C<Changes> file
208  
209 =head1 DEPENDENCIES
210  
211 L<Mojolicious>
212  
213 =head1 TO DO
214  
215 See C<TODO> file
216  
217 =head1 SEE ALSO
218  
219 L<CTK>, L<Mojo::UserAgent>
220  
221 =head1 AUTHOR
222  
223 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
224  
225 =head1 COPYRIGHT
226  
227 Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved
228  
229 =head1 LICENSE
230  
231 This program is free software; you can redistribute it and/or
232 modify it under the same terms as Perl itself.
233  
234 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
235  
236 =cut
237  
238 2 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 2 $VERSION = '1.02';
240  
241 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 2 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 2 ▶use Mojo::File qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 2 ▶use Mojo::Util qw/steady_time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
246  
247 2 ▶use CTK::Util qw/dtf lf_normalize touch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
248  
249 2 ▶use MToken::Util qw/md5sum parse_credentials tcd_save tcd_load/;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 2 ▶use MToken::Const;
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
253 2 MAX_REDIRECTS => 10,
254 CONNECT_TIMEOUT => 10,
255 INACTIVITY_TIMEOUT => 30,
256 REQUEST_TIMEOUT => 180,
257 TRANSACTION_MASK => "%s %s >>> %s %s [%s in %s%s]", # GET /auth >>> 200 OK [1.04 KB in 0.0242 seconds (43.1 KB/sec)]
258 CONTENT_TYPE => "application/json",
259 PWCACHE_TTL => 300, # 5 min (Time to Live)
260 2 };
261  
262  
263 0 ▶sub new {
Subroutine new: not called CC=20 SCAR=60.4
Pod documented
264 0 my $class = shift;
265 0 my %args = @_;
266  
267 # General
268 0 ▶ $args{url} ||= ""; # base url
Condition0%0/2
$args{"url"} ||= ""
l!l
00
MC/DC0%0/1
$args{"url"} ||= ""
$args{"url"}
Truth table0%0/2
A
A= $args{"url"}
Aresult
11
00
269 0 $args{prefix} = "";
270 0 ▶ if ($args{url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url"}) 0 0 0
271 0 $args{url} =~ s/\/+$//; $args{url} .= "/"; # Correct slash
272 0 $args{url} =~ s/mtoken\///; # Delete mtoken prefix
273 0 $args{url} = Mojo::URL->new($args{url});
274 0 $args{prefix} = $args{url}->path;
275 }
276 0 $args{status} = 1; # 0 - error, 1 - ok
277 0 $args{error} = ""; # string
278 0 $args{code} = 0; # integer
279 0 $args{trace} = []; # trace pool
280 0 $args{tx_time} = 0;
281 0 $args{req} = undef;
282 0 $args{res} = undef;
283 0 ▶ $args{user} ||= "";
Condition0%0/2
$args{"user"} ||= ""
l!l
00
MC/DC0%0/1
$args{"user"} ||= ""
$args{"user"}
Truth table0%0/2
A
A= $args{"user"}
Aresult
11
00
284 0 ▶ $args{password} ||= "";
Condition0%0/2
$args{"password"} ||= ""
l!l
00
MC/DC0%0/1
$args{"password"} ||= ""
$args{"password"}
Truth table0%0/2
A
A= $args{"password"}
Aresult
11
00
285 0 ▶ if ($args{url} && $args{user}) { # Set userinfo
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'url'} and $args{'user'}) { } 0 0 0
elsif ($args{'url'}) { } 0 0 0
Condition0%0/3
$args{"url"} and $args{"user"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"url"} and $args{"user"}
$args{"url"} $args{"user"}
Truth table0%0/3
A and B
A= $args{"url"}B= $args{"user"}
ABresult
0X0
100
111
286 0 $args{url}->userinfo(sprintf("%s:%s", $args{user}, $args{password}))
287 } elsif ($args{url}) {
288 0 ($args{user}, $args{password}) = (parse_credentials($args{url}->to_unsafe_string));
289 }
290 0 ▶ $args{pwcache} ||= ""; # pwcache file
Condition0%0/2
$args{"pwcache"} ||= ""
l!l
00
MC/DC0%0/1
$args{"pwcache"} ||= ""
$args{"pwcache"}
Truth table0%0/2
A
A= $args{"pwcache"}
Aresult
11
00
291 0 ▶ $args{pwcache_ttl} //= PWCACHE_TTL;
Condition0%0/2
$args{"pwcache_ttl"} //= 300
l!l
00
MC/DC0%0/1
$args{"pwcache_ttl"} //= 300
$args{"pwcache_ttl"}
Truth table0%0/2
A
A= $args{"pwcache_ttl"}
Aresult
11
00
292 0 ▶ if ($args{pwcache} && -e $args{pwcache}) { # Set pwcache
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"pwcache"} and -e $args{"pwcache"}) 0 0 0
Condition0%0/3
$args{"pwcache"} and -e $args{"pwcache"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"pwcache"} and -e $args{"pwcache"}
$args{"pwcache"} -e $args{"pwcache"}
Truth table0%0/3
A and B
A= $args{"pwcache"}B= -e $args{"pwcache"}
ABresult
0X0
100
111
293 0 my $pwcache_path = path($args{pwcache});
294 0 ▶ if ($args{pwcache_ttl} && ($pwcache_path->stat->mtime + $args{pwcache_ttl}) < time) { # expired
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'pwcache_ttl'} and $pwcache_path->stat->mtime + $args{'pwcache_ttl'} < time) { } 0 0 0
Condition0%0/3
$args{"pwcache_ttl"} and $pwcache_path->stat->mtime + $args{"pwcache_ttl"} < time
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"pwcache_ttl"} and $pwcache_path->stat->mtime + $args{"pwcache_ttl"} < time
$args{"pwcache_ttl"} $pwcache_path->stat->mtime + $args{"pwcache_ttl"} < time
Truth table0%0/3
A and B
A= $args{"pwcache_ttl"}B= $pwcache_path->stat->mtime + $args{"pwcache_ttl"} < time
ABresult
0X0
100
111
295 0 $pwcache_path->remove;
296 } else {
297 0 touch($args{pwcache});
298 }
299 }
300  
301 # User Agent
302 0 my $ua = $args{ua};
303 0 ▶ unless ($ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 0 0 0
304 # Create the instance
305 $ua = Mojo::UserAgent->new(
306 max_redirects => $args{max_redirects} || MAX_REDIRECTS,
307 connect_timeout => $args{connect_timeout} || CONNECT_TIMEOUT,
308 inactivity_timeout => $args{inactivity_timeout} || INACTIVITY_TIMEOUT,
309 request_timeout => $args{request_timeout} || REQUEST_TIMEOUT,
310 0 ▶ insecure => $args{insecure} || 0,
Condition0%0/10
$args{'max_redirects'} || 10
l!l
00
$args{'connect_timeout'} || 10
l!l
00
$args{'inactivity_timeout'} || 30
l!l
00
$args{'request_timeout'} || 180
l!l
00
$args{'insecure'} || 0
l!l
00
MC/DC0%0/1
$args{'max_redirects'} || 10
$args{'max_redirects'}
MC/DC0%0/1
$args{'connect_timeout'} || 10
$args{'connect_timeout'}
MC/DC0%0/1
$args{'inactivity_timeout'} || 30
$args{'inactivity_timeout'}
MC/DC0%0/1
$args{'request_timeout'} || 180
$args{'request_timeout'}
MC/DC0%0/1
$args{'insecure'} || 0
$args{'insecure'}
Truth table0%0/2
A
A= $args{'max_redirects'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'connect_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'inactivity_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'request_timeout'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'insecure'}
Aresult
11
00
311 );
312 0 $args{ua} = $ua;
313 }
314  
315 0 my $self = bless {%args}, $class;
316 0 return $self;
317 }
318 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
319 0 my $self = shift;
320 0 my $e = shift;
321 0 ▶ $self->{error} = $e if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
322 0 return $self->{error};
323 }
324 0 ▶sub status {
Subroutine status: not called CC=2 SCAR=17.9
Pod documented
325 0 my $self = shift;
326 0 my $s = shift;
327 0 ▶ $self->{status} = $s if defined $s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
328 0 return $self->{status};
329 }
330 0 ▶sub code {
Subroutine code: not called CC=2 SCAR=17.9
Pod documented
331 0 my $self = shift;
332 0 my $c = shift;
333 0 ▶ $self->{code} = $c if defined $c;
Branch: 1 branch
BranchTrueFalseTotal
if defined $c 0 0 0
334 0 return $self->{code};
335 }
336 0 ▶sub trace {
Subroutine trace: not called CC=3 SCAR=24.8
Pod documented
337 0 my $self = shift;
338 0 my $v = shift;
339 0 ▶ if (defined($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 0 0 0
340 0 my $a = $self->{trace};
341 0 push @$a, lf_normalize($v);
342 0 return lf_normalize($v);
343 }
344 0 ▶ my $trace = $self->{trace} || [];
Condition0%0/2
$self->{'trace'} || []
l!l
00
MC/DC0%0/1
$self->{'trace'} || []
$self->{'trace'}
Truth table0%0/2
A
A= $self->{'trace'}
Aresult
11
00
345 0 return join("\n",@$trace);
346 }
347 0 ▶sub cleanup {
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod documented
348 0 my $self = shift;
349 0 $self->{status} = 1;
350 0 $self->{error} = "";
351 0 $self->{code} = 0;
352 0 undef $self->{req};
353 0 $self->{req} = undef;
354 0 undef $self->{res};
355 0 $self->{res} = undef;
356 0 undef $self->{trace};
357 0 $self->{trace} = [];
358 0 return $self;
359 }
360 0 ▶sub req {
Subroutine req: not called CC=1 SCAR=6.9
Pod documented
361 0 my $self = shift;
362 0 return $self->{req};
363 }
364 0 ▶sub res {
Subroutine res: not called CC=1 SCAR=6.9
Pod documented
365 0 my $self = shift;
366 0 return $self->{res};
367 }
368 0 ▶sub url {
Subroutine url: not called CC=1 SCAR=6.9
Pod documented
369 0 my $self = shift;
370 0 return $self->{url};
371 }
372 0 ▶sub ua {
Subroutine ua: not called CC=1 SCAR=6.9
Pod documented
373 0 my $self = shift;
374 0 return $self->{ua};
375 }
376 0 ▶sub tx {
Subroutine tx: not called CC=16 SCAR=56.1
Pod documented
377 0 my $self = shift;
378 0 my $tx = shift;
379  
380 # Check Error
381 0 my $err = $tx->error;
382 0 ▶ unless (!$err || $err->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $err or $err->{"code"}) 0 0 0
Condition0%0/3
not $err or $err->{"code"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $err or $err->{"code"}
not $err $err->{"code"}
Truth table0%0/3
not(A) or B
A= not $errB= $err->{"code"}
ABresult
1X1
011
000
383 0 $self->error($err->{message});
384 0 $self->status(0);
385 }
386 0 ▶ $self->code($tx->res->code || "000");
Condition0%0/2
$tx->res->code || '000'
l!l
00
MC/DC0%0/1
$tx->res->code || '000'
$tx->res->code
Truth table0%0/2
A
A= $tx->res->code
Aresult
11
00
387 0 ▶ $self->status($tx->res->is_success ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$tx->res->is_success ? : 0 0 0
388 0 ▶ $self->error($tx->res->json("/message") || $err->{message} || "Unknown error" )
Branch: 1 branch
BranchTrueFalseTotal
if $tx->res->is_error and not $self->error 0 0 0
Condition0%0/8
$tx->res->is_error and not $self->error
!ll&&!rl&&r
000
$tx->res->json('/message') || $err->{'message'} || 'Unknown error'
l!l
00
$tx->res->json("/message") or $err->{"message"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx->res->is_error and not $self->error
$tx->res->is_error not $self->error
MC/DC0%0/2
$tx->res->json('/message') || $err->{'message'} || 'Unknown error'
$tx->res->json("/message") $err->{"message"}
Truth table0%0/3
A and not(B)
A= $tx->res->is_errorB= not $self->error
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $tx->res->json("/message")B= $err->{"message"}
ABresult
1X1
011
000
389 if $tx->res->is_error && !$self->error;
390  
391 # Tracing
392 0 ▶ my $length = $tx->res->body_size || 0;
Condition0%0/2
$tx->res->body_size || 0
l!l
00
MC/DC0%0/1
$tx->res->body_size || 0
$tx->res->body_size
Truth table0%0/2
A
A= $tx->res->body_size
Aresult
11
00
393 0 ▶ my $rtime = $self->{tx_time} // 0;
Condition0%0/2
$self->{"tx_time"} // 0
l!l
00
MC/DC0%0/1
$self->{"tx_time"} // 0
$self->{"tx_time"}
Truth table0%0/2
A
A= $self->{"tx_time"}
Aresult
11
00
394 $self->trace(sprintf(TRANSACTION_MASK,
395 $tx->req->method, $tx->req->url->to_abs, # Method & URL
396 0 ▶ $self->code, $tx->res->message || $err->{message} || "Unknown error", # Line
Branch: 1 branch
BranchTrueFalseTotal
$rtime ? : 0 0 0
Condition0%0/5
$tx->res->message || $err->{'message'} || 'Unknown error'
l!l
00
$tx->res->message or $err->{"message"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx->res->message || $err->{'message'} || 'Unknown error'
$tx->res->message $err->{"message"}
Truth table0%0/3
A or B
A= $tx->res->messageB= $err->{"message"}
ABresult
1X1
011
000
397 _fbytes($length), # Length
398 _fduration($rtime), # Duration
399 $rtime ? sprintf(" (%s/sec)", _fbytes($length/$rtime)) : "",
400 ));
401 0 my $req_hdrs = $tx->req->headers->to_string;
402 0 ▶ if ($req_hdrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_hdrs) 0 0 0
403 0 $self->trace(join("\n", map {$_ = "> $_"} split(/\n/, $req_hdrs)));
404 0 $self->trace(">");
405 }
406 0 my $res_hdrs = $tx->res->headers->to_string;
407 0 ▶ if ($res_hdrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_hdrs) 0 0 0
408 0 $self->trace(join("\n", map {$_ = "< $_"} split(/\n/, $res_hdrs)));
409 0 $self->trace("<");
410 }
411  
412 # Request And Response
413 0 $self->{req} = $tx->req;
414 0 $self->{res} = $tx->res;
415  
416 0 return $self->status;
417 }
418 0 ▶sub request {
Subroutine request: not called CC=12 SCAR=50.5
Pod documented
419 0 my $self = shift;
420 0 my $meth = shift;
421 0 my $_url = shift;
422 0 my @params = @_;
423 0 $self->cleanup(); # Cleanup first
424  
425 # Set URL + credentials
426 my $url = $_url
427 ? Mojo::URL->new("$_url")
428 : $self->{url}
429 0 ▶ ? $self->url->clone
Branch: 2 branches
BranchTrueFalseTotal
$_url ? : 0 0 0
$self->{'url'} ? : 0 0 0
430 : Mojo::URL->new(DEFAULT_URL);
431 0 ▶ $url->userinfo($self->credentials($url)) if $_url;
Branch: 1 branch
BranchTrueFalseTotal
if $_url 0 0 0
432  
433 # Request #1
434 0 my $start_time = steady_time() * 1;
435 0 my $tx = $self->ua->build_tx($meth, $url, @params); # Create transaction (tx) #1
436 0 my $status = $self->tx($self->ua->start($tx)); # Run it and validate!);
437 0 $self->{tx_time} = sprintf("%.*f",4, steady_time()*1 - $start_time) * 1;
438  
439 # Auth test
440 0 ▶ if (!$status && $self->code == 401) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $status and $self->code == 401) 0 0 0
Condition0%0/3
not $status and $self->code == 401
!ll&&!rl&&r
000
MC/DC0%0/2
not $status and $self->code == 401
not $status $self->code == 401
Truth table0%0/3
not(A) and B
A= not $statusB= $self->code == 401
ABresult
0X0
100
111
441 0 $self->cleanup();
442 # Request #2
443 0 $url->userinfo($self->credentials($url, 1));
444 0 $tx = $self->ua->build_tx($meth, $url, @params); # Create transaction (tx) #2
445 0 $status = $self->tx($self->ua->start($tx)); # Run it and validate!);
446 0 $self->{tx_time} = sprintf("%.*f",4, steady_time()*1 - $start_time) * 1;
447 0 ▶ if (!$status && $self->code == 401) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $status and $self->code == 401) { } 0 0 0
elsif ($status and $self->{'pwcache'}) { } 0 0 0
Condition0%0/3
not $status and $self->code == 401
!ll&&!rl&&r
000
MC/DC0%0/2
not $status and $self->code == 401
not $status $self->code == 401
Truth table0%0/3
not(A) and B
A= not $statusB= $self->code == 401
ABresult
0X0
100
111
448 0 $self->{user} = "";
449 0 $self->{password} = "";
450 0 ▶ path($self->{pwcache})->remove if $self->{pwcache} && -e $self->{pwcache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pwcache"} and -e $self->{"pwcache"} 0 0 0
Condition0%0/3
$self->{"pwcache"} and -e $self->{"pwcache"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pwcache"} and -e $self->{"pwcache"}
$self->{"pwcache"} -e $self->{"pwcache"}
Truth table0%0/3
A and B
A= $self->{"pwcache"}B= -e $self->{"pwcache"}
ABresult
0X0
100
111
451 ▶ } elsif ($status && $self->{pwcache}) {
Condition0%0/3
$status and $self->{"pwcache"}
!ll&&!rl&&r
000
MC/DC0%0/2
$status and $self->{"pwcache"}
$status $self->{"pwcache"}
Truth table0%0/3
A and B
A= $statusB= $self->{"pwcache"}
ABresult
0X0
100
111
452 0 tcd_save($self->{pwcache}, $url->userinfo);
453 }
454 }
455  
456 0 return $status;
457 }
458 0 ▶sub credentials {
Subroutine credentials: not called CC=12 SCAR=50.5
Pod documented
459 0 my $self = shift;
460 0 my $url = shift;
461 0 ▶ my $ask = shift(@_) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift @_ ? : 0 0 0
462 0 ▶ $url ||= $self->{url};
Condition0%0/3
$url ||= $self->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url ||= $self->{"url"}
$url $self->{"url"}
Truth table0%0/3
A ||= B
A= $urlB= $self->{"url"}
ABresult
1X1
011
000
463 0 my ($user, $password);
464  
465 # return predefined credentials
466 0 ▶ return sprintf("%s:%s", $self->{user}, $self->{password}) if $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"user"} 0 0 0
467  
468 # return if url contains credentials
469 0 ($user, $password) = (parse_credentials($url->to_unsafe_string));
470 0 ▶ return sprintf("%s:%s", $user, $password) if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
471  
472 # Get from cache
473 0 ▶ if ($self->{pwcache} && -e $self->{pwcache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pwcache"} and -e $self->{"pwcache"}) 0 0 0
Condition0%0/3
$self->{"pwcache"} and -e $self->{"pwcache"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"pwcache"} and -e $self->{"pwcache"}
$self->{"pwcache"} -e $self->{"pwcache"}
Truth table0%0/3
A and B
A= $self->{"pwcache"}B= -e $self->{"pwcache"}
ABresult
0X0
100
111
474 0 ▶ my $pair = tcd_load($self->{pwcache}) // "";
Condition0%0/2
tcd_load($self->{"pwcache"}) // ""
l!l
00
MC/DC0%0/1
tcd_load($self->{"pwcache"}) // ""
tcd_load($self->{"pwcache"})
Truth table0%0/2
A
A= tcd_load($self->{"pwcache"})
Aresult
11
00
475 0 ($user, $password) = split(/\:/, $pair);
476 0 ▶ return sprintf("%s:%s", $user, $password) if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
477 0 unlink($self->{pwcache});
478 }
479  
480 # prompt if ask flag is true and is terminal
481 0 ▶ if ($ask && -t STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ask and -t STDIN) 0 0 0
Condition0%0/3
$ask and -t STDIN
!ll&&!rl&&r
000
MC/DC0%0/2
$ask and -t STDIN
$ask -t STDIN
Truth table0%0/3
A and B
A= $askB= -t STDIN
ABresult
0X0
100
111
482 0 my $realm = 'server';
483 0 printf STDERR "Enter username for %s at %s: ", $realm, $url->host_port;
484 0 $user = <STDIN>;
485 0 chomp($user);
486 0 ▶ if (length($user)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $user) { } 0 0 0
487 0 print STDERR "Password: ";
488 0 system("stty -echo") unless IS_MSWIN;
489 0 $password = <STDIN>;
490 0 system("stty echo") unless IS_MSWIN;
491 0 print STDERR "\n"; # because we disabled echo
492 0 chomp($password);
493 0 $self->{user} = $user;
494 0 $self->{password} = $password;
495 } else {
496 0 return "";
497 }
498 0 return sprintf("%s:%s", $user, $password);
499 }
500 0 return "";
501 }
502 0 ▶sub check {
Subroutine check: not called CC=3 SCAR=24.8
Pod documented
503 0 my $self = shift;
504 0 my $url = shift;
505 0 ▶ if (!$url && $self->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $url and $self->{"url"}) 0 0 0
Condition0%0/3
not $url and $self->{"url"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $url and $self->{"url"}
not $url $self->{"url"}
Truth table0%0/3
not(A) and B
A= not $urlB= $self->{"url"}
ABresult
0X0
100
111
506 0 $url = $self->url->clone;
507 0 $url->path("mtoken/");
508 }
509 0 return $self->request(HEAD => $url);
510 }
511 0 ▶sub upload {
Subroutine upload: not called CC=1 SCAR=6.9
Pod documented
512 0 my $self = shift;
513 0 my $token = shift;
514 0 my $file = shift;
515 0 my $filepath = path($file);
516 0 my $filename = $filepath->basename;
517 0 my $url = $self->url->clone->path(sprintf("mtoken/%s/%s", $token, $filename));
518  
519 0 my $asset_file = Mojo::Asset::File->new(path => $file);
520 0 $self->request(PUT => $url =>
521 { # Headers
522 'User-Agent' => sprintf("%s/%s", __PACKAGE__, $self->VERSION),
523 'Content-Type' => 'multipart/form-data',
524 },
525 form => {
526 size => $asset_file->size,
527 md5 => md5sum($asset_file->path),
528 tarball => {
529 file => $asset_file,
530 filename => $filename,
531 'Content-Type' => 'application/octet-stream',
532 },
533 },
534 );
535 }
536 0 ▶sub info {
Subroutine info: not called CC=2 SCAR=17.9
Pod documented
537 0 my $self = shift;
538 0 my $token = shift;
539 0 ▶ my $url = $token
Branch: 1 branch
BranchTrueFalseTotal
$token ? : 0 0 0
540 ? $self->url->clone->path(sprintf("mtoken/%s", $token))
541 : $self->url->clone->path("mtoken");
542 0 return $self->request(GET => $url);
543 }
544 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
545 0 my $self = shift;
546 0 my $token = shift;
547 0 my $file = shift;
548 0 my $filepath = path($file);
549 0 my $filename = $filepath->basename;
550 0 my $url = $self->url->clone->path(sprintf("mtoken/%s/%s", $token, $filename));
551 0 return $self->request(DELETE => $url);
552 }
553 0 ▶sub download {
Subroutine download: not called CC=3 SCAR=24.8
Pod documented
554 0 my $self = shift;
555 0 my $token = shift;
556 0 my $file = shift;
557 0 my $filepath = path($file);
558 0 my $filename = $filepath->basename;
559 0 my $url = $self->url->clone->path(sprintf("mtoken/%s/%s", $token, $filename));
560 0 my $status = $self->request(GET => $url);
561 0 ▶ return $status unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 0 0 0
562 0 $self->res->save_to($file);
563 0 ▶ return 1 if $filepath->stat->size;
Branch: 1 branch
BranchTrueFalseTotal
if $filepath->stat->size 0 0 0
564 0 $self->error("Can't download file");
565 0 return $self->status(0);
566 }
567  
568 0 ▶sub _fduration {
Subroutine _fduration: not called CC=5 SCAR=34.0
569 0 ▶ my $msecs = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
570 0 my $secs = int($msecs);
571 0 my $hours = int($secs / (60*60));
572 0 $secs -= $hours * 60*60;
573 0 my $mins = int($secs / 60);
574 0 $secs %= 60;
575 0 ▶ if ($hours) {
Branch: 3 branches
BranchTrueFalseTotal
if ($hours) { } 0 0 0
elsif ($mins >= 2) { } 0 0 0
elsif ($secs < 120) { } 0 0 0
576 0 return sprintf("%d hours %d minutes", $hours, $mins);
577 } elsif ($mins >= 2) {
578 0 return sprintf("%d minutes", $mins);
579 } elsif ($secs < 2*60) {
580 0 return sprintf("%.4f seconds", $msecs);
581 } else {
582 0 $secs += $mins * 60;
583 0 return sprintf("%d seconds", $secs);
584 }
585 }
586 0 ▶sub _fbytes {
Subroutine _fbytes: not called CC=3 SCAR=24.8
587 0 my $n = int(shift);
588 0 ▶ if ($n >= 1024 * 1024) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n >= 1048576) { } 0 0 0
elsif ($n >= 1024) { } 0 0 0
589 0 return sprintf "%.3g MB", $n / (1024.0 * 1024);
590 } elsif ($n >= 1024) {
591 0 return sprintf "%.3g KB", $n / 1024.0;
592 } else {
593 0 return "$n bytes";
594 }
595 }
596  
597 2 1;
598  
599 __END__
« MToken.pm ↑ Summary MToken/Config.pm »