blib/lib/WWW/ProxyChecker.pm

filter: Statementstmt 32.0% 34 / 106 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 69.2% 9 / 13 Podpod 66.6% 2 / 3 totaltotal 28.9% 46 / 159 CC 27 SCAR 46.0
CC
27
Cov
25%
_start_checker
240.0
_check_proxy
30.0
fastest
20.0
CRAP 99.7
↑ Summary
1 package WWW::ProxyChecker;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # VERSION
7  
8 3 our $VERSION = '1.005';
9  
10 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 __PACKAGE__->mk_group_accessors(simple => qw/
18 max_kids
19 debug
20 alive
21 check_sites
22 max_working_per_kid
23 timeout
24 agent
25 /);
26  
27 3 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
28 3 my $self = bless {}, shift;
29 3 ▶ croak "Must have even number of arguments to new()"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 3 3
30 if @_ & 1;
31  
32 3 my %args = @_;
33 3 $args{ +lc } = delete $args{ $_ } for keys %args;
34  
35 3 %args = (
36 timeout => 5,
37 max_kids => 20,
38 check_sites => [ qw(
39 http://google.com
40 http://microsoft.com
41 http://yahoo.com
42 http://digg.com
43 http://facebook.com
44 http://myspace.com
45 )
46 ],
47 agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12)'
48 .' Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12',
49  
50 %args,
51 );
52  
53 3 $self->$_( $args{ $_ } ) for keys %args;
54  
55 3 return $self;
56 }
57 0 ▶sub check {
Subroutine check: not called CC=3 SCAR=24.8
Pod documented
58 0 my ( $self, $proxies_ref ) = @_;
59  
60 0 $self->alive(undef);
61 0 $self->{fastest} = {};
62  
63 0 ▶ warn "About to check " . @$proxies_ref . " proxies\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
64 if $self->debug;
65  
66 0 my $working_ref = $self->_start_checker( @$proxies_ref );
67  
68 0 ▶ warn @$working_ref . ' out of ' . @$proxies_ref
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
69 . " seem to be alive\n" if $self->debug;
70  
71 0 return $self->alive($working_ref);
72 }
73 0 ▶sub fastest {
Subroutine fastest: not called CC=4 SCAR=30.0
Pod undocumented
74 0 my $self = shift;
75  
76 0 my @debug_list;
77 my @fastest;
78  
79 0 my %list = %{ $self->{fastest} };
80  
81 0 for my $proxy (sort { $list{$a} <=> $list{$b} } keys %list){
82 0 $list{$proxy} = sprintf("%.2f", $list{$proxy});
83 0 push @debug_list, "$proxy :: $list{$proxy}";
84 0 push @fastest, $proxy;
85 }
86  
87 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
88 0 warn "$_\n" for @debug_list;
89 }
90  
91 0 return \@fastest;
92 }
93 0 ▶sub _start_checker {
Subroutine _start_checker: not called CC=15 SCAR=54.8
94 0 my ( $self, @proxies ) = @_;
95  
96 0 my $n = $self->max_kids;
97 0 ▶ $n > @proxies and $n = @proxies;
Branch: 1 branch
BranchTrueFalseTotal
$n > @proxies and $n = @proxies 0 0 0
98 0 my $mod = @proxies / $n;
99 0 my %prox;
100 0 for ( 1 .. $n ) {
101 0 $prox{ $_ } = [ splice @proxies, 0,$mod ]
102 }
103 0 push @{ $prox{ $n } }, @proxies; # append any left over addresses
104  
105 0 $SIG{CHLD} = 'IGNORE';
106 0 my @children;
107 0 for my $num ( 1 .. $self->max_kids ) {
108 0 my $pipe = new IO::Pipe;
109 0 my $time;
110  
111 0 ▶ if ( my $pid = fork ) { # parent
Branch: 2 branches
BranchTrueFalseTotal
if (my $pid = fork) { } 0 0 0
elsif (defined $pid) { } 0 0 0
112 0 $pipe->reader;
113 0 push @children, $pipe;
114 }
115 elsif ( defined $pid ) { # kid
116 0 $pipe->writer;
117  
118 0 my $ua = LWP::UserAgent->new(
119 timeout => $self->timeout,
120 agent => $self->agent,
121 );
122  
123 0 my $check_sites_ref = $self->check_sites;
124 0 my $debug = $self->debug;
125 0 my @working;
126 0 for my $proxy ( @{ $prox{ $num } } ) {
127 0 ▶ warn "Checking $proxy in kid $num\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
128 if $debug;
129  
130 0 ▶ if ($time = $self->_check_proxy($ua, $proxy, $check_sites_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time = $self->_check_proxy($ua, $proxy, $check_sites_ref)) 0 0 0
131 0 push @working, $proxy;
132  
133 last
134 0 ▶ if defined $self->max_working_per_kid
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->max_working_per_kid and @working >= $self->max_working_per_kid 0 0 0
Condition0%0/3
defined $self->max_working_per_kid and @working >= $self->max_working_per_kid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->max_working_per_kid and @working >= $self->max_working_per_kid
defined $self->max_working_per_kid @working >= $self->max_working_per_kid
Truth table0%0/3
A and B
A= defined $self->max_working_per_kidB= @working >= $self->max_working_per_kid
ABresult
0X0
100
111
135 and @working >= $self->max_working_per_kid;
136 }
137 }
138 0 print $pipe "$_ $time\n" for @working;
139 0 exit;
140 }
141 else { # error
142 0 carp "Failed to fork kid number $num ($?)";
143 }
144  
145 }
146  
147 0 my @working_proxies;
148 0 for my $num ( 0 .. $#children ) {
149 0 my $fh = $children[$num];
150 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
151 0 chomp;
152 0 my ($proxy, $time) = split;
153 0 ▶ $self->{fastest}{$proxy} = $time if $time != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $time != 1 0 0 0
154 0 push @working_proxies, $proxy;
155 }
156 }
157  
158 0 return \@working_proxies;
159 }
160 0 ▶sub _check_proxy {
Subroutine _check_proxy: not called CC=5 SCAR=34.0
161 0 my ( $self, $ua, $proxy, $sites_ref ) = @_;
162  
163 0 $ua->proxy( [ 'http', 'https', 'ftp', 'ftps' ], $proxy);
164  
165 0 my $start = gettimeofday();
166  
167 0 my $response = $ua->get( $sites_ref->[rand @$sites_ref] );
168  
169 0 my $time = gettimeofday() - $start;
170  
171 0 ▶ if ( $response->is_success ){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
172 0 return $time;
173 }
174 else {
175 0 ▶ warn "Failed on $proxy " . $response->status_line . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
176 if $self->debug;
177  
178 0 my $response_code = $response->code;
179 return 0
180 0 ▶ if grep { $response_code eq $_ } qw(407 502 503 403);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$response_code eq $_;} 407, 502, 503, 403 0 0 0
181  
182 0 ( my $proxy_no_scheme = $proxy ) =~ s{(?:ht|f)tps?://}{}i;
183 0 ▶ return $response->status_line
Branch: 1 branch
BranchTrueFalseTotal
$response->status_line =~ /^500 read timeout$|\Q$proxy_no_scheme\E/ ? : 0 0 0
184 =~ /^500 read timeout$|\Q$proxy_no_scheme/ ? 0 : 1;
185 }
186 }
187  
188 3 1;
189 __END__
↑ Summary