File Coverage

blib/lib/HTTP/SimpleLinkChecker.pm
Criterion Covered Total %
statement 26 32 81.2
branch 3 6 50.0
condition 1 3 33.3
subroutine 8 8 100.0
pod 2 2 100.0
total 40 51 78.4


line stmt bran cond sub pod time code
1             package HTTP::SimpleLinkChecker;
2 6     6   71430 use strict;
  6         11  
  6         276  
3              
4 6     6   35 use warnings;
  6         10  
  6         223  
5 6     6   36 no warnings;
  6         13  
  6         287  
6              
7 6     6   31 use Exporter qw(import);
  6         10  
  6         329  
8              
9 6     6   45 use vars qw($ERROR $VERSION @EXPORT_OK);
  6         17  
  6         526  
10              
11 6     6   4546 use Mojo::UserAgent;
  6         1902853  
  6         64  
12              
13             @EXPORT_OK = qw(check_link);
14              
15             my $UA = Mojo::UserAgent->new();
16             $UA->proxy->detect;
17             $UA->max_redirects(3);
18              
19             $VERSION = '1.165';
20              
21             sub check_link {
22 5     5 1 6355 my $link = shift;
23 5 50       23 unless( defined $link ) {
24 0         0 $ERROR = 'Received no argument';
25 0         0 return;
26             }
27              
28 5         30 my $transaction = $UA->head($link);
29 5         2515928 my $response = $transaction->res;
30              
31 5 50 33     72 if( ! $response and $response->code >= 400 ) {
32 0         0 $transaction = $UA->get($link);
33 0         0 $response = $transaction->res;
34             }
35              
36 5 50       30 unless( ref $response ) {
37 0         0 $ERROR = 'Could not get response';
38 0         0 return;
39             }
40              
41 5         29 return $response->code;
42             }
43              
44 2     2 1 1762 sub user_agent { $UA }
45              
46             1;
47              
48             =encoding utf8
49              
50             =head1 NAME
51              
52             HTTP::SimpleLinkChecker - Check the HTTP response code for a link
53              
54             =head1 SYNOPSIS
55              
56             use HTTP::SimpleLinkChecker;
57              
58             my $code = HTTP::SimpleLinkChecker::check_link($url);
59              
60             unless( defined $code ) {
61             print "Error: $HTTP::SimpleLinkChecker::ERROR\n";
62             }
63              
64             =head1 DESCRIPTION
65              
66             You don't have to know anything about objected-oriented Perl, LWP, or
67             the HTTP module to be able to check your links. This module is
68             designed for the casual user. It has one function, C, that
69             returns the HTTP response code that it receives when it tries to fetch
70             the web address passed to it. The undef value is returned for any
71             non-HTTP failure and the C<$HTTP::SimpleLinkChecker::ERROR> variable
72             is set.
73              
74             The HEAD method is tried first, although if anything other than a good
75             status code (those less than 400) is received, another request is made
76             with the GET method.
77              
78             The user-agent will automatically handle redirects. If you don't like
79             that, you can change the user agent settings before you start:
80              
81             HTTP::SimpleLinkChecker::user_agent()->max_redirects(0);
82              
83             The user agent is L, so anything you do with that
84             module you can do with the user agent.
85              
86             Note that even with the best code, no module can control how
87             servers decide to respond to a check, or control any of the myriad
88             things that can go wrong with the network between you and the remote
89             server. Some may filter requests based on origin IP address,
90             user-agent type, or any other arbitrary factor. Some servers may not
91             respond correctly at all. Furthermore, some servers might be
92             temporarily down or overloaded. I recommend that you recheck "broken"
93             links a couple times over a long period (like a day or two) before you
94             decide they are really broken.
95              
96             If you are behind a firewall or proxy, this module picks up those
97             settings through L's C method. See
98             L for more details.
99              
100             =head2 Functions
101              
102             =over 4
103              
104             =item check_link( URL )
105              
106             Returns the HTTP response code for URL.
107              
108             =item user_agent
109              
110             Returns a reference to the Mojo::UserAgent object. You
111             can affect it directly. See L.
112              
113             my $ua = HTTP::SimpleLinkChecker::user_agent();
114             $ua->transactor->name( 'Mozilla 19.2' );
115              
116             =back
117              
118             =head1 SOURCE AVAILABILITY
119              
120             This source is in Github:
121              
122             http://github.com/briandfoy/http-simplelinkchecker
123              
124             If, for some reason, I disappear from the world, one of the other
125             members of the project can shepherd this module appropriately.
126              
127             =head1 AUTHOR
128              
129             brian d foy, C<< >>
130              
131             =head2 Contributors
132              
133             =over 4
134              
135             =item * Sebastian Paaske Tørholm, C<< >>
136              
137             =back
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             Copyright © 2004-2016, brian d foy . All rights reserved.
142              
143             This program is free software; you can redistribute it and/or modify
144             it under the same terms as Perl itself.
145              
146             =cut