| 1 | package Mojo::UserAgent::Paranoid 0.01; | ||||||||||
| 2 | 2 | ▶ | use 5.020; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use Mojo::Base 'Mojo::UserAgent'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Mojo::IOLoop::Client::Paranoid; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Mojo::UserAgent::Paranoid - paranoid user agent for fetching unknown URLs | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | my $io = Mojo::IOLoop->singleton; | ||||||||||
| 13 | my $ua = Mojo::UserAgent::Paranoid->new( | ||||||||||
| 14 | paranoid_dns => Net::DNS::Paranoid->new( | ||||||||||
| 15 | blocked_hosts => [qr{\.dev.example\.com$}] | ||||||||||
| 16 | ), | ||||||||||
| 17 | ioloop => $io, | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | has 'paranoid_dns' => sub { Net::DNS::Paranoid->new() }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | # Copied from Mojo::IOLoop::client() , and adapted to create a paranoid | ||||||||||
| 25 | # client instead | ||||||||||
| 26 | 4 | ▶ | sub paranoid_client { | ||||||||
|
Subroutine
paranoid_client: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 27 | 4 | my $self = shift; | |||||||||
| 28 | 4 | my $ioloop = shift; | |||||||||
| 29 | 4 | my $cb = pop; | |||||||||
| 30 | |||||||||||
| 31 | 4 | my $id = $ioloop->_id; | |||||||||
| 32 | 4 | my $client = $ioloop->{out}{$id}{client} = Mojo::IOLoop::Client::Paranoid->new( | |||||||||
| 33 | paranoid_dns => $self->paranoid_dns, | ||||||||||
| 34 | ); | ||||||||||
| 35 | 4 | weaken $ioloop; | |||||||||
| 36 | $client->on( | ||||||||||
| 37 | 1 | ▶ | connect => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | delete $ioloop->{out}{$id}{client}; | |||||||||
| 39 | 1 | my $stream = Mojo::IOLoop::Stream->new(pop); | |||||||||
| 40 | 1 | $ioloop->_stream($stream => $id); | |||||||||
| 41 | 1 | $ioloop->$cb(undef, $stream); | |||||||||
| 42 | } | ||||||||||
| 43 | 4 | ); | |||||||||
| 44 | 4 | ▶ | $client->on(error => sub { $ioloop->_remove($id); $ioloop->$cb(pop, undef) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 4 | $client->connect(@_); | |||||||||
| 46 | 4 | return $id; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | # Copied from Mojo::UserAgent, with the line ->client(...) changed to ->paranoid_client(...) | ||||||||||
| 50 | 4 | ▶ | sub _connect { | ||||||||
|
Subroutine
_connect: called CC=8 SCAR=23.2
| |||||||||||
| 51 | 4 | my ($self, $loop, $tx, $handle) = @_; | |||||||||
| 52 | 4 | my $t = $self->transactor; | |||||||||
| 53 | 4 | ▶ | my ($proto, $host, $port) = $handle ? $t->endpoint($tx) : $t->peer($tx); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 4 | my %options = (timeout => $self->connect_timeout); | |||||||||
| 55 | 0 | ▶ | if ($proto eq 'http+unix') { $options{path} = $host } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 4 | else { @options{qw(address port)} = ($host, $port) } | |||||||||
| 57 | 4 | $options{socket_options} = $self->socket_options; | |||||||||
| 58 | 4 | ▶ | $options{handle} = $handle if $handle; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | # SOCKS | ||||||||||
| 60 | 4 | ▶ | if ($proto eq 'socks') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | @options{qw(socks_address socks_port)} = @options{qw(address port)}; | |||||||||
| 62 | 0 | ($proto, @options{qw(address port)}) = $t->endpoint($tx); | |||||||||
| 63 | 0 | my $userinfo = $tx->req->via_proxy(0)->proxy->userinfo; | |||||||||
| 64 | 0 | ▶ | @options{qw(socks_user socks_pass)} = split /:/, $userinfo if $userinfo; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | } | ||||||||||
| 66 | # TLS | ||||||||||
| 67 | 4 | ▶ | if ($options{tls} = $proto eq 'https') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 1 | map { $options{"tls_$_"} = $self->$_ } qw(ca cert key); | |||||||||
| 69 | 1 | $options{tls_options} = $self->tls_options; | |||||||||
| 70 | 1 | ▶ | $options{tls_options}{SSL_verify_mode} = 0x00 if $self->insecure; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | } | ||||||||||
| 72 | 4 | weaken $self; | |||||||||
| 73 | 4 | my $id; | |||||||||
| 74 | return $id = $self->paranoid_client($loop, | ||||||||||
| 75 | 4 | ▶ | %options => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
| |||||||||||
| 76 | 4 | my ($loop, $err, $stream) = @_; | |||||||||
| 77 | # Connection error | ||||||||||
| 78 | 4 | ▶ | return unless $self; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 4 | ▶ | return $self->_error($id, $err) if $err; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | # Connection established | ||||||||||
| 81 | 0 | ▶ | $stream->on(timeout => sub { $self->_error($id, 'Inactivity timeout') }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 82 | 1 | ▶ | $stream->on(close => sub { $self && $self->_finish($id, 1) }); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||
| 83 | 0 | ▶ | $stream->on(error => sub { $self && $self->_error($id, pop) }); | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||
| 84 | 1 | ▶ | $stream->on(read => sub { $self->_read($id, pop) }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 1 | $self->_process($id); | |||||||||
| 86 | } | ||||||||||
| 87 | 4 | ); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 2 | 1; | |||||||||
| 91 | |||||||||||
| 92 | =head1 SEE ALSO | ||||||||||
| 93 | |||||||||||
| 94 | L<Mojo::UserAgent> | ||||||||||
| 95 | |||||||||||
| 96 | L<Net::DNS::Paranoid> | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||