line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package URL::Check; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$URL::Check::VERSION = '0.11'; |
4
|
|
|
|
|
|
|
$URL::Check::AUTHORITY = 'cpan:ALEXMASS'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
URL::Check - Check a list of URL and respond accordingly. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.11 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
6
|
|
|
6
|
|
54349
|
use 5.006; |
|
6
|
|
|
|
|
13
|
|
17
|
6
|
|
|
6
|
|
18
|
use strict; use warnings; |
|
6
|
|
|
6
|
|
7
|
|
|
6
|
|
|
|
|
92
|
|
|
6
|
|
|
|
|
17
|
|
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
161
|
|
18
|
|
|
|
|
|
|
|
19
|
6
|
|
|
6
|
|
2240
|
use LWP::Simple qw/get/; |
|
6
|
|
|
|
|
269644
|
|
|
6
|
|
|
|
|
41
|
|
20
|
6
|
|
|
6
|
|
3354
|
use Time::HiRes qw /gettimeofday/; |
|
6
|
|
|
|
|
5644
|
|
|
6
|
|
|
|
|
19
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
This module is not aimed at being used directly but via the script C. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
url-check --config=my-config.txt |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
If no C<--config argument> is set, the value is taken from environment variable C. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
More examples can be found in the C directory but consist |
31
|
|
|
|
|
|
|
in default parameter (mailto etc., then each url to be tested can be followed |
32
|
|
|
|
|
|
|
by dedicated test (time.delay, xpath etc.) |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
More info can be found L |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 readConfig($file_name) |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Read the config file. Default file name is taken from C<$URL_CHECK_FILE> |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
our %config; |
45
|
|
|
|
|
|
|
our @report; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub readConfig { |
48
|
|
|
|
|
|
|
my $configFile = shift || $ENV{URL_CHECK_CONFIG} |
49
|
6
|
|
50
|
6
|
1
|
2648
|
|| die "ERROR: No config file is passed or env URL_CHECK_CONFIG is set.\n"; |
50
|
|
|
|
|
|
|
|
51
|
5
|
|
|
|
|
7
|
my $FD; |
52
|
5
|
100
|
|
|
|
147
|
open ($FD, "<$configFile") |
53
|
|
|
|
|
|
|
|| die "ERROR: Cannot open config file [$configFile]: $!\n"; |
54
|
|
|
|
|
|
|
|
55
|
4
|
|
|
|
|
12
|
p_clearConfig(); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# $currenturl will contain at first the default map for handling error, |
58
|
|
|
|
|
|
|
# then, when a url is encountered, it will contain the url's error handlers |
59
|
4
|
|
|
|
|
7
|
my $currentUrl; |
60
|
4
|
|
|
|
|
57
|
while (my $line = <$FD>) { |
61
|
39
|
|
|
|
|
49
|
$line=~s/^#.*//; # remove comments |
62
|
39
|
|
|
|
|
117
|
$line=~s/\s*$//; # end of line spaces |
63
|
39
|
100
|
|
|
|
90
|
next unless $line =~/\S/; # skip emplty lines |
64
|
|
|
|
|
|
|
|
65
|
23
|
100
|
|
|
|
37
|
if ($line=~/^onerror/i) { |
66
|
4
|
|
33
|
|
|
21
|
p_addOnErrorLine($line, $currentUrl || $config{default}); |
67
|
4
|
|
|
|
|
11
|
next; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
19
|
100
|
|
|
|
32
|
if ($line=~/^check/i) { |
71
|
5
|
|
|
|
|
7
|
p_addCheckLine($line, $currentUrl); |
72
|
5
|
|
|
|
|
13
|
next; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
14
|
50
|
|
|
|
37
|
if ($line=~/^(ftp|http|file):\/\//i) { |
76
|
14
|
|
|
|
|
19
|
$currentUrl = p_addUrl($line); |
77
|
14
|
|
|
|
|
38
|
next; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
die "ERROR: Cannot parse line: $line\n"; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
4
|
|
|
|
|
32
|
close($FD); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 run() |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Run the configured tests, and store the result into the local @report |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub run { |
93
|
|
|
|
|
|
|
|
94
|
3
|
|
|
3
|
1
|
559
|
undef @report; |
95
|
3
|
|
|
|
|
4
|
foreach my $urlConfig (@{$config{urls}}) { |
|
3
|
|
|
|
|
9
|
|
96
|
10
|
|
|
|
|
33
|
push @report, p_runOneUrl($urlConfig); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 submitReport(%report) |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Print on console or send by mail the error output |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub submitReport { |
107
|
2
|
|
|
2
|
1
|
2974
|
my %report = @_; |
108
|
|
|
|
|
|
|
|
109
|
2
|
50
|
|
|
|
15
|
if ($config{default}{onError}{console}) { |
110
|
2
|
|
|
|
|
62
|
print "ERROR REPORT: $report{subject}\n$report{contents}\n"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
2
|
50
|
|
|
|
19
|
if ($config{default}{onError}{mailto}) { |
114
|
0
|
|
|
|
|
0
|
require Mail::Sendmail; |
115
|
0
|
|
|
|
|
0
|
my $hostname = `hostname`; |
116
|
0
|
|
|
|
|
0
|
chomp $hostname; |
117
|
|
|
|
|
|
|
my %mail = ( |
118
|
0
|
|
|
|
|
0
|
To => join(',', @{$config{default}{onError}{mailto}}), |
|
0
|
|
|
|
|
0
|
|
119
|
|
|
|
|
|
|
Subject => "[url-check] $report{subject}", |
120
|
|
|
|
|
|
|
From => "url.check\@$hostname", |
121
|
|
|
|
|
|
|
Message => "$report{contents}\n", |
122
|
|
|
|
|
|
|
); |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
0
|
warn "sending error report by mail: $report{subject}\n"; |
125
|
0
|
0
|
|
|
|
0
|
Mail::Sendmail::sendmail(%mail) or die $Mail::Sendmail::error |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 errorReport() |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Build a map (subject => ..., content=> ...) with the errors after the run |
132
|
|
|
|
|
|
|
return () if no error were detected |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub errorReport { |
137
|
3
|
|
|
3
|
1
|
19
|
my @errors = grep {! $_->{success}} @report; |
|
10
|
|
|
|
|
22
|
|
138
|
|
|
|
|
|
|
|
139
|
3
|
100
|
|
|
|
18
|
unless (@errors) { |
140
|
1
|
|
|
|
|
5
|
return (); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
( |
144
|
|
|
|
|
|
|
subject => ''.scalar(@errors).' errors reported', |
145
|
2
|
|
|
|
|
12
|
contents => join("\n", (map {$_->{url}." : ".$_->{message}} @errors)) |
|
4
|
|
|
|
|
23
|
|
146
|
|
|
|
|
|
|
); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# |
150
|
|
|
|
|
|
|
# |
151
|
|
|
|
|
|
|
# PRIVATE METHODS |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub p_addOnErrorLine { |
154
|
11
|
|
|
11
|
0
|
1557
|
my ($line, $conf) = @_; |
155
|
|
|
|
|
|
|
|
156
|
11
|
50
|
|
|
|
56
|
die "ERROR: Cannot parse error line: $line" |
157
|
|
|
|
|
|
|
unless $line=~/^onerror\.(.+?)=(.+)/i; |
158
|
|
|
|
|
|
|
|
159
|
11
|
|
|
|
|
29
|
my ($errorCat, $params) = ($1, $2); |
160
|
|
|
|
|
|
|
|
161
|
11
|
100
|
|
|
|
25
|
if ($errorCat eq 'mailto') { |
162
|
2
|
|
|
|
|
8
|
my @tmp = split(/,/, $params); |
163
|
2
|
|
|
|
|
4
|
$conf->{onError}{mailto}=\@tmp; |
164
|
2
|
|
|
|
|
3
|
return; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
9
|
100
|
|
|
|
19
|
if ($errorCat eq 'console') { |
168
|
8
|
|
|
|
|
39
|
$conf->{onError}{console}= $params =~ /\s*(y(es)?|t(rue)?|1)\s*$/i; |
169
|
8
|
|
|
|
|
14
|
return; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
1
|
|
|
|
|
5
|
die "ERROR: Unknown onerror type [$errorCat]\n"; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub p_addCheckLine { |
176
|
5
|
|
|
5
|
0
|
4
|
my ($line, $conf) = @_; |
177
|
|
|
|
|
|
|
|
178
|
5
|
50
|
|
|
|
12
|
die "ERROR: Cannot parse error line: $line\n" |
179
|
|
|
|
|
|
|
unless $line=~/^check\.(.+?)=(.+)/i; |
180
|
|
|
|
|
|
|
|
181
|
5
|
|
|
|
|
8
|
my ($cat, $params) = ($1, $2); |
182
|
|
|
|
|
|
|
|
183
|
5
|
100
|
|
|
|
7
|
if ($cat eq 'contains') { |
184
|
3
|
|
|
|
|
3
|
push @{ $conf->{check}{contains}}, $params ; |
|
3
|
|
|
|
|
6
|
|
185
|
3
|
|
|
|
|
16
|
return; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
2
|
|
|
|
|
3
|
$conf->{check}{$cat} = $params |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub p_addUrl { |
192
|
14
|
|
|
14
|
0
|
14
|
my ($line) = @_; |
193
|
|
|
|
|
|
|
|
194
|
14
|
|
|
|
|
28
|
my $h = { |
195
|
|
|
|
|
|
|
url => $line, |
196
|
|
|
|
|
|
|
check => {} |
197
|
|
|
|
|
|
|
}; |
198
|
|
|
|
|
|
|
|
199
|
14
|
|
|
|
|
12
|
push @{$config{urls}}, $h; |
|
14
|
|
|
|
|
19
|
|
200
|
14
|
|
|
|
|
14
|
return $h; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub p_clearConfig { |
204
|
|
|
|
|
|
|
|
205
|
4
|
|
|
4
|
0
|
21
|
%config = ( |
206
|
|
|
|
|
|
|
default => { onError=>{} }, |
207
|
|
|
|
|
|
|
urls => [], |
208
|
|
|
|
|
|
|
); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub p_runOneUrl { |
212
|
10
|
|
|
10
|
0
|
14
|
my %urlConfig = %{$_[0]}; |
|
10
|
|
|
|
|
814
|
|
213
|
|
|
|
|
|
|
|
214
|
10
|
|
|
|
|
22
|
my $url = $urlConfig{url}; |
215
|
10
|
|
|
|
|
39
|
my ($sec0, $micros0) = gettimeofday; |
216
|
10
|
|
|
|
|
45
|
my $content = get($url); |
217
|
|
|
|
|
|
|
|
218
|
10
|
100
|
|
|
|
1753865
|
unless ($content) { |
219
|
|
|
|
|
|
|
return { |
220
|
2
|
|
|
|
|
26
|
url => $url, |
221
|
|
|
|
|
|
|
success => 0, |
222
|
|
|
|
|
|
|
message => 'cannot load content' |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
8
|
|
|
|
|
52
|
my ($sec1, $micros1) = gettimeofday; |
227
|
8
|
|
|
|
|
55
|
my $dtime = int(($sec1-$sec0)*1000 + ($micros1-$micros0)/1000); |
228
|
|
|
|
|
|
|
|
229
|
8
|
100
|
100
|
|
|
55
|
if ((exists $urlConfig{check}{overtime}) && ($urlConfig{check}{overtime} < $dtime)) { |
230
|
|
|
|
|
|
|
return { |
231
|
1
|
|
|
|
|
19
|
url => $url, |
232
|
|
|
|
|
|
|
success => 0, |
233
|
|
|
|
|
|
|
message => "overtime > $urlConfig{check}{overtime} (${dtime}ms)", |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
|
237
|
7
|
100
|
|
|
|
25
|
if (exists $urlConfig{check}{contains}) { |
238
|
2
|
|
|
|
|
3
|
my @searchFor = @{$urlConfig{check}{contains}}; |
|
2
|
|
|
|
|
9
|
|
239
|
2
|
|
|
|
|
5
|
foreach (@searchFor) { |
240
|
2
|
100
|
|
|
|
17
|
if (index($content, $_) < 0) { |
241
|
|
|
|
|
|
|
return { |
242
|
1
|
|
|
|
|
10
|
url => $url, |
243
|
|
|
|
|
|
|
success => 0, |
244
|
|
|
|
|
|
|
message => "does not contains \"$_\"", |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
return { |
251
|
6
|
|
|
|
|
48
|
url => $url, |
252
|
|
|
|
|
|
|
success => 1 |
253
|
|
|
|
|
|
|
}; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head1 AUTHOR |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Alexandre Masselot, C<< >> |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Currently maintained by Mohammad S Anwar (MANWAR) C<< >> |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head1 REPOSITORY |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
L |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head1 BUGS |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, |
269
|
|
|
|
|
|
|
or through the web interface at L. |
270
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
271
|
|
|
|
|
|
|
your bug as I make changes. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 SUPPORT |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
perldoc URL::Check |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
You can also look for information at: |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=over 4 |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
L |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
L |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item * CPAN Ratings |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
L |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item * Search CPAN |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
L |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=back |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
Copyright (C) 2011 - 2016 Alexandre Masselot. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under the |
306
|
|
|
|
|
|
|
terms of either: the GNU General Public License as published by the Free Software |
307
|
|
|
|
|
|
|
Foundation; or the Artistic License. |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=cut |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
1; # End of URL::Check |