File Coverage

blib/lib/HTTP/SimpleLinkChecker.pm
Criterion Covered Total %
statement 29 35 82.8
branch 3 6 50.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 2 2 100.0
total 44 55 80.0


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