/usr/share/perl5/Mojolicious/Command/get.pm is in libmojolicious-perl 2.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | package Mojolicious::Command::get;
use Mojo::Base 'Mojo::Command';
use Getopt::Long 'GetOptions';
use Mojo::DOM;
use Mojo::IOLoop;
use Mojo::Transaction::HTTP;
use Mojo::UserAgent;
use Mojo::Util qw/decode encode/;
has description => <<'EOF';
Perform HTTP 1.1 request.
EOF
has usage => <<"EOF";
usage: $0 get [OPTIONS] URL [SELECTOR] [COMMANDS]
mojo get /
mojo get mojolicio.us
mojo get -v -r google.com
mojo get --method POST --content 'trololo' mojolicio.us
mojo get --header 'X-Bender: Bite my shiny metal ass!' mojolicio.us
mojo get mojolicio.us 'head > title' text
mojo get mojolicio.us .footer all
mojo get mojolicio.us a attr href
mojo get mojolicio.us '*' attr id
mojo get mojolicio.us 'h1, h2, h3' 3 text
These options are available:
--charset <charset> Charset of HTML5/XML content, defaults to auto
detection or "UTF-8".
--content <content> Content to send with request.
--header <name:value> Additional HTTP header.
--method <method> HTTP method to use, defaults to "GET".
--redirect Follow up to 5 redirects.
--verbose Print verbose debug information to STDERR.
EOF
# "Objection.
# In the absence of pants, defense's suspenders serve no purpose.
# I'm going to allow them... for now."
sub run {
my $self = shift;
# Options
local @ARGV = @_;
my $method = 'GET';
my @headers;
my $content = '';
my ($charset, $redirect, $verbose) = 0;
GetOptions(
'charset=s' => sub { $charset = $_[1] },
'content=s' => sub { $content = $_[1] },
'header=s' => \@headers,
'method=s' => sub { $method = $_[1] },
redirect => sub { $redirect = 1 },
verbose => sub { $verbose = 1 }
);
# Headers
my $headers = {};
for my $header (@headers) {
next unless $header =~ /^\s*([^\:]+)\s*:\s*([^\:]+)\s*$/;
$headers->{$1} = $2;
}
# URL and selector
my $url = shift @ARGV;
die $self->usage unless $url;
$url = decode 'UTF-8', $url;
my $selector = shift @ARGV;
# Fresh user agent
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
$ua->log->level('fatal');
$ua->max_redirects(5) if $redirect;
# Absolute URL
if ($url =~ m#^\w+://#) { $ua->detect_proxy }
# Application
else { $ua->app($ENV{MOJO_APP} || 'Mojo::HelloWorld') }
# Start
my $v;
my $buffer = '';
$ua->on(
start => sub {
my $tx = pop;
# Prepare request information
my $req = $tx->req;
my $startline = $req->build_start_line;
my $headers = $req->build_headers;
# Verbose callback
my $v = $verbose;
my $cb = sub {
my $res = shift;
# Wait for headers
return unless $v && $res->headers->is_finished;
# Request
warn "$startline$headers";
# Response
my $version = $res->version;
my $code = $res->code;
my $message = $res->message;
warn "HTTP/$version $code $message\n",
$res->headers->to_string, "\n\n";
# Finished
$v = 0;
};
# Progress
$tx->res->on(progress => sub { $cb->(shift) });
# Stream content
$tx->res->body(
sub {
$cb->(my $res = shift);
# Ignore intermediate content
return if $redirect && $res->is_status_class(300);
# Chunk
$selector ? ($buffer .= pop) : print(pop);
}
);
}
);
# Get
my $tx = $ua->build_tx($method, $url, $headers, $content);
STDOUT->autoflush(1);
$tx = $ua->start($tx);
# Error
my ($message, $code) = $tx->error;
$url = encode 'UTF-8', $url;
warn qq/Problem loading URL "$url". ($message)\n/ if $message && !$code;
# Charset
($tx->res->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/
and $charset = $1
unless defined $charset;
# Select
$self->_select($buffer, $charset, $selector) if $selector;
}
sub _say {
return unless length(my $value = shift);
say encode('UTF-8', $value);
}
sub _select {
my ($self, $buffer, $charset, $selector) = @_;
# Find
my $dom = Mojo::DOM->new->charset($charset)->parse($buffer);
my $results = $dom->find($selector);
# Commands
my $finished = 0;
while (defined(my $command = shift @ARGV)) {
# Number
if ($command =~ /^\d+$/) {
return unless ($results = [$results->[$command]])->[0];
next;
}
# Text
elsif ($command eq 'text') { _say($_->text) for @$results }
# All text
elsif ($command eq 'all') { _say($_->all_text) for @$results }
# Attribute
elsif ($command eq 'attr') {
next unless my $name = shift @ARGV;
_say($_->attrs->{$name}) for @$results;
}
# Unknown
else { die qq/Unknown command "$command".\n/ }
$finished++;
}
# Render
unless ($finished) { _say($_) for @$results }
}
1;
__END__
=head1 NAME
Mojolicious::Command::get - Get command
=head1 SYNOPSIS
use Mojolicious::Command::get;
my $get = Mojolicious::Command::get->new;
$get->run(@ARGV);
=head1 DESCRIPTION
L<Mojolicious::Command::get> is a command interface to L<Mojo::UserAgent>.
=head1 ATTRIBUTES
L<Mojolicious::Command::get> inherits all attributes from L<Mojo::Command>
and implements the following new ones.
=head2 C<description>
my $description = $get->description;
$get = $get->description('Foo!');
Short description of this command, used for the command list.
=head2 C<usage>
my $usage = $get->usage;
$get = $get->usage('Foo!');
Usage information for this command, used for the help screen.
=head1 METHODS
L<Mojolicious::Command::get> inherits all methods from L<Mojo::Command> and
implements the following new ones.
=head2 C<run>
$get->run(@ARGV);
Run this command.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|