Mojolicious/Command/get.pm

filter: Statementstmt 88.8% 80 / 90 Branchbran 57.4% 31 / 54 Conditioncond 40.7% 11 / 27 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 76.0% 19 / 25 Podpod 100% 1 / 1 totaltotal 67.1% 143 / 213 CC 38 SCAR 24.7
CC
38
Cov
71%
run
24.4
_select
22.5
_json
6.8
CRAP 11.9
« Mojolicious/Validator.pm ↑ Summary Mojolicious/Static.pm »
1 package Mojolicious::Command::get;
2 1 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Mojo::Collection qw(c);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::JSON qw(to_json j);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mojo::Util qw(decode encode getopt);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has description => 'Perform HTTP request';
15 1 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 4 ▶sub run {
Subroutine run: called CC=17 SCAR=31.9
Pod documented
18 4 my ($self, @args) = @_;
19  
20 # Data from STDIN
21 4 vec(my $r = '', fileno(STDIN), 1) = 1;
22 4 ▶ my $in = !-t STDIN && select($r, undef, undef, 0) ? join '', <STDIN> : undef;
Branch: 1 branch
BranchTrueFalseTotal
!-t(STDIN) && select($r, undef, undef, 0) ? : 4 0 4
Condition33%1/3
not -t STDIN and select $r, undef, undef, 0
!ll&&!rl&&r
004
MC/DC0%0/2
not -t STDIN and select $r, undef, undef, 0
not -t STDIN select $r, undef, undef, 0
Truth table33%1/3
not(A) and B
A= not -t STDINB= select $r, undef, undef, 0
ABresult
0X0
100
111
23  
24 4 my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
25 4 my %form;
26 die $self->usage
27 unless getopt \@args,
28 'C|charset=s' => \my $charset,
29 'c|content=s' => \$in,
30 1 ▶ 'f|form=s' => sub { _form(\%form) if $_[1] =~ /^(.+)=(\@?)(.+)$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /^(.+)=(\@?)(.+)$/u 1 0 1
31 'H|header=s' => \my @headers,
32 0 ▶ 'i|inactivity-timeout=i' => sub { $ua->inactivity_timeout($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 ▶ 'k|insecure' => sub { $ua->insecure(1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 'M|method=s' => \(my $method = 'GET'),
35 0 ▶ 'o|connect-timeout=i' => sub { $ua->connect_timeout($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 'r|redirect' => \my $redirect,
37 0 ▶ 'S|response-size=i' => sub { $ua->max_response_size($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 4 ▶ 'u|user=s' => \my $user,
Branch: 1 branch
BranchTrueFalseTotal
unless getopt(\@args, "C|charset=s", \my $charset, "c|content=s", \$in, "f|form=s", sub { _form(\%form) if $_[1] =~ /^(.+)=(\@?)(.+)$/u; } , "H|header=s", \my @headers, "i|inactivity-timeout=i", sub { $ua->inactivity_timeout($_[1]); } , "k|insecure", sub { $ua->insecure(1); } , "M|method=s", \(my $method = "GET"), "o|connect-timeout=i", sub { $ua->connect_timeout($_[1]); } , "r|redirect", \my $redirect, "S|response-size=i", sub { $ua->max_response_size($_[1]); } , "u|user=s", \my $user, "v|verbose", \my $verbose) 1 3 4
39 'v|verbose' => \my $verbose;
40  
41 3 @args = map { decode 'UTF-8', $_ } @args;
42 3 ▶ die $self->usage unless my $url = shift @args;
Branch: 1 branch
BranchTrueFalseTotal
unless my $url = shift @args 0 3 3
43 3 my $selector = shift @args;
44  
45 # Parse header pairs
46 3 ▶ my %headers = map { /^\s*([^:]+)\s*:\s*(.*+)$/ ? ($1, $2) : () } @headers;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*([^:]+)\s*:\s*(.*+)$/u ? : 1 0 1
47  
48 # Detect proxy for absolute URLs
49 3 ▶ $url !~ m!^/! ? $ua->proxy->detect : $ua->server->app($self->app);
Branch: 1 branch
BranchTrueFalseTotal
!($url =~ m[^/]u) ? : 0 3 3
50 3 ▶ $url = Mojo::URL->new($url)->userinfo($user) if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 3 3
51 3 ▶ $ua->max_redirects(10) if $redirect;
Branch: 1 branch
BranchTrueFalseTotal
if $redirect 0 3 3
52  
53 3 my $buffer = '';
54 $ua->on(
55 3 ▶ start => sub {
Subroutine __ANON__: called CC=2 SCAR=7.5
56 3 my ($ua, $tx) = @_;
57  
58 # Verbose
59 3 weaken $tx;
60 0 ▶ $tx->res->content->on(body => sub { warn _header($tx->req), _header($tx->res) }) if $verbose;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 3 3
61  
62 # Stream content (ignore redirects)
63 $tx->res->content->unsubscribe('read')->on(
64 3 ▶ read => sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
65 3 ▶ return if $redirect && $tx->res->is_redirect;
Branch: 1 branch
BranchTrueFalseTotal
if $redirect and $tx->res->is_redirect 0 3 3
Condition33%1/3
$redirect and $tx->res->is_redirect
!ll&&!rl&&r
300
MC/DC0%0/2
$redirect and $tx->res->is_redirect
$redirect $tx->res->is_redirect
Truth table33%1/3
A and B
A= $redirectB= $tx->res->is_redirect
ABresult
0X0
100
111
66 3 ▶ defined $selector ? ($buffer .= pop) : print pop;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 2 1 3
67 }
68 3 );
69 }
70 3 );
71  
72 # Switch to verbose for HEAD requests
73 3 ▶ $verbose = 1 if $method eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "HEAD" 0 3 3
74 3 STDOUT->autoflush(1);
75 3 ▶ my @content = %form ? (form => \%form) : defined $in ? ($in) : ();
Branch: 2 branches
BranchTrueFalseTotal
%form ? : 1 2 3
defined $in ? : 2 0 2
76 3 my $tx = $ua->start($ua->build_tx($method, $url, \%headers, @content));
77 3 my $res = $tx->result;
78  
79 # JSON Pointer
80 3 ▶ return undef unless defined $selector;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $selector 1 2 3
81 2 ▶ return _json($buffer, $selector) if !length $selector || $selector =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if not length $selector or $selector =~ m[^/]u 1 1 2
Condition66%2/3
not length $selector or $selector =~ m[^/]u
l!l&&r!l&&!r
011
MC/DC50%1/2
not length $selector or $selector =~ m[^/]u
not length $selector $selector =~ m[^/]u
Truth table66%2/3
not(A) or B
A= not length $selectorB= $selector =~ m[^/]u
ABresult
1X1
011
000
82  
83 # Selector
84 1 ▶ $charset //= $res->content->charset || $res->default_charset;
Condition33%2/6
$charset //= $res->content->charset || $res->default_charset
l!l&&r!l&&!r
010
$res->content->charset || $res->default_charset
l!l&&r!l&&!r
100
MC/DC0%0/3
$charset //= $res->content->charset || $res->default_charset
$charset $res->content->charset $res->default_charset
Truth table0%0/4
A //= B || C
A= $charsetB= $res->content->charsetC= $res->default_charset
ABCresult
1XX1
01X1
0011
0000
85 1 _select($buffer, $selector, $charset, @args);
86 }
87  
88 1 ▶sub _form { push @{$_[0]{$1}}, $2 ? {file => $3} : $3 }
Subroutine _form: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 0 1 1
89  
90 0 ▶sub _header { $_[0]->build_start_line, $_[0]->headers->to_string, "\n\n" }
Subroutine _header: not called CC=1 SCAR=6.9
91  
92 1 ▶sub _json {
Subroutine _json: called CC=5 SCAR=19.2
93 1 ▶ return unless my $data = j(shift);
Branch: 1 branch
BranchTrueFalseTotal
unless my $data = j(shift()) 0 1 1
94 1 ▶ return unless defined($data = Mojo::JSON::Pointer->new($data)->get(shift));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($data = "Mojo::JSON::Pointer"->new($data)->get(shift())) 0 1 1
95 1 ▶ _say(ref $data eq 'HASH' || ref $data eq 'ARRAY' ? to_json($data) : $data);
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' || ref $data eq 'ARRAY' ? : 0 1 1
Condition33%1/3
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $data eq "HASH" or ref $data eq "ARRAY"
ref $data eq "HASH" ref $data eq "ARRAY"
Truth table33%1/3
A or B
A= ref $data eq "HASH"B= ref $data eq "ARRAY"
ABresult
1X1
011
000
96 }
97  
98 2 ▶sub _say { length && say encode('UTF-8', $_) for @_ }
Subroutine _say: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
length $_ and say encode("UTF-8", $_) 2 0 2
99  
100 1 ▶sub _select {
Subroutine _select: called CC=10 SCAR=31.1
101 1 my ($buffer, $selector, $charset, @args) = @_;
102  
103 # Keep a strong reference to the root
104 1 ▶ $buffer = decode($charset, $buffer) // $buffer if $charset;
Branch: 1 branch
BranchTrueFalseTotal
if $charset 1 0 1
Condition33%1/3
decode($charset, $buffer) // $buffer
l!l&&r!l&&!r
100
MC/DC0%0/2
decode($charset, $buffer) // $buffer
decode($charset, $buffer) $buffer
Truth table33%1/3
A // B
A= decode($charset, $buffer)B= $buffer
ABresult
1X1
011
000
105 1 my $dom = Mojo::DOM->new($buffer);
106 1 my $results = $dom->find($selector);
107  
108 1 ▶ while (defined(my $command = shift @args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $command = shift @args)) 2 0 2
109  
110 # Number
111 2 ▶ ($results = c($results->[$command])) and next if $command =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $command =~ /^\d+$/u 1 1 2
Condition75%3/4
$command =~ /^\d+$/u && (($results = c($results->[$command])) && (next))
!ll
11
$results = c($results->[$command]) and next
!ll
01
MC/DC0%0/2
$command =~ /^\d+$/u && (($results = c($results->[$command])) && (next))
$command =~ /^\d+$/u $results = c($results->[$command])
Truth table33%1/3
A && B
A= $command =~ /^\d+$/uB= $results = c($results->[$command])
ABresult
0X0
100
111
112  
113 # Text
114 1 ▶ return _say($results->map('text')->each) if $command eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "text" 1 0 1
115  
116 # All text
117 0 ▶ return _say($results->map('all_text')->each) if $command eq 'all';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "all" 0 0 0
118  
119 # Attribute
120 0 ▶ return _say($results->map(attr => $args[0] // '')->each) if $command eq 'attr';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "attr" 0 0 0
Condition0%0/2
$args[0] // ""
l!l
00
MC/DC0%0/1
$args[0] // ""
$args[0]
Truth table0%0/2
A
A= $args[0]
Aresult
11
00
121  
122 # Unknown
123 0 die qq{Unknown command "$command".\n};
124 }
125  
126 0 _say($results->each);
127 }
128  
129 1 1;
130  
131 =encoding utf8
132  
133 =head1 NAME
134  
135 Mojolicious::Command::get - Get command
136  
137 =head1 SYNOPSIS
138  
139 Usage: APPLICATION get [OPTIONS] URL [SELECTOR|JSON-POINTER] [COMMANDS]
140  
141 ./myapp.pl get /
142 ./myapp.pl get -H 'Accept: text/html' /hello.html 'head > title' text
143 ./myapp.pl get //sri:secr3t@/secrets.json /1/content
144 mojo get mojolicious.org
145 mojo get -v -r -o 25 -i 50 google.com
146 mojo get -v -H 'Host: mojolicious.org' -H 'Accept: */*' mojolicious.org
147 mojo get -u 'sri:s3cret' https://mojolicious.org
148 mojo get mojolicious.org > example.html
149 mojo get -M PUT mojolicious.org < example.html
150 mojo get -f 'q=Mojolicious' -f 'size=5' https://metacpan.org/search
151 mojo get -M POST -f 'upload=@example.html' mojolicious.org
152 mojo get mojolicious.org 'head > title' text
153 mojo get mojolicious.org .footer all
154 mojo get mojolicious.org a attr href
155 mojo get mojolicious.org '*' attr id
156 mojo get mojolicious.org 'h1, h2, h3' 3 text
157 mojo get https://fastapi.metacpan.org/v1/author/SRI /name
158 mojo get -H 'Host: example.com' http+unix://%2Ftmp%2Fmyapp.sock/index.html
159  
160 Options:
161 -C, --charset <charset> Charset of HTML/XML content, defaults
162 to auto-detection
163 -c, --content <content> Content to send with request
164 -f, --form <name=value> One or more form values and file
165 uploads
166 -H, --header <name:value> One or more additional HTTP headers
167 -h, --help Show this summary of available options
168 --home <path> Path to home directory of your
169 application, defaults to the value of
170 MOJO_HOME or auto-detection
171 -i, --inactivity-timeout <seconds> Inactivity timeout, defaults to the
172 value of MOJO_INACTIVITY_TIMEOUT or 40
173 -k, --insecure Do not require a valid TLS certificate
174 to access HTTPS sites
175 -M, --method <method> HTTP method to use, defaults to "GET"
176 -m, --mode <name> Operating mode for your application,
177 defaults to the value of
178 MOJO_MODE/PLACK_ENV or "development"
179 -o, --connect-timeout <seconds> Connect timeout, defaults to the value
180 of MOJO_CONNECT_TIMEOUT or 10
181 -r, --redirect Follow up to 10 redirects
182 -S, --response-size <size> Maximum response size in bytes,
183 defaults to 2147483648 (2GiB)
184 -u, --user <userinfo> Alternate mechanism for specifying
185 colon-separated username and password
186 -v, --verbose Print request and response headers to
187 STDERR
188  
189 =head1 DESCRIPTION
190  
191 L<Mojolicious::Command::get> performs requests to remote hosts or local applications.
192  
193 This is a core command, that means it is always enabled and its code a good example for learning to build new commands,
194 you're welcome to fork it.
195  
196 See L<Mojolicious::Commands/"COMMANDS"> for a list of commands that are available by default.
197  
198 =head1 ATTRIBUTES
199  
200 L<Mojolicious::Command::get> inherits all attributes from L<Mojolicious::Command> and implements the following new
201 ones.
202  
203 =head2 description
204  
205 my $description = $get->description;
206 $get = $get->description('Foo');
207  
208 Short description of this command, used for the command list.
209  
210 =head2 usage
211  
212 my $usage = $get->usage;
213 $get = $get->usage('Foo');
214  
215 Usage information for this command, used for the help screen.
216  
217 =head1 METHODS
218  
219 L<Mojolicious::Command::get> inherits all methods from L<Mojolicious::Command> and implements the following new ones.
220  
221 =head2 run
222  
223 $get->run(@ARGV);
224  
225 Run this command.
226  
227 =head1 SEE ALSO
228  
229 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
230  
231 =cut
« Mojolicious/Validator.pm ↑ Summary Mojolicious/Static.pm »