ParanoidAgent.pm

filter: Statementstmt 73.2% 175 / 239 Branchbran 49.2% 68 / 138 Conditioncond 48.4% 47 / 97 MC/DCmcdc 16.3% 8 / 49 Subroutinesub 85.0% 17 / 20 Podpod 71.4% 5 / 7 totaltotal 58.1% 320 / 550 CC 109 SCAR 37.1
CC
109
Cov
61%
_bad_host
63.2
send_request
62.7
_resolve
58.9
CRAP 40.9
« Protocol/http_paranoid.pm ↑ Summary Protocol/https_paranoid.pm »
1 package LWPx::ParanoidAgent;
2 2 require LWP::UserAgent;
3  
4 2 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 @ISA = qw(LWP::UserAgent);
6 2 $VERSION = '1.12';
7  
8 2 require HTTP::Request;
9 2 require HTTP::Response;
10  
11 2 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use LWP::Debug ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
17 1 my $class = shift;
18 1 my %opts = @_;
19  
20 1 ▶ my $blocked_hosts = delete $opts{blocked_hosts} || [];
Condition50%1/2
delete $opts{'blocked_hosts'} || []
l!l
01
MC/DC0%0/1
delete $opts{'blocked_hosts'} || []
delete $opts{'blocked_hosts'}
Truth table50%1/2
A
A= delete $opts{'blocked_hosts'}
Aresult
11
00
21 1 ▶ my $whitelisted_hosts = delete $opts{whitelisted_hosts} || [];
Condition50%1/2
delete $opts{'whitelisted_hosts'} || []
l!l
01
MC/DC0%0/1
delete $opts{'whitelisted_hosts'} || []
delete $opts{'whitelisted_hosts'}
Truth table50%1/2
A
A= delete $opts{'whitelisted_hosts'}
Aresult
11
00
22 1 my $resolver = delete $opts{resolver};
23 1 my $paranoid_proxy = delete $opts{paranoid_proxy};
24 1 ▶ $opts{timeout} ||= 15;
Condition50%1/2
$opts{"timeout"} ||= 15
l!l
01
MC/DC0%0/1
$opts{"timeout"} ||= 15
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
25
26 1 my $self = LWP::UserAgent->new( %opts );
27  
28 1 $self->{'blocked_hosts'} = $blocked_hosts;
29 1 $self->{'whitelisted_hosts'} = $whitelisted_hosts;
30 1 $self->{'resolver'} = $resolver;
31 1 $self->{'paranoid_proxy'} = $paranoid_proxy;
32  
33 1 $self = bless $self, $class;
34 1 return $self;
35 }
36  
37 # returns seconds remaining given a request
38 0 ▶sub _time_remain {
Subroutine _time_remain: not called CC=2 SCAR=17.9
39 0 my $self = shift;
40 0 my $req = shift;
41  
42 0 my $now = time();
43 0 ▶ my $start_time = $req->{_time_begin} || $now;
Condition0%0/3
$req->{'_time_begin'} || $now
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->{'_time_begin'} || $now
$req->{'_time_begin'} $now
Truth table0%0/3
A || B
A= $req->{'_time_begin'}B= $now
ABresult
1X1
011
000
44 0 return $start_time + $self->{timeout} - $now;
45 }
46  
47 1 ▶sub _resolve {
Subroutine _resolve: called CC=17 SCAR=40.8
48 1 my ($self, $host, $request, $timeout, $depth) = @_;
49 1 my $res = $self->resolver;
50 1 ▶ $depth ||= 0;
Condition50%1/2
$depth ||= 0
l!l
01
MC/DC0%0/1
$depth ||= 0
$depth
Truth table50%1/2
A
A= $depth
Aresult
11
00
51  
52 1 ▶ die "CNAME recursion depth limit exceeded.\n" if $depth > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 10 0 1 1
53 1 ▶ die "DNS lookup resulted in bad host." if $self->_bad_host($host);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_bad_host($host) 0 1 1
54  
55 # return the IP address if it looks like one and wasn't marked bad
56 1 ▶ return ($host) if $host =~ /^\d+\.\d+\.\d+\.\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /^\d+\.\d+\.\d+\.\d+$/ 0 1 1
57  
58 1 ▶ my $dns_ref = $res->bgsend($host)
Branch: 1 branch
BranchTrueFalseTotal
my $dns_ref = $res->bgsend($host) or die "No sock from bgsend" 1 0 1
59 or die "No sock from bgsend";
60 1 my $sock;
61  
62 # Net::DNS 1.03 started returning IO::Select objects instead of sockets
63 1 ▶ if (UNIVERSAL::isa($dns_ref, "IO::Select")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($dns_ref, 'IO::Select')) { } 0 1 1
64 0 my $handles = [ $dns_ref->handles ];
65 0 $sock = $handles->[0]->[0];
66 }
67 else {
68 1 $sock = $dns_ref;
69 }
70  
71 # wait for the socket to become readable, unless this is from our test
72 # mock resolver.
73 1 ▶ unless ($dns_ref && $dns_ref eq "MOCK") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dns_ref and $dns_ref eq "MOCK") 0 1 1
Condition33%1/3
$dns_ref and $dns_ref eq "MOCK"
!ll&&!rl&&r
001
MC/DC0%0/2
$dns_ref and $dns_ref eq "MOCK"
$dns_ref $dns_ref eq "MOCK"
Truth table33%1/3
A and B
A= $dns_refB= $dns_ref eq "MOCK"
ABresult
0X0
100
111
74 0 my $rin = '';
75 0 vec($rin, fileno($sock), 1) = 1;
76 0 my $nf = select($rin, undef, undef, $self->_time_remain($request));
77 0 ▶ die "DNS lookup timeout" unless $nf;
Branch: 1 branch
BranchTrueFalseTotal
unless $nf 0 0 0
78 }
79  
80 1 ▶ my $packet = $res->bgread($dns_ref)
Branch: 1 branch
BranchTrueFalseTotal
my $packet = $res->bgread($dns_ref) or die "DNS bgread failure" 1 0 1
81 or die "DNS bgread failure";
82 1 $dns_ref = $sock = undef;
83  
84 1 my @addr;
85 my $cname;
86 1 foreach my $rr ($packet->answer) {
87 1 ▶ if ($rr->type eq "A") {
Branch: 2 branches
BranchTrueFalseTotal
if ($rr->type eq 'A') { } 1 0 1
elsif ($rr->type eq 'CNAME') { } 0 0 0
88 1 ▶ die "Suspicious DNS results from A record\n" if $self->_bad_host($rr->address);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_bad_host($rr->address) 1 0 1
89 # untaints the address:
90 0 push @addr, join(".", ($rr->address =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/));
91 } elsif ($rr->type eq "CNAME") {
92 # will be checked for validity in the recursion path
93 0 $cname = $rr->cname;
94 }
95 }
96  
97 0 ▶ return @addr if @addr;
Branch: 1 branch
BranchTrueFalseTotal
if @addr 0 0 0
98 0 ▶ return () unless $cname;
Branch: 1 branch
BranchTrueFalseTotal
unless $cname 0 0 0
99 0 return $self->_resolve($cname, $request, $timeout, $depth + 1);
100 }
101  
102 23 ▶sub _host_list_match {
Subroutine _host_list_match: called CC=8 SCAR=22.5
103 23 my $self = shift;
104 23 my $list_name = shift;
105 23 my $host = shift;
106  
107 23 ▶ foreach my $rule (@{ $self->{$list_name} || [] }) {
Condition50%1/2
$self->{$list_name} || []
l!l
230
MC/DC0%0/1
$self->{$list_name} || []
$self->{$list_name}
Truth table50%1/2
A
A= $self->{$list_name}
Aresult
11
00
108 35 ▶ if (ref $rule eq "CODE") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $rule eq 'CODE') { } 0 35 35
elsif (ref $rule) { } 12 23 35
109 0 ▶ return 1 if $rule->($host);
Branch: 1 branch
BranchTrueFalseTotal
if &$rule($host) 0 0 0
110 } elsif (ref $rule) {
111 # assume regexp
112 12 ▶ return 1 if $host =~ /$rule/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /$rule/ 0 12 12
113 } else {
114 23 ▶ return 1 if $host eq $rule;
Branch: 1 branch
BranchTrueFalseTotal
if $host eq $rule 1 22 23
115 }
116 }
117 }
118  
119 11 ▶sub _bad_host {
Subroutine _bad_host: called CC=36 SCAR=41.5
120 11 my $self = shift;
121 11 my $host = lc(shift);
122  
123 11 ▶ return 0 if $self->_host_list_match("whitelisted_hosts", $host);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_host_list_match("whitelisted_hosts", $host) 0 11 11
124 11 ▶ return 1 if $self->_host_list_match("blocked_hosts", $host);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_host_list_match("blocked_hosts", $host) 0 11 11
125 11 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /^localhost$/i or $host =~ /\s/i 0 11 11
Condition33%1/3
$host =~ /^localhost$/i or $host =~ /\s/i
l!l&&r!l&&!r
0011
MC/DC0%0/2
$host =~ /^localhost$/i or $host =~ /\s/i
$host =~ /^localhost$/i $host =~ /\s/i
Truth table33%1/3
A or B
A= $host =~ /^localhost$/iB= $host =~ /\s/i
ABresult
1X1
011
000
126 $host =~ /^localhost$/i || # localhost is bad. even though it'd be stopped in
127 # a later call to _bad_host with the IP address
128 $host =~ /\s/i; # any whitespace is questionable
129  
130 # Let's assume it's an IP address now, and get it into 32 bits.
131 # Uf at any time something doesn't look like a number, then it's
132 # probably a hostname and we've already either whitelisted or
133 # blacklisted those, so we'll just say it's okay and it'll come
134 # back here later when the resolver finds an IP address.
135 11 my @parts = split(/\./, $host);
136 11 ▶ return 0 if @parts > 4;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 4 0 11 11
137  
138 # un-octal/un-hex the parts, or return if there's a non-numeric part
139 11 my $overflow_flag = 0;
140 11 foreach (@parts) {
141 26 ▶ return 0 unless /^\d+$/ || /^0x[a-f\d]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d+$/ or /^0x[a-f\d]+$/ 2 24 26
Condition100%3/3
/^\d+$/ or /^0x[a-f\d]+$/
l!l&&r!l&&!r
1862
MC/DC100%2/2
/^\d+$/ or /^0x[a-f\d]+$/
/^\d+$/ /^0x[a-f\d]+$/
Truth table100%3/3
A or B
A= /^\d+$/B= /^0x[a-f\d]+$/
ABresult
1X1
011
000
142 0 ▶ local $SIG{__WARN__} = sub { $overflow_flag = 1; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
143 24 ▶ $_ = oct($_) if /^0/;
Branch: 1 branch
BranchTrueFalseTotal
if /^0/ 13 11 24
144 }
145  
146 # a purely numeric address shouldn't overflow.
147 9 ▶ return 1 if $overflow_flag;
Branch: 1 branch
BranchTrueFalseTotal
if $overflow_flag 0 9 9
148  
149 9 my $addr; # network order packed IP address
150  
151 9 ▶ if (@parts == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (@parts == 1) { } 1 8 9
elsif (@parts == 2) { } 4 4 8
elsif (@parts == 3) { } 1 3 4
elsif (@parts == 4) { } 3 0 3
152 # a - 32 bits
153 1 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] > 4294967295 0 1 1
154 $parts[0] > 0xffffffff;
155 1 $addr = pack("N", $parts[0]);
156 } elsif (@parts == 2) {
157 # a.b - 8.24 bits
158 4 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] > 255 or $parts[1] > 16777215 0 4 4
Condition33%1/3
$parts[0] > 255 or $parts[1] > 16777215
l!l&&r!l&&!r
004
MC/DC0%0/2
$parts[0] > 255 or $parts[1] > 16777215
$parts[0] > 255 $parts[1] > 16777215
Truth table33%1/3
A or B
A= $parts[0] > 255B= $parts[1] > 16777215
ABresult
1X1
011
000
159 $parts[0] > 0xff ||
160 $parts[1] > 0xffffff;
161 4 $addr = pack("N", $parts[0] << 24 | $parts[1]);
162 } elsif (@parts == 3) {
163 # a.b.c - 8.8.16 bits
164 1 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] > 255 or $parts[1] > 255 or $parts[2] > 65535 0 1 1
Condition33%2/6
$parts[0] > 255 or $parts[1] > 255 or $parts[2] > 65535
l!l&&r!l&&!r
001
$parts[0] > 255 or $parts[1] > 255
l!l&&r!l&&!r
001
MC/DC0%0/3
$parts[0] > 255 or $parts[1] > 255 or $parts[2] > 65535
$parts[0] > 255 $parts[1] > 255 $parts[2] > 65535
Truth table25%1/4
A or B or C
A= $parts[0] > 255B= $parts[1] > 255C= $parts[2] > 65535
ABCresult
1XX1
01X1
0011
0000
165 $parts[0] > 0xff ||
166 $parts[1] > 0xff ||
167 $parts[2] > 0xffff;
168 1 $addr = pack("N", $parts[0] << 24 | $parts[1] << 16 | $parts[2]);
169 } elsif (@parts == 4) {
170 # a.b.c.d - 8.8.8.8 bits
171 3 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $parts[0] > 255 or $parts[1] > 255 or $parts[2] > 255 or $parts[3] > 255 0 3 3
Condition33%3/9
$parts[0] > 255 or $parts[1] > 255 or $parts[2] > 255 or $parts[3] > 255
l!l&&r!l&&!r
003
$parts[0] > 255 or $parts[1] > 255 or $parts[2] > 255
l!l&&r!l&&!r
003
$parts[0] > 255 or $parts[1] > 255
l!l&&r!l&&!r
003
MC/DC0%0/4
$parts[0] > 255 or $parts[1] > 255 or $parts[2] > 255 or $parts[3] > 255
$parts[0] > 255 $parts[1] > 255 $parts[2] > 255 $parts[3] > 255
Truth table20%1/5
A or B or C or D
A= $parts[0] > 255B= $parts[1] > 255C= $parts[2] > 255D= $parts[3] > 255
ABCDresult
1XXX1
01XX1
001X1
00011
00000
172 $parts[0] > 0xff ||
173 $parts[1] > 0xff ||
174 $parts[2] > 0xff ||
175 $parts[3] > 0xff;
176 3 $addr = pack("N", $parts[0] << 24 | $parts[1] << 16 | $parts[2] << 8 | $parts[3]);
177 } else {
178 0 return 1;
179 }
180  
181 9 my $haddr = unpack("N", $addr); # host order IP address
182 9 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if ($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520 or ($haddr & 4294967040) == 3221225984 or ($haddr & 4294967040) == 3227017984 or $haddr == 4294967295 or ($haddr & 4026531840) == 3758096384 8 1 9
Condition85%23/27
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520 or ($haddr & 4294967040) == 3221225984 or ($haddr & 4294967040) == 3227017984 or $haddr == 4294967295 or ($haddr & 4026531840) == 3758096384
l!l&&r!l&&!r
801
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520 or ($haddr & 4294967040) == 3221225984 or ($haddr & 4294967040) == 3227017984 or $haddr == 4294967295
l!l&&r!l&&!r
711
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520 or ($haddr & 4294967040) == 3221225984 or ($haddr & 4294967040) == 3227017984
l!l&&r!l&&!r
612
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520 or ($haddr & 4294967040) == 3221225984
l!l&&r!l&&!r
513
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520
l!l&&r!l&&!r
414
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648
l!l&&r!l&&!r
405
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728
l!l&&r!l&&!r
405
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432
l!l&&r!l&&!r
135
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160
l!l&&r!l&&!r
108
MC/DC60%6/10
($haddr & 4278190080) == 0 or ($haddr & 4278190080) == 167772160 or ($haddr & 4278190080) == 2130706432 or ($haddr & 4293918720) == 2886729728 or ($haddr & 4294901760) == 2851995648 or ($haddr & 4294901760) == 3232235520 or ($haddr & 4294967040) == 3221225984 or ($haddr & 4294967040) == 3227017984 or $haddr == 4294967295 or ($haddr & 4026531840) == 3758096384
($haddr & 4278190080) == 0 ($haddr & 4278190080) == 167772160 ($haddr & 4278190080) == 2130706432 ($haddr & 4293918720) == 2886729728 ($haddr & 4294901760) == 2851995648 ($haddr & 4294901760) == 3232235520 ($haddr & 4294967040) == 3221225984 ($haddr & 4294967040) == 3227017984 $haddr == 4294967295 ($haddr & 4026531840) == 3758096384
Truth table63%7/11
A or B or C or D or E or F or G or H or I or J
A= ($haddr & 4278190080) == 0B= ($haddr & 4278190080) == 167772160C= ($haddr & 4278190080) == 2130706432D= ($haddr & 4293918720) == 2886729728E= ($haddr & 4294901760) == 2851995648F= ($haddr & 4294901760) == 3232235520G= ($haddr & 4294967040) == 3221225984H= ($haddr & 4294967040) == 3227017984I= $haddr == 4294967295J= ($haddr & 4026531840) == 3758096384
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
183 ($haddr & 0xFF000000) == 0x00000000 || # 0.0.0.0/8
184 ($haddr & 0xFF000000) == 0x0A000000 || # 10.0.0.0/8
185 ($haddr & 0xFF000000) == 0x7F000000 || # 127.0.0.0/8
186 ($haddr & 0xFFF00000) == 0xAC100000 || # 172.16.0.0/12
187 ($haddr & 0xFFFF0000) == 0xA9FE0000 || # 169.254.0.0/16
188 ($haddr & 0xFFFF0000) == 0xC0A80000 || # 192.168.0.0/16
189 ($haddr & 0xFFFFFF00) == 0xC0000200 || # 192.0.2.0/24 "TEST-NET" docs/example code
190 ($haddr & 0xFFFFFF00) == 0xC0586300 || # 192.88.99.0/24 6to4 relay anycast addresses
191 $haddr == 0xFFFFFFFF || # 255.255.255.255
192 ($haddr & 0xF0000000) == 0xE0000000; # multicast addresses
193  
194 # as final IP address check, pass in the canonical a.b.c.d decimal form
195 # to the blacklisted host check to see if matches as bad there.
196 1 my $can_ip = join(".", map { ord } split //, $addr);
197 1 ▶ return 1 if $self->_host_list_match("blocked_hosts", $can_ip);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_host_list_match("blocked_hosts", $can_ip) 1 0 1
198  
199 # looks like an okay IP address
200 0 return 0;
201 }
202  
203 9 ▶sub request {
Subroutine request: called CC=6 SCAR=18.6
Pod documented
204 9 my ($self, $req, $arg, $size, $previous) = @_;
205  
206 # walk back to the first request, and set our _time_begin to its _time_begin, or if
207 # we're the first, then use current time. used by LWPx::Protocol::http_paranoid
208 9 my $first_res = $previous; # previous is the previous response that invoked this request
209 9 ▶ $first_res = $first_res->previous while $first_res && $first_res->previous;
Branch: 1 branch
BranchTrueFalseTotal
$first_res and $first_res->previous and do { $first_res = $first_res->previous } 0 9 9
Condition33%1/3
$first_res and $first_res->previous
!ll&&!rl&&r
900
MC/DC0%0/2
$first_res and $first_res->previous
$first_res $first_res->previous
Truth table33%1/3
A and B
A= $first_resB= $first_res->previous
ABresult
0X0
100
111
210 9 ▶ $req->{_time_begin} = $first_res ? $first_res->request->{_time_begin} : time();
Branch: 1 branch
BranchTrueFalseTotal
$first_res ? : 0 9 9
211  
212 9 my $host = $req->uri->host;
213 9 ▶ if ($self->_bad_host($host)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_bad_host($host)) 8 1 9
214 8 my $err_res = HTTP::Response->new(403, "Unauthorized access to blocked host");
215 8 $err_res->request($req);
216 8 $err_res->header("Client-Date" => HTTP::Date::time2str(time));
217 8 $err_res->header("Client-Warning" => "Internal response");
218 8 $err_res->header("Content-Type" => "text/plain");
219 8 $err_res->content("403 Unauthorized access to blocked host\n");
220 8 return $err_res;
221 }
222  
223 1 ▶ if (my $pp = $self->{paranoid_proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pp = $self->{"paranoid_proxy"}) 0 1 1
224 $req->uri("$pp?url=" . eurl($req->uri) .
225 "&timeout=" . ($self->{timeout} + 0) .
226 0 "&max_size=" . ($self->{max_size} + 0));
227 }
228  
229 1 return $self->SUPER::request($req, $arg, $size, $previous);
230 }
231  
232 # taken from LWP::UserAgent and modified slightly. (proxy support removed,
233 # and map http and https schemes to separate protocol handlers)
234 sub send_request
235 1 ▶{
Subroutine send_request: called CC=21 SCAR=41.4
Pod undocumented
236 1 my ($self, $request, $arg, $size) = @_;
237 1 $self->_request_sanity_check($request);
238  
239 1 my ($method, $url) = ($request->method, $request->uri);
240  
241 1 local($SIG{__DIE__}); # protect against user defined die handlers
242  
243 # Check that we have a METHOD and a URL first
244 1 ▶ return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "Method missing")
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 1 1
245 unless $method;
246 1 ▶ return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "URL missing")
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 1 1
247 unless $url;
248 1 ▶ return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "URL must be absolute")
Branch: 1 branch
BranchTrueFalseTotal
unless $url->scheme 0 1 1
249 unless $url->scheme;
250 1 ▶ return _new_response($request, &HTTP::Status::RC_BAD_REQUEST,
Branch: 1 branch
BranchTrueFalseTotal
if $self->_need_proxy($url) 0 1 1
251 "ParanoidAgent doesn't support going through proxies. ".
252 "In that case, do your paranoia at your proxy instead.")
253 if $self->_need_proxy($url);
254  
255 1 my $scheme = $url->scheme;
256 1 ▶ return _new_response($request, &HTTP::Status::RC_BAD_REQUEST, "Only http and https are supported by ParanoidAgent")
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme eq "http" or $scheme eq "https" 0 1 1
Condition33%1/3
$scheme eq "http" or $scheme eq "https"
l!l&&r!l&&!r
100
MC/DC0%0/2
$scheme eq "http" or $scheme eq "https"
$scheme eq "http" $scheme eq "https"
Truth table33%1/3
A or B
A= $scheme eq "http"B= $scheme eq "https"
ABresult
1X1
011
000
257 unless $scheme eq "http" || $scheme eq "https";
258  
259 1 LWP::Debug::trace("$method $url");
260  
261 1 my $protocol;
262  
263 {
264 # Honor object-specific restrictions by forcing protocol objects
265 # into class LWP::Protocol::nogo.
266 1 my $x;
267 1 ▶ if($x = $self->protocols_allowed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x = $self->protocols_allowed) { } 0 1 1
elsif ($x = $self->protocols_forbidden) { } 0 1 1
268 0 ▶ if(grep lc($_) eq $scheme, @$x) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep((lc $_ eq $scheme), @$x)) { } 0 0 0
269 0 LWP::Debug::trace("$scheme URLs are among $self\'s allowed protocols (@$x)");
270 }
271 else {
272 0 LWP::Debug::trace("$scheme URLs aren't among $self\'s allowed protocols (@$x)");
273 0 require LWP::Protocol::nogo;
274 0 $protocol = LWP::Protocol::nogo->new;
275 }
276 }
277 elsif ($x = $self->protocols_forbidden) {
278 0 ▶ if(grep lc($_) eq $scheme, @$x) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep((lc $_ eq $scheme), @$x)) { } 0 0 0
279 0 LWP::Debug::trace("$scheme URLs are among $self\'s forbidden protocols (@$x)");
280 0 require LWP::Protocol::nogo;
281 0 $protocol = LWP::Protocol::nogo->new;
282 }
283 else {
284 0 LWP::Debug::trace("$scheme URLs aren't among $self\'s forbidden protocols (@$x)");
285 }
286 }
287 # else fall thru and create the protocol object normally
288 }
289  
290 1 ▶ unless ($protocol) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($protocol) 1 0 1
291 1 LWP::Protocol::implementor("${scheme}_paranoid", "LWPx::Protocol::${scheme}_paranoid");
292 1 eval "require LWPx::Protocol::${scheme}_paranoid;";
293 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
294 0 $@ =~ s/ at .* line \d+.*//s; # remove file/line number
295 0 my $response = _new_response($request, &HTTP::Status::RC_NOT_IMPLEMENTED, $@);
296 0 return $response;
297 }
298  
299 1 ▶ $protocol = eval { LWP::Protocol::create($scheme eq "http" ? "http_paranoid" : "https_paranoid", $self) };
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'http' ? : 1 0 1
300 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
301 0 $@ =~ s/ at .* line \d+.*//s; # remove file/line number
302 0 my $response = _new_response($request, &HTTP::Status::RC_NOT_IMPLEMENTED, $@);
303 0 ▶ if ($scheme eq "https") {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq "https") 0 0 0
304 0 $response->message($response->message . " (Crypt::SSLeay not installed)");
305 0 $response->content_type("text/plain");
306 0 $response->content(<<EOT);
307 LWP will support https URLs if the Crypt::SSLeay module is installed.
308 More information at <http://www.linpro.no/lwp/libwww-perl/README.SSL>.
309 EOT
310 }
311 0 return $response;
312 }
313 }
314  
315 # Extract fields that will be used below
316 my ($timeout, $cookie_jar, $use_eval, $parse_head, $max_size) =
317 1 @{$self}{qw(timeout cookie_jar use_eval parse_head max_size)};
318  
319 1 my $response;
320 1 my $proxy = undef;
321 1 ▶ if ($use_eval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_eval) { } 1 0 1
322 # we eval, and turn dies into responses below
323 1 eval {
324 1 $response = $protocol->request($request, $proxy,
325 $arg, $size, $timeout);
326 };
327 1 ▶ my $error = $@ || $response->header( 'x-died' );
Condition33%1/3
$@ || $response->header('x-died')
l!l&&r!l&&!r
100
MC/DC0%0/2
$@ || $response->header('x-died')
$@ $response->header('x-died')
Truth table33%1/3
A || B
A= $@B= $response->header('x-died')
ABresult
1X1
011
000
328 1 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 0 1
329 1 $error =~ s/ at .* line \d+.*//s; # remove file/line number
330 1 $response = _new_response($request,
331 &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
332 $error);
333 }
334 }
335 else {
336 0 $response = $protocol->request($request, $proxy,
337 $arg, $size, $timeout);
338 # XXX: Should we die unless $response->is_success ???
339 }
340  
341 1 $response->request($request); # record request for reference
342 1 ▶ $cookie_jar->extract_cookies($response) if $cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie_jar 0 1 1
343 1 $response->header("Client-Date" => HTTP::Date::time2str(time));
344 1 ▶ $self->run_handlers("response_done", $response) if $self->can('run_handlers');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("run_handlers") 1 0 1
345 1 return $response;
346 }
347  
348 # blocked hostnames, compiled patterns, or subrefs
349 sub blocked_hosts
350 1 ▶{
Subroutine blocked_hosts: called CC=3 SCAR=13.5
Pod documented
351 1 my $self = shift;
352 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
353 1 my @hosts = @_;
354 1 $self->{'blocked_hosts'} = \@hosts;
355 1 return;
356 }
357 0 ▶ return @{ $self->{'blocked_hosts'} || [] };
Condition0%0/2
$self->{'blocked_hosts'} || []
l!l
00
MC/DC0%0/1
$self->{'blocked_hosts'} || []
$self->{'blocked_hosts'}
Truth table0%0/2
A
A= $self->{'blocked_hosts'}
Aresult
11
00
358 }
359  
360 # whitelisted hostnames, compiled patterns, or subrefs
361 sub whitelisted_hosts
362 1 ▶{
Subroutine whitelisted_hosts: called CC=3 SCAR=13.5
Pod documented
363 1 my $self = shift;
364 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
365 1 my @hosts = @_;
366 1 $self->{'whitelisted_hosts'} = \@hosts;
367 1 return;
368 }
369 0 ▶ return @{ $self->{'whitelisted_hosts'} || [] };
Condition0%0/2
$self->{'whitelisted_hosts'} || []
l!l
00
MC/DC0%0/1
$self->{'whitelisted_hosts'} || []
$self->{'whitelisted_hosts'}
Truth table0%0/2
A
A= $self->{'whitelisted_hosts'}
Aresult
11
00
370 }
371  
372 # get/set Net::DNS resolver object
373 sub resolver
374 3 ▶{
Subroutine resolver: called CC=4 SCAR=14.3
Pod documented
375 3 my $self = shift;
376 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
377 2 $self->{'resolver'} = shift;
378 2 require UNIVERSAL ;
379 die "Not a Net::DNS::Resolver object" unless
380 2 ▶ UNIVERSAL::isa($self->{'resolver'}, "Net::DNS::Resolver");
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self->{"resolver"}, "Net::DNS::Resolver") 0 2 2
381 }
382 3 ▶ return $self->{'resolver'} ||= Net::DNS::Resolver->new;
Condition33%1/3
$self->{"resolver"} ||= "Net::DNS::Resolver"->new
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"resolver"} ||= "Net::DNS::Resolver"->new
$self->{"resolver"} "Net::DNS::Resolver"->new
Truth table33%1/3
A ||= B
A= $self->{"resolver"}B= "Net::DNS::Resolver"->new
ABresult
1X1
011
000
383 }
384  
385 # Taken directly from LWP::UserAgent because it was private there, and we can't depend on it
386 # staying there in future versions: needed by our modified version of send_request
387 sub _need_proxy
388 1 ▶{
Subroutine _need_proxy: called CC=9 SCAR=36.7
389 1 my($self, $url) = @_;
390 1 ▶ $url = $HTTP::URI_CLASS->new($url) unless ref $url;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $url 0 1 1
391  
392 1 ▶ my $scheme = $url->scheme || return;
Condition50%1/2
$url->scheme || (return)
l!l
10
MC/DC0%0/1
$url->scheme || (return)
$url->scheme
Truth table50%1/2
A
A= $url->scheme
Aresult
11
00
393 1 ▶ if (my $proxy = $self->{'proxy'}{$scheme}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proxy = $self->{"proxy"}{$scheme}) 0 1 1
394 0 ▶ if ($self->{'no_proxy'} && @{ $self->{'no_proxy'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_proxy"} and @{$self->{"no_proxy"};}) 0 0 0
Condition0%0/3
$self->{"no_proxy"} and @{$self->{"no_proxy"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"no_proxy"} and @{$self->{"no_proxy"};}
$self->{"no_proxy"} @{$self->{"no_proxy"};}
Truth table0%0/3
A and B
A= $self->{"no_proxy"}B= @{$self->{"no_proxy"};}
ABresult
0X0
100
111
395 0 ▶ if (my $host = eval { $url->host }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = eval { do { $url->host } }) 0 0 0
396 0 for my $domain (@{ $self->{'no_proxy'} }) {
397 0 ▶ if ($host =~ /\Q$domain\E$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /\Q$domain\E$/) 0 0 0
398 0 LWP::Debug::trace("no_proxy configured");
399 0 return;
400 }
401 }
402 }
403 }
404 0 LWP::Debug::debug("Proxied to $proxy");
405 0 return $HTTP::URI_CLASS->new($proxy);
406 }
407 1 LWP::Debug::debug('Not proxied');
408 1 undef;
409 }
410  
411 # Taken directly from LWP::UserAgent because it was private there, and we can't depend on it
412 # staying there in future versions: needed by our modified version of send_request
413 1 ▶sub _request_sanity_check {
Subroutine _request_sanity_check: called CC=7 SCAR=26.4
414 1 my($self, $request) = @_;
415 # some sanity checking
416 1 ▶ if (defined $request) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request) { } 1 0 1
417 1 ▶ if (ref $request) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $request) { } 1 0 1
418 1 ▶ Carp::croak("You need a request object, not a " . ref($request) . " object")
Branch: 1 branch
BranchTrueFalseTotal
if ref $request eq "ARRAY" or ref $request eq "HASH" or not $request->can("method") or not $request->can("uri") 0 1 1
Condition33%3/9
ref $request eq "ARRAY" or ref $request eq "HASH" or not $request->can("method") or not $request->can("uri")
l!l&&r!l&&!r
001
ref $request eq "ARRAY" or ref $request eq "HASH" or not $request->can("method")
l!l&&r!l&&!r
001
ref $request eq "ARRAY" or ref $request eq "HASH"
l!l&&r!l&&!r
001
MC/DC0%0/4
ref $request eq "ARRAY" or ref $request eq "HASH" or not $request->can("method") or not $request->can("uri")
ref $request eq "ARRAY" ref $request eq "HASH" not $request->can("method") not $request->can("uri")
Truth table20%1/5
A or B or not(C) or not(D)
A= ref $request eq "ARRAY"B= ref $request eq "HASH"C= not $request->can("method")D= not $request->can("uri")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
419 if ref($request) eq 'ARRAY' or ref($request) eq 'HASH' or
420 !$request->can('method') or !$request->can('uri');
421 }
422 else {
423 0 Carp::croak("You need a request object, not '$request'");
424 }
425 }
426 else {
427 0 Carp::croak("No request object passed in");
428 }
429 }
430  
431 # Taken directly from LWP::UserAgent because it was private there, and we can't depend on it
432 # staying there in future versions: needed by our modified version of send_request
433 1 ▶sub _new_response {
Subroutine _new_response: called CC=1 SCAR=0.0
434 1 my($request, $code, $message) = @_;
435 1 my $response = HTTP::Response->new($code, $message);
436 1 $response->request($request);
437 1 $response->header("Client-Date" => HTTP::Date::time2str(time));
438 1 $response->header("Client-Warning" => "Internal response");
439 1 $response->header("Content-Type" => "text/plain");
440 1 $response->content("$code $message\n");
441 1 return $response;
442 }
443  
444 0 ▶sub eurl {
Subroutine eurl: not called CC=1 SCAR=6.9
Pod undocumented
445 0 my $a = $_[0];
446 0 $a =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg;
447 0 $a =~ tr/ /+/;
448 0 return $a;
449 }
450  
451 2 1;
452  
453 __END__
« Protocol/http_paranoid.pm ↑ Summary Protocol/https_paranoid.pm »