File Coverage

blib/lib/WWW/Link/Tester/Simple.pm
Criterion Covered Total %
statement 56 62 90.3
branch 10 16 62.5
condition 1 3 33.3
subroutine 8 8 100.0
pod 0 2 0.0
total 75 91 82.4


line stmt bran cond sub pod time code
1             package WWW::Link::Tester::Simple;
2             $REVISION=q$Revision: 1.9 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
3              
4             =head1 NAME
5              
6             WWW::Link::Tester::Simple - a link tester that uses the LWP head method
7              
8             =head1 SYNOPSIS
9              
10             use WWW::Link::Test::Simple
11             $ua=create_a_user_agent();
12             $link=get_a_link_object();
13             WWW::Link::Test::Simple::test_link($ua, $link);
14             WWW::Link::Tester::Simple::Test($url)
15              
16             =head1 DESCRIPTION
17              
18             This is a simple Link Testing module which accepts a url and returns a
19             status based on the result returned by the LWP useragent.
20              
21             The link is tested and then given information about what was
22             discovered. The link then records this information for future use..
23              
24             =head1 METHODS
25              
26             =head2 test_link
27              
28             This function tests a link by going out to the world and checking it
29             and then telling the associated link object what happened.
30              
31             =cut
32              
33 3     3   18 use HTTP::Response;
  3         6  
  3         85  
34 3     3   920 use HTTP::Request;
  3         936  
  3         71  
35 3     3   18 use WWW::Link::Tester;
  3         7  
  3         269  
36             @ISA="WWW::Link::Tester";
37 3     3   18 use warnings;
  3         6  
  3         121  
38 3     3   16 use strict;
  3         6  
  3         107  
39 3     3   1462 use Carp;
  3         6  
  3         4039  
40              
41             sub new {
42 4     4 0 62 my $proto = shift;
43 4   33     29 my $class = ref($proto) || $proto;
44 4         9 my $self = {};
45 4         17 $self->{"user_agent"}=shift;
46 4         12 bless $self, $class;
47 4         42 $self->config();
48 4         14 return $self;
49             }
50              
51             sub get_response {
52 30     30 0 126 my $self=shift;
53 30         36 my $link=shift;
54 30         50 my $user_agent=$self->{"user_agent"};
55 30         73 my $url=$link->url();
56 30         35 my $supported;
57              
58 30         44 my $verbose=$self->{"verbose"};
59              
60 30         105 my $urlo=new URI($url);
61 30         27232 my $proto=$urlo->scheme();
62              
63 30         942 eval { $supported = $user_agent->is_protocol_supported($proto) ; };
  30         118  
64              
65             #if it get's really upset, is_protocol_supported sometimes dies. We
66             #just treat this as an unsupported link.
67              
68 30 50       222 $@ && do {
69 0         0 warn $@;
70 0         0 $supported=0;
71             };
72              
73 30 100       63 $supported or do {
74 6         198 my $response=new HTTP::Response ( RC_PROTOCOL_UNSUPPORTED,
75             MSG_PROTOCOL_UNSUPPORTED );
76 6         381 return $response;
77             };
78              
79 24         159 my $request=new HTTP::Request ('HEAD',$url);
80              
81 24 50       2191 print STDERR "sending request\n" if $verbose;
82 24         83 my $response=$user_agent->simple_request($request);
83 24 50       2173 print STDERR "got response\n" if $verbose;
84             #warn on client error
85              
86 24 50       61 if ($self->{warn_access}) { # warn about links where we can't access it
87             # but someone might
88 0         0 print STDERR "didn't have authorisation\n";
89             }
90              
91 24         26 my @redirects;
92 24         69 REDIRECT: while ($response->is_redirect()) {
93 32         2833 my $loc = $response->headers->header('Location');
94 32 50       1125 (defined $loc) || do {
95 0         0 carp "redirect with no location!";
96 0         0 $response=new HTTP::Response (RC_REDIRECT_LIMIT_EXCEEDED,
97             MSG_REDIRECT_LIMIT_EXCEEDED);
98 0         0 last REDIRECT;
99             };
100 32 50       60 print STDERR "have a redirect: " . $loc . "\n" if $verbose;
101              
102 32         43 push @redirects, $response;
103              
104 32 100       79 (@redirects == $self->{"max_redirects"}) && do {
105 2         10 $response=new HTTP::Response (RC_REDIRECT_LIMIT_EXCEEDED,
106             MSG_REDIRECT_LIMIT_EXCEEDED);
107 2         73 last;
108             };
109 30         82 my $request=new HTTP::Request ('HEAD',$loc);
110 30         3060 $response=$user_agent->simple_request($request);
111             }
112 24         520 return $response, @redirects;
113             }
114              
115              
116             1; #kEEp rEqUIrE HaPpY.
117              
118