UserAgent/Paranoid.pm

filter: Statementstmt 88.5% 54 / 61 Branchbran 50.0% 11 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 1 totaltotal 78.3% 76 / 97 CC 12 SCAR 15.3
CC
12
Cov
78%
_connect
10.1
__ANON__
3.7
__ANON__
3.2
CRAP 4.6
« IOLoop/Client/Paranoid.pm ↑ Summary
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
BranchTrueFalseTotal
$handle ? : 0 4 4
54 4 my %options = (timeout => $self->connect_timeout);
55 0 ▶ if ($proto eq 'http+unix') { $options{path} = $host }
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq 'http+unix') { } 0 4 4
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
BranchTrueFalseTotal
if $handle 0 4 4
59 # SOCKS
60 4 ▶ if ($proto eq 'socks') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "socks") 0 4 4
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
BranchTrueFalseTotal
if $userinfo 0 0 0
65 }
66 # TLS
67 4 ▶ if ($options{tls} = $proto eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"tls"} = $proto eq "https") 1 3 4
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
BranchTrueFalseTotal
if $self->insecure 0 1 1
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
BranchTrueFalseTotal
unless $self 0 4 4
79 4 ▶ return $self->_error($id, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 1 4
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
BranchTrueFalseTotal
$self and $self->_finish($id, 1) 1 0 1
83 0 ▶ $stream->on(error => sub { $self && $self->_error($id, pop) });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->_error($id, pop()) 0 0 0
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
« IOLoop/Client/Paranoid.pm ↑ Summary