blib/lib/HON/Http/UrlChecker/Service.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 20 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 100% 126 / 126 CC 16 SCAR 14.3
CC
16
Cov
100%
p_retrieveInfo
7.0
p_isUrlAllowed
7.0
p_parseResponse
3.0
CRAP 4.2
↑ Summary
1 package HON::Http::UrlChecker::Service;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 HON::Http::UrlChecker::Service - HTTP Status Code Checker
15  
16 =head1 VERSION
17  
18 Version 0.02
19  
20 =cut
21  
22 5 our $VERSION = '0.02';
23  
24 =head1 SYNOPSIS
25  
26 use HON::Http::UrlChecker::Service;
27  
28 my @listOfStatus = checkUrl('http://www.example.com');
29  
30 =head1 DESCRIPTION
31  
32 Check status code, response headers, redirect location and redirect chain
33 of a HTTP connection.
34  
35 =cut
36  
37 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 our @EXPORT_OK =
39 qw/p_createUserAgent p_getUrl p_parseResponse p_isUrlAllowed checkUrl/;
40  
41 5 Readonly::Scalar my $TIMEOUT => 1200;
42  
43 5 Readonly::Scalar my $MAXREDIRECT => 10;
44  
45 5 Readonly::Array my @HEADERFIELDS => qw(
46 location
47 server
48 content-type
49 title
50 date
51 );
52  
53 5 Readonly::Array my @RESPONSEFIELDS => qw(
54 protocol
55 code
56 message
57 );
58  
59 =head1 SUBROUTINES/METHODS
60  
61 =head2 checkUrl( $url )
62  
63 Check a url (status code, response headers, redirect location and
64 redirect chain).
65  
66 =cut
67  
68 8 ▶sub checkUrl {
Subroutine checkUrl: called CC=2 SCAR=6.9
Pod documented
69 8 my $url = shift;
70  
71 8 ▶ if ( p_isUrlAllowed($url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_isUrlAllowed($url)) { } 2 6 8
72 2 my $ua = p_createUserAgent();
73 2 my $response = p_getUrl( $ua, $url );
74 2 my @listStatus = p_parseResponse($response);
75  
76 2 return @listStatus;
77 }
78 else {
79 6 croak "Wrong url: $url";
80 }
81 }
82  
83 =head1 PRIVATE SUBROUTINES/METHODS
84  
85 =head2 p_createUserAgent
86  
87 Return a LWP::UserAgent.
88 LWP::UserAgent objects can be used to dispatch web requests.
89  
90 =cut
91  
92 3 ▶sub p_createUserAgent {
Subroutine p_createUserAgent: called CC=1 SCAR=0.0
Pod documented
93 3 my $ua = LWP::UserAgent->new;
94  
95 3 $ua->timeout($TIMEOUT);
96 3 $ua->agent('HonBot');
97 3 $ua->env_proxy;
98 3 $ua->max_redirect($MAXREDIRECT);
99  
100 3 return $ua;
101 }
102  
103 =head2 p_getUrl
104  
105 Dispatch a GET request on the given $url
106 The return value is a response object. See HTTP::Response for a description
107 of the interface it provides.
108  
109 =cut
110  
111 4 ▶sub p_getUrl {
Subroutine p_getUrl: called CC=1 SCAR=0.0
Pod documented
112 4 my ( $ua, $url ) = @_;
113  
114 4 return $ua->get($url);
115 }
116  
117 =head2 p_retrieveInfo
118  
119 Retrieve desired fields from an HTTP::Response
120  
121 =cut
122  
123 5 ▶sub p_retrieveInfo {
Subroutine p_retrieveInfo: called CC=7 SCAR=19.5
Pod documented
124 5 my $response = shift;
125  
126 5 my %locationStatus = ();
127 5 foreach my $field (@HEADERFIELDS) {
128 25 ▶ if ( defined $response->header($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->header($field)) 8 17 25
129 8 $locationStatus{$field} = $response->header($field);
130 }
131 }
132  
133 5 foreach my $field (@RESPONSEFIELDS) {
134 15 ▶ if ( defined $response->$field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->$field) 9 6 15
135 9 $locationStatus{$field} = $response->$field;
136  
137 # Put status code in integer
138 9 ▶ if ( $field eq 'code' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "code") 5 4 9
139 5 $locationStatus{$field} = $locationStatus{$field} + 0;
140 }
141  
142 9 ▶ if ( $field eq 'message' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "message") 4 5 9
143 9 $locationStatus{$field} = join q{ }, map { ucfirst lc } split q{ },
144 4 $locationStatus{$field};
145 }
146 }
147 }
148  
149 5 return %locationStatus;
150 }
151  
152 =head2 p_parseResponse
153  
154 Retrieve a list of Status from HTTP::Response
155  
156 =cut
157  
158 4 ▶sub p_parseResponse {
Subroutine p_parseResponse: called CC=3 SCAR=11.0
Pod documented
159 4 my $response = shift;
160  
161 4 my @listStatus = ();
162 4 my @redirects = $response->redirects;
163 4 ▶ if ( scalar @redirects > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @redirects > 0) 1 3 4
164 1 foreach my $redirect (@redirects) {
165 1 my %status = p_retrieveInfo($redirect);
166 1 push @listStatus, \%status;
167 }
168 }
169 4 my %status = p_retrieveInfo($response);
170 4 push @listStatus, \%status;
171  
172 4 return @listStatus;
173 }
174  
175 =head2 p_isUrlAllowed
176  
177 Check if the url is formatted correctly
178  
179 =cut
180  
181 12 ▶sub p_isUrlAllowed {
Subroutine p_isUrlAllowed: called CC=7 SCAR=19.5
Pod documented
182 12 my $url = shift;
183  
184 12 ▶ return unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 2 10 12
185 10 my $uri = URI->new($url);
186 10 ▶ return unless $uri->scheme and $uri->opaque;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme and $uri->opaque 5 5 10
Condition100%3/3
$uri->scheme and $uri->opaque
!ll&&!rl&&r
415
MC/DC100%2/2
$uri->scheme and $uri->opaque
$uri->scheme $uri->opaque
Truth table100%3/3
A and B
A= $uri->schemeB= $uri->opaque
ABresult
0X0
100
111
187  
188 5 ▶ if ( $uri->scheme eq 'http' or $uri->scheme eq 'https' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme eq "http" or $uri->scheme eq "https") 2 3 5
Condition100%3/3
$uri->scheme eq "http" or $uri->scheme eq "https"
l!l&&r!l&&!r
113
MC/DC100%2/2
$uri->scheme eq "http" or $uri->scheme eq "https"
$uri->scheme eq "http" $uri->scheme eq "https"
Truth table100%3/3
A or B
A= $uri->scheme eq "http"B= $uri->scheme eq "https"
ABresult
1X1
011
000
189 2 ▶ return unless $uri->authority;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->authority 1 1 2
190 }
191  
192 4 return 1;
193 }
194  
195 =head1 AUTHOR
196  
197 William Belle, C<< <william.belle at gmail.com> >>
198  
199 =head1 BUGS AND LIMITATIONS
200  
201 Please report any bugs or feature requests to C<bug-hon-http-urlchecker-service at rt.cpan.org>, or through
202 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HON-Http-UrlChecker-Service>. I will be notified, and then you'll
203 automatically be notified of progress on your bug as I make changes.
204  
205 =head1 SUPPORT
206  
207 You can find documentation for this module with the perldoc command.
208  
209 perldoc HON::Http::UrlChecker::Service
210  
211 You can also look for information at:
212  
213 =over 4
214  
215 =item * RT: CPAN's request tracker (report bugs here)
216  
217 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HON-Http-UrlChecker-Service>
218  
219 =item * AnnoCPAN: Annotated CPAN documentation
220  
221 L<http://annocpan.org/dist/HON-Http-UrlChecker-Service>
222  
223 =item * CPAN Ratings
224  
225 L<http://cpanratings.perl.org/d/HON-Http-UrlChecker-Service>
226  
227 =item * Search CPAN
228  
229 L<http://search.cpan.org/dist/HON-Http-UrlChecker-Service/>
230  
231 =back
232  
233 =head1 LICENSE AND COPYRIGHT
234  
235 Copyright 2016 William Belle.
236  
237 This program is distributed under the MIT (X11) License:
238 L<http://www.opensource.org/licenses/mit-license.php>
239  
240 Permission is hereby granted, free of charge, to any person
241 obtaining a copy of this software and associated documentation
242 files (the "Software"), to deal in the Software without
243 restriction, including without limitation the rights to use,
244 copy, modify, merge, publish, distribute, sublicense, and/or sell
245 copies of the Software, and to permit persons to whom the
246 Software is furnished to do so, subject to the following
247 conditions:
248  
249 The above copyright notice and this permission notice shall be
250 included in all copies or substantial portions of the Software.
251  
252 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
253 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
254 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
255 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
256 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
257 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
258 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
259 OTHER DEALINGS IN THE SOFTWARE.
260  
261 =cut
262  
263 5 1; # End of HON::Http::UrlChecker::Service
↑ Summary