File Coverage

blib/lib/WWW/Mechanize/Plugin/FollowMetaRedirect.pm
Criterion Covered Total %
statement 33 35 94.2
branch 15 18 83.3
condition 18 24 75.0
subroutine 7 8 87.5
pod 1 2 50.0
total 74 87 85.0


line stmt bran cond sub pod time code
1             #
2              
3             package WWW::Mechanize::Plugin::FollowMetaRedirect;
4              
5 3     3   2543349 use strict;
  3         7  
  3         102  
6 3     3   13 use warnings;
  3         5  
  3         79  
7 3     3   13 use vars qw($VERSION);
  3         8  
  3         126  
8 3     3   1089 use HTML::TokeParser;
  3         12400  
  3         277  
9              
10             $VERSION = '0.03';
11              
12             sub init {
13 3     3   17 no strict 'refs'; ## no critic
  3         7  
  3         1096  
14 0     0 0 0 *{caller() . '::follow_meta_redirect'} = \&follow_meta_redirect;
  0         0  
15             }
16              
17             sub follow_meta_redirect {
18 19     19 1 629978 my ($mech, %args) = @_;
19 19 100 100     351 my $waiting = ( defined $args{ignore_wait} and $args{ignore_wait} ) ? 0 : 1;
20              
21 19 50       189 my $p = HTML::TokeParser->new( \ $mech->content )
22             or return;
23              
24 19         6546 while( my $token = $p->get_token ){
25             # the line should emerge before
26 163 100 100     5468 last if $token->[0] eq 'E' && $token->[1] eq 'head';
27            
28 161         315 my ($url, $sec) = &_extract( $token );
29 161 100 66     1101 next if ! defined $url || $url eq '';
30            
31 17 100 66     62 if( $waiting and defined $sec ){
32 2         2000353 sleep int $sec;
33             }
34            
35 17         120 return $mech->get( $url );
36             }
37              
38 2         43 return;
39             }
40              
41             *WWW::Mechanize::follow_meta_redirect = \&follow_meta_redirect;
42              
43             sub _extract {
44 161     161   197 my $token = shift;
45              
46 161 100 100     655 if( $token->[0] eq 'S' and $token->[1] eq 'meta' ){
47 26 50 33     240 if( defined $token->[2] and ref $token->[2] eq 'HASH' ){
48 26 100 100     287 if( defined $token->[2]->{'http-equiv'} and $token->[2]->{'http-equiv'} =~ /^refresh$/io ){
49 17 50 33     288 if( defined $token->[2]->{'content'} and $token->[2]->{'content'} =~ m|^(([0-9]+)\s*;\s*)*url\='?([^']+)'?$|io ){
50 17         114 return ($3, $2);
51             }
52             }
53             }
54             }
55              
56 144         236 return;
57             }
58              
59             1;
60              
61             __END__