Link/Tester/Complex.pm

filter: Statementstmt 52.4% 149 / 284 Branchbran 22.3% 38 / 170 Conditioncond 22.2% 4 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 90.0% 18 / 20 Podpod 0.0% 0 / 7 totaltotal 41.1% 210 / 511 CC 92 SCAR 60.4
CC
92
Cov
40%
telnet_http_retry
1122.0
follow_url
224.6
get_location_header
72.0
CRAP 418.6
↑ Summary Link/Selector.pm »
1 package WWW::Link::Tester::Complex;
2 3 $REVISION=q$Revision: 1.8 $ ; $VERSION = sprintf ( "%d.%02d", $REVISION =~ /(\d+).(\d+)/ );
3  
4 3 ▶use Carp qw(carp cluck croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 WWW::Link::Tester::Complex - a careful tester for broken links
9  
10 =head1 SYNOPSIS
11  
12 use WWW::Link::Test::Complex
13 $ua=create_a_user_agent();
14 $link=get_a_link_object();
15 WWW::Link::Test::Complex::test_link($ua, $link);
16 WWW::Link::Tester::Simple::Test($url)
17  
18  
19 =head1 DESCRIPTION
20  
21 This is a link testing module based on the work of Phil Mitchell at
22 Harvard College. The aim is to test very carefully if a link is
23 really there.
24  
25 N.B. I have done the minimum reasonable edits on the file so that any
26 later improvements can be easily added. This means that the module
27 contains and sections of code which are not relevant to
28 LinkController.
29  
30 =head1 ROBOT LOGIC
31  
32 This system should be controlled by the robot logic of the user agent it
33 uses provided that the robot returns a 4xx response code.
34  
35 =head1 AUTHOR
36  
37 Copyright (c) 2000 by the President and Fellows of Harvard College
38  
39 This program is free software; you can redistribute it and/or modify
40 it under the terms of the GNU General Public License as published by
41 the Free Software Foundation; either version 2 of the License, or (at
42 your option) any later version.
43  
44 Please see the source code for further details
45  
46 =cut
47  
48 ############################################################################
49 #
50 # Copyright (c) 2000 by the President and Fellows of Harvard College
51 #
52 # This program is free software; you can redistribute it and/or modify
53 # it under the terms of the GNU General Public License as published by
54 # the Free Software Foundation; either version 2 of the License, or (at
55 # your option) any later version.
56 #
57 # This program is distributed in the hope that it will be useful, but
58 # WITHOUT ANY WARRANTY; without even the implied warranty of
59 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60 # General Public License for more details.
61 #
62 # You should have received a copy of the GNU General Public License
63 # along with this program; if not, write to the Free Software
64 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
65 # USA.
66 #
67 # Contact information:
68 #
69 # Phil Mitchell
70 # Office for Information Systems
71 # Harvard University
72 # philip_mitchell at harvard.edu
73 #
74 #############################################################################
75 #
76 # When called without args, this script reads a list of URLs, one per line,
77 # from $INPUT_FILE, extracts the url from each record, and tries to access
78 # the url using the appropriate protocol. This includes following redirects
79 # until either:
80 # 1. the target page is successfully received; or
81 # 2. a page cycle is detected; or
82 # 3. a bad server or page request is detected; or
83 # 4. a maximum number of redirects ($MAX_REDIRECTS) is exceeded.
84 #
85 #...deleted...
86 #
87 # Protocols supported: http, https, ftp, gopher, file, telnet.
88 #
89 # Status codes:
90 # Success: All successful response codes have the form:
91 # 2xx. Because we limit the size of responses we accept, we get a
92 # lot of 206's in addition to 200's.
93 # UNSUPPORTED_PROTOCOL:
94 # Linkcheck handles {http, https, ftp, gopher, file, telnet}. Other
95 # protocols will get this error. More commonly, it is the result of a
96 # typo (eg. "thttp://").
97 # MALFORMED_URL: The url is syntactically incorrect. EG.,
98 # "http:/www.domain.com".
99 # TELNET_FAILURE: Couldn't open the requested telnet connection.
100 # HTTP_0_9_FAIL: Failed HTTP/0.9 connection (0.9 does not return
101 # status codes).
102 # REDIRECT_LIMIT_EXCEEDED:
103 # Too many redirections. This error code should not normally be
104 # received, it is in place to catch infinite redirect cycles.
105 # UNKNOWN_ERROR: Rarely, LWP or HTTP modules will die, reporting an
106 # error that is not useful to us. This error code should
107 # not normally be received; it
108 # will generally be corrected in subsequent passes.
109 #
110 # There are various configurable parameters documented below. In
111 # addition to setting the input and output filenames, the most
112 # important ones are those that control the timeout, the number of
113 # retries, and the time between retries. These settings have an
114 # important effect on the accuracy of results.
115 #
116 # Accuracy of results:
117 #
118 # Informal tests (results can be found at the end of this script) have
119 # shown that: (1) a timeout of 30 sec is adequate; increasing to 60
120 # sec is not useful; 10 seconds is too short. (2) The absolute number
121 # of recheck passes is less important than spreading them over
122 # time. Reasonable results are obtained with 3 recheck passes, each
123 # separated by 8 hours of sleep.
124 #
125 # In our set of about 10,000 urls, a first pass produces about 800
126 # (8%) bad urls. Subsequent passes will reduce that to about 650
127 # (6.5%). The use of telnet retry will reach another 25% of those
128 # apparently bad urls. The estimate of total bad urls in our sample is
129 # thus 4.5%. That list of bad urls is consistent across distinct runs
130 # of the link checker at greater than 99%. Handchecking of a large
131 # sample from this final list indicates a high degree of accuracy.
132 #
133 # Notes:
134 #
135 # - A "page cycle" is the use of a redirect or refresh tag to cycle through
136 # a list of one or more pages for data refresh purposes.
137 #
138 # Design Notes:
139 #
140 # - Cookies: This version accepts all cookies. This allows it to handle some
141 # URLs which require cookies.
142 #
143 # - Timeout bug: Due to an apparent bug in the interaction between
144 # Solaris and certain web servers, some http responses come back
145 # improperly terminated. As a result, LWP times out and reports a
146 # server error when a (nearly) valid response has been received. To
147 # avoid this, we open a telnet connection to the relevant port
148 # (usually 80) and do a manual GET on the url. Telnet will also time
149 # out in this case, but telnet.pm provides a dump of the partial
150 # response received, and we use this.
151 #
152 # - WWW unreliability: Any given access to a server on the web is
153 # subject to various kinds of flakiness. To avoid false reports of
154 # bad servers, it is essential to re-test all errors, preferably over
155 # a period of hours or days. This script completes a first pass
156 # through all urls, typically taking 8 hours or more on 10,000
157 # urls. Then it performs additional ($RECHECKS) passes on all urls
158 # that received error codes. It sleeps ($HOURS_TO_SLEEP) between
159 # passes to improve the chances of getting a valid return code.
160 #
161 # - Redirects and cycles: The challenge is to follow redirects all
162 # the way to the end of the line, but know when to stop. It is
163 # complicated by the fact that some sites use the meta refresh tag
164 # for their redirection, and by the fact that some sites have
165 # infinite loop cycles for page refresh purposes. Five distinct cases
166 # have been identified:
167 #
168 # 1. Proper redirect, using Location header. (Action: Follow redirect.)
169 # 2. Proper meta refresh, on a single page. (Action: Detect cycle
170 # and exit.)
171 # 3. Proper meta refresh, on a cycle of pages. (Action:Detect
172 # cycle and exit.)
173 # 4. Redirect using meta refresh. (Action: Follow redirect.)
174 # 5. Redirect loop on a single page for setting cookies. (Action:
175 # Follow redirect.)
176 #
177 # Maintenance and Future Development Notes:
178 #
179 # - 401's and 403's: Currently does not handle authentication; just
180 # reports these as errors.
181 #
182 # - Cookie warnings: With perl's -w option, many warnings will be
183 # received about Cookies.pm. This seems to be due to the fact that
184 # Cookies.pm does not cleanly handle incorrectly formatted
185 # cookies. As far as I know, these warnings may be safely ignored.
186  
187 # Author: Phil Mitchell
188 # Date: 02/22/01
189 # Version: 1.5
190 #
191 #############################################################################
192  
193 3 ▶use WWW::Link::Tester;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 3 @ISA="WWW::Link::Tester";
195  
196 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
198 3 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 3 ▶use HTTP::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 3 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
201 3 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
203 3 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 3 ▶use Net::Telnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 #use LWP::Debug qw(+);
206  
207 ###########################################
208 # Global variables
209 ###########################################
210  
211 3 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
212 %url_hash
213 $HTTP_DEFAULT_PORT
214 $HTTP_VERSION
215 $ADMIN_EMAIL
216 $MAX_REDIRECTS
217 $RECHECKS
218 $HOURS_TO_SLEEP
219 $AGENT_TIMEOUT
220 $AGENT_MAX_RESPONSE
221 $INPUT_FILE
222 $OUTPUT_FILE
223 $TMP_FILE
224 $TELNET_LOGFILE
225 $ADMIN_LOGFILE
226 $REDIRECT_LIMIT_EXCEEDED
227 $UNSUPPORTED_PROTOCOL
228 $MALFORMED_URL
229 $HTTP_0_9_OKAY
230 $HTTP_0_9_FAIL
231 $UNKNOWN_ERROR
232 $VERBOSE
233 $DEBUG
234 $LOGGING
235 $TELNET_SUCCESS
236 $TELNET_FAILURE
237 $agent
238 $telnetAgent
239 $cookieJar
240 $redirectCount
241 3 );
242  
243  
244  
245 ###########################################
246 # Configurable parameters
247 ###########################################
248  
249 3 $ADMIN_EMAIL = ''; # If non-empty, script will send confirmation and result stats.
250 3 $AGENT_TIMEOUT = 10; # In seconds, time for http agent to wait. 10 secs is often too
251 # short, leads to spurious reports of server errors. Longer than
252 # 30 secs not usually helpful.
253 3 $AGENT_MAX_RESPONSE = 524288; # In bytes, max response to accept. Mainly want to
254 # avoid being swamped by something huge.
255 3 $MAX_REDIRECTS = 15; # Number of redirects to tolerate before giving up. Should never hit
256 # this limit; it's here to avoid infinite loop.
257 3 $RECHECKS = 3; # Number of recheck passes to recheck urls that return error codes. Note
258 # that every server error automatically gets one retry via telnet.
259 3 $HOURS_TO_SLEEP = 0; # Number of hours to sleep between recheck passes.
260 3 $HTTP_DEFAULT_PORT = 80;
261 3 $HTTP_VERSION = 'HTTP/1.0'; # Perl's HTTP module defaults to 0.9
262 3 $INPUT_FILE = "CURRENT.URLS.TXT";
263 3 $INPUT_FILE = "smalltest.txt";
264 3 $OUTPUT_FILE = "OUT.URLS.TXT";
265 3 $ADMIN_LOGFILE = "admin_logfile.txt"; # Log for result stats.
266 3 $VERBOSE = 1; # If 1, print processing status to stdout
267 3 $DEBUG = 0; # If 1, provides additional output to stdout; mainly HTTP headers.
268 3 $LOGGING = 1; # Enable logging to $ADMIN_LOGFILE.
269  
270 ###########################################
271 # Misc. initializations
272 ###########################################
273  
274 3 $TMP_FILE = "tmp.txt";
275 3 $TELNET_LOGFILE = "telnet_logfile.txt"; # Used internally to buffer data.
276  
277 # Response codes. All successful response codes have the form: 2xx.
278 3 $REDIRECT_LIMIT_EXCEEDED = 'REDIRECT_LIMIT_EXCEEDED';
279 3 $UNSUPPORTED_PROTOCOL = 'UNSUPPORTED_PROTOCOL';
280 3 $MALFORMED_URL = 'MALFORMED_URL';
281 3 $TELNET_FAILURE = 'TELNET_FAILURE';
282 3 $HTTP_0_9_FAIL = 'HTTP_0_9_FAIL';
283 3 $UNKNOWN_ERROR = 'UNKNOWN_ERROR';
284 3 $TELNET_SUCCESS = 299; # Mimic a successful HTTP code
285 3 $HTTP_0_9_OKAY = 298;
286  
287 =head1 test_link
288  
289 This function acts as glue between follow_url and LinkController. It
290 returns a constructed HTTP::Response. This will mean that information
291 is lost since we actually often have created the code from another
292 response.
293  
294 =cut
295  
296 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
297 4 my $proto = shift;
298 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
299 4 my $self = {};
300 4 $self->{"user_agent"}=shift;
301 4 bless $self, $class;
302 }
303  
304 3 ▶use vars qw($redirect_count $redirects %convert);
Subroutine BEGIN: called CC=1 SCAR=0.0
305  
306 3 %convert=(
307 $REDIRECT_LIMIT_EXCEEDED => RC_REDIRECT_LIMIT_EXCEEDED,
308 $UNSUPPORTED_PROTOCOL => RC_PROTOCOL_UNSUPPORTED,
309 $MALFORMED_URL => RC_PROTOCOL_UNSUPPORTED,
310 $TELNET_FAILURE => RC_NOT_FOUND,
311 $HTTP_0_9_FAIL => RC_INTERNAL_SERVER_ERROR,
312 $UNKNOWN_ERROR => RC_BAD_REQUEST,
313 );
314  
315  
316 14 ▶sub get_response {
Subroutine get_response: called CC=6 SCAR=18.0
Pod undocumented
317 14 my $self=shift;
318 14 my $link=shift;
319 14 $redirects=[];
320 14 $redirect_count=0;
321 14 %url_hash=();
322 14 my $code=$self->follow_url($link->url());
323 14 scalar (keys %convert);
324 14 ▶ CONVERT: while (my ($key,$value) = each %convert) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %WWW::Link::Tester::Complex::convert) 77 10 87
325 77 ▶ $code eq $key && do {
Branch: 1 branch
BranchTrueFalseTotal
$code eq $key and do { $code = $value; last CONVERT } 4 73 77
326 4 $code=$value;
327 4 last CONVERT;
328 };
329 }
330 print STDERR "COMPLEX generated response code $code\n"
331 14 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 14 14
332 #cluck and die here generate coredumps!!!???! in perl 5.6.0 on Linux
333 # cluck STDERR "COMPLEX generated response code $code";
334 14 ▶ die "non numeric response code generated" . $code
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /[1-9][0-9]+/ 0 14 14
335 unless $code =~ m/[1-9][0-9]+/;
336 14 my $response=HTTP::Response->new($code);
337  
338 14 ▶ die "response: $response not reference" unless ref $response ;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response 0 14 14
339  
340 14 return $response, @$redirects
341 }
342  
343 # Set up the web agents and helpers.
344 # $agent = new LWP::UserAgent;
345 # $agent->timeout($AGENT_TIMEOUT);
346 # $agent->max_size($AGENT_MAX_RESPONSE);
347 3 $cookieJar = new HTTP::Cookies;
348 3 $telnetAgent = new Net::Telnet(Timeout => $AGENT_TIMEOUT,
349 Errmode => 'return');
350  
351 3 my ($url, $result, $newResult, %results, $outputStr, $urlCount,
352 $count, $recheckCount, %resultSummary);
353  
354 ###########################################
355 # check_for_meta_refresh
356 ###########################################
357 # Routine that searches input string for something of the form:
358 # <meta http-equiv="refresh" [verbiage] url="http://some.url.com">
359 # It is tolerant of extra whitespace, single or no quotes instead of
360 # doublequotes, spaces around equals signs, and extra verbiage, and is
361 # case-insensitive.
362 # Call with: String of content to be searched
363 # Returns: url, if a meta refresh is found; otherwise returns
364 # empty string.
365  
366 8 ▶sub check_for_meta_refresh {
Subroutine check_for_meta_refresh: called CC=3 SCAR=12.7
Pod undocumented
367 0 ▶ if ($DEBUG) { print "check_for_meta_refresh()...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 8 8
368 8 my $inputStr = shift;
369 8 ▶ if ($inputStr =~
Branch: 1 branch
BranchTrueFalseTotal
if ($inputStr =~ / #" <meta \s+ http-equiv \s* = \s* ["']? refresh ["']? [^>]+? url \s* = \s* ["']? ([^"' >]+) ["']? [^>]+? > /xi) { } 0 8 8
370 m{ #"
371 <meta \s+ http-equiv \s* = \s* ["']? refresh ["']? [^>]+? url
372 \s* = \s* ["']? ([^"' >]+) ["']? [^>]+? >
373 }ix)
374 {
375 0 return $1;
376 }
377 else {
378 8 return "";
379 }
380 }#end check_for_meta_refresh
381  
382 ###########################################
383 # follow_url
384 ###########################################
385  
386 # Tries to access a given url. The main case is HTTP protocol, but
387 # also handles any protocol handled by LWP, plus telnet. For telnet,
388 # just tries to open a connection. For HTTP, follows redirects until
389 # a final status code is received or until $MAX_REDIRECTS is
390 # exceeded. Accepts all cookies. To avoid infinite loops, detects page
391 # refresh cycles.
392  
393 # Call with: url, and optional second arg of referring url which is
394 # used to absolutize url.
395  
396 # Returns: HTTP status code, or internal response codes (see above).
397  
398 30 ▶sub follow_url {
Subroutine follow_url: called CC=37 SCAR=54.1
Pod undocumented
399 30 my $self=shift;
400 30 my $agent=$self->{"user_agent"};
401 30 my ($url, $referrer) = @_;
402 30 my $VERBOSE=$self->{"verbose"};
403  
404 30 ▶ return $MALFORMED_URL unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 30 30
405 30 my ($response, $protocol, $host, $port, $ping, $telnetResult,
406 $request, $statusCode, $new_url);
407 0 ▶ if ($VERBOSE || $DEBUG) { print "follow_url(): $url\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE or $WWW::Link::Tester::Complex::DEBUG) 0 30 30
Condition33%1/3
$VERBOSE or $WWW::Link::Tester::Complex::DEBUG
l!l&&r!l&&!r
0030
MC/DC0%0/2
$VERBOSE or $WWW::Link::Tester::Complex::DEBUG
$VERBOSE $WWW::Link::Tester::Complex::DEBUG
Truth table33%1/3
A or B
A= $VERBOSEB= $WWW::Link::Tester::Complex::DEBUG
ABresult
1X1
011
000
408 30 $url_hash{$url} = 1; # Track all urls in each run, to detect cycles.
409  
410 # Note: It is crucial to hash this url BEFORE absolutizing it, b/c
411 # we will test for cycles before absolutizing.
412  
413 30 ▶ if ($referrer) { $url = make_url_absolute($url, $referrer); }
Branch: 1 branch
BranchTrueFalseTotal
if ($referrer) 16 14 30
414 30 ▶ if (keys(%url_hash) > $MAX_REDIRECTS) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %WWW::Link::Tester::Complex::url_hash > $WWW::Link::Tester::Complex::MAX_REDIRECTS) 1 29 30
415 0 ▶ if ($VERBOSE) { print "Redirect limit exceeded.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 1 1
416 1 return $REDIRECT_LIMIT_EXCEEDED;
417 }
418  
419 # EXTRACT PROTOCOL, HOST, AND (OPTIONAL) PORT.
420 29 $url =~ m{ ^\s* ([a-z]+) :// ([^/:]+) }ix;
421 29 ▶ if (!($1 && $2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $1 && $2) { } 3 26 29
Condition66%2/3
$1 && $2
!ll&&!rl&&r
3026
MC/DC50%1/2
$1 && $2
$1 $2
Truth table66%2/3
A && B
A= $1B= $2
ABresult
0X0
100
111
422 0 ▶ if ($VERBOSE) { print "URL not well-formed.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 3 3
423 3 return $MALFORMED_URL;
424 }
425 else {
426 26 $protocol = $1;
427 26 $host = $2;
428 }
429 26 $url =~ m{ \w+ :// [^/]+ : (\d+) }x; # Extract port
430 26 ▶ if ($1) { $port = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 26 0 26
431  
432 # HANDLE TELNET REQUESTS -- just see if we can open the connection.
433 26 ▶ if ($protocol =~ /^telnet$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol =~ /^telnet$/i) 0 26 26
434 0 ▶ if ($port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) { } 0 0 0
435 0 $ping = $telnetAgent->open(Host => $host,
436 Port => $port);
437 }
438 else {
439 0 $ping = $telnetAgent->open(Host => $host);
440 }
441 0 ▶ if (!$ping) { return $TELNET_FAILURE; }
Branch: 1 branch
BranchTrueFalseTotal
if (not $ping) { } 0 0 0
442 0 else { return $TELNET_SUCCESS; }
443 }
444  
445 # HANDLE ALL OTHER REQUESTS (HTTP, HTTPS, FTP, GOPHER, FILE)
446 26 ▶ if (!$agent->is_protocol_supported($protocol)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($agent->is_protocol_supported($protocol)) 0 26 26
447 0 ▶ if ($VERBOSE) { print "Protocol not supported.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 0 0
448 0 return $UNSUPPORTED_PROTOCOL;
449 }
450 # Use eval to avoid aborting if LWP or HTTP sends "die".
451 26 eval {
452 26 $request = HTTP::Request->new(GET => $url);
453 26 $request->protocol($HTTP_VERSION);
454 26 $cookieJar->add_cookie_header($request);
455 0 ▶ if ($DEBUG) { print "\nRequest: \n", $request->as_string; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 26 26
456  
457 # Use simple_request so we don't follow redirects automatically
458 26 $response = $agent->simple_request($request);
459 26 $cookieJar->extract_cookies($response);
460 26 $statusCode = $response->code;
461 };
462 26 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 26 26
463 0 ▶ if ($VERBOSE) { print "LWP or HTTP error: $@\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 0 0
464 0 ▶ if ($LOGGING) { print STDERR "LWP or HTTP error: $@\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::LOGGING) 0 0 0
465 0 return $UNKNOWN_ERROR;
466 }
467 0 ▶ if ($DEBUG) { print "Status: $statusCode\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 26 26
468 0 ▶ if ($DEBUG) { print "\nResponse Header: \n", $response->headers->as_string; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 26 26
469  
470 # Note: In case of timeout, agent sets $statusCode to server error.
471 26 ▶ if ($statusCode =~ /2../) {
Branch: 4 branches
BranchTrueFalseTotal
if ($statusCode =~ /2../) { } 8 18 26
elsif ($statusCode =~ /3../) { } 16 2 18
elsif ($statusCode =~ /4../) { } 2 0 2
elsif ($statusCode =~ /5../) { } 0 0 0
472 0 ▶ if ($VERBOSE) { print "Good response, checking for meta refresh tag...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 8 8
473 8 $new_url = check_for_meta_refresh($response->content);
474 8 ▶ if ($new_url ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_url ne '') { } 0 8 8
475 0 ▶ if (exists($url_hash{$new_url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $WWW::Link::Tester::Complex::url_hash{$new_url}) { } 0 0 0
476 0 ▶ if ($VERBOSE) { print "This url already visited ... returning $statusCode.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 0 0
477 0 return $statusCode; }
478 else {
479 0 ▶ if ($VERBOSE) { print "Refresh to: $new_url\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 0 0
480 0 return $self->follow_url($new_url, $url);
481 }
482 }
483 8 else { return $statusCode;}
484 }
485 elsif ($statusCode =~ /3../) {
486 16 $redirect_count++;
487 0 ▶ if ($VERBOSE) { print "Proper redirect...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 16 16
488 # Note that we don't check for page cycles here. Some sites
489 # will redirect to the same page while setting cookies, but
490 # eventually they'll stop.
491 16 $new_url = $response->headers->header('Location');
492 16 push @$redirects, $new_url;
493 0 ▶ if ($VERBOSE) { print "Redirect to: $new_url\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 16 16
494 16 return $self->follow_url($new_url, $url);
495 }
496 elsif ($statusCode =~ /4../) {
497 0 ▶ if ($VERBOSE) { print "Client error...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 2 2
498 2 return $statusCode;
499 }
500 elsif ($statusCode =~ /5../) {
501 0 ▶ if ($VERBOSE) { print "Server error...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE) 0 0 0
502  
503 # You might be tempted to do a retry right here. It is problematic
504 # b/c you need to do another follow_url, but that will clash with
505 # url_hash -- it will look like a page cycle. But if you do the
506 # retry by hand w/ a simple request, you don't handle all the
507 # cases properly. What we do is retry once using telnet, and leave
508 # other retries to subsequent passes following main loop.
509  
510 0 ▶ if ($protocol =~ /^http$/i) { # Only works for HTTP requests.
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol =~ /^http$/i) 0 0 0
511 0 $telnetResult =
512 $self->telnet_http_retry($host, $url, $request, $port);
513 0 ▶ if ($telnetResult ne 'FAIL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($telnetResult ne "FAIL") 0 0 0
514 0 $statusCode = $telnetResult;
515 }
516 }
517 0 return $statusCode;
518 } # end 5xx case.
519 else { # Everything else case.
520 0 return $statusCode;
521 }
522  
523 } # end sub follow_url
524  
525 ###########################################
526 # get_location_header
527 ###########################################
528 # Extracts the url from the Location field of an HTTP redirect.
529 # Call with: ref to array of header lines, w or w/o body at end.
530 # Returns: URL found in Location header, or empty string.
531 0 ▶sub get_location_header {
Subroutine get_location_header: not called CC=8 SCAR=42.8
Pod undocumented
532  
533 0 ▶ if ($VERBOSE || $DEBUG) { print "Looking for location header... \n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE or $WWW::Link::Tester::Complex::DEBUG) 0 0 0
Condition0%0/3
$WWW::Link::Tester::Complex::VERBOSE or $WWW::Link::Tester::Complex::DEBUG
l!l&&r!l&&!r
000
MC/DC0%0/2
$WWW::Link::Tester::Complex::VERBOSE or $WWW::Link::Tester::Complex::DEBUG
$WWW::Link::Tester::Complex::VERBOSE $WWW::Link::Tester::Complex::DEBUG
Truth table0%0/3
A or B
A= $WWW::Link::Tester::Complex::VERBOSEB= $WWW::Link::Tester::Complex::DEBUG
ABresult
1X1
011
000
534 0 my ($headersRef) = @_;
535 0 my $line;
536  
537 0 ▶ while ($line = shift @$headersRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = shift @$headersRef) 0 0 0
538 0 ▶ if ($DEBUG) { print "Checking line: $line\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 0 0
539 0 ▶ last if $line =~ /^\s$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s$/ 0 0 0
540 0 ▶ if ($line =~ m{^Location: \s* (\S+)}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Location: \s* (\S+)/x) 0 0 0
541 0 ▶ if ($DEBUG) { print "Line found: $line\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 0 0
542 0 return $1;
543 }
544 }
545 0 return "";
546  
547 } # end sub get_location_header
548  
549 ###########################################
550 # make_url_absolute
551 ###########################################
552 # Make a relative url absolute by appending it to path of old url.
553 # Call with: a fully qualified url as second arg, which will provide
554 # path info for relative url which is first arg.
555 # Returns: new absolute url
556 16 ▶sub make_url_absolute {
Subroutine make_url_absolute: called CC=9 SCAR=39.9
Pod undocumented
557  
558 0 ▶ if ($DEBUG) { print "make_url_absolute()...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 16 16
559 16 my ($new_url, $old_url) = @_;
560  
561 # Test to see if it's already absolute (starts w/ a syntactically correct scheme)
562 16 ▶ if ($new_url =~ m{^[a-z]+://}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_url =~ m[^[a-z]+://]i) 16 0 16
563 16 return $new_url;
564 }
565
566 0 ▶ if ($VERBOSE) { print "Adding path to relative url: $new_url\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
567 # Case 1: new url is relative to root; it starts with slash, and
568 # should be appended to raw domain name.
569 0 ▶ if ($new_url =~ m{^/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_url =~ m[^/]) { } 0 0 0
elsif ($old_url =~ m[ (\w+://\S+/) ]x) { } 0 0 0
570 0 $old_url =~ m{ (\w+ :// [^/]+) }x;
571 0 ▶ if ($VERBOSE) { print "Case 1: append to $1\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
572 0 return $1 . $new_url;
573 }
574 # For cases 2 & 3, assume new url is relative to current directory;
575 # Case 2: old url contains a trailing slash, eg. http://www.fib.com/bigfib/;
576 # may or may not contain trailing filename
577 elsif ($old_url =~ m{ (\w+://\S+/) }x ) {
578 0 ▶ if ($VERBOSE) { print "Case 2: append to $1\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
579 0 return $1 . $new_url;
580 }
581 # Case 3: old url has no trailing slash, eg. http://www.fab.net
582 else {
583 0 ▶ if ($VERBOSE) { print "Case 3: append to $old_url/\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
584 0 return "$old_url/$new_url";
585 }
586
587 } # End make_url_absolute
588  
589 ###########################################
590 # telnet_http_retry
591 ###########################################
592 # Open a telnet connection to a host and try an HTTP GET for an
593 # url. The response is processed according to status code similarly to
594 # follow_url, and calls follow_url to handle redirects. Uses an LWP
595 # request object b/c that's a convenient way to stick cookies into the
596 # request string.
597 # Note: Handles the Solaris/LWP bug (cf notes above) by reading the
598 # telnet.pm input_log if telnet times out.
599 # Call with: hostname, absolute url, LWP request object, and optional
600 # port (default is $HTTP_DEFAULT_PORT).
601 # Returns: status code, or 'FAIL' if can't make telnet connection.
602 0 ▶sub telnet_http_retry {
Subroutine telnet_http_retry: not called CC=33 SCAR=70.2
Pod undocumented
603 0 my $self=shift;
604 0 ▶ if ($VERBOSE || $DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE or $WWW::Link::Tester::Complex::DEBUG) 0 0 0
Condition0%0/3
$WWW::Link::Tester::Complex::VERBOSE or $WWW::Link::Tester::Complex::DEBUG
l!l&&r!l&&!r
000
MC/DC0%0/2
$WWW::Link::Tester::Complex::VERBOSE or $WWW::Link::Tester::Complex::DEBUG
$WWW::Link::Tester::Complex::VERBOSE $WWW::Link::Tester::Complex::DEBUG
Truth table0%0/3
A or B
A= $WWW::Link::Tester::Complex::VERBOSEB= $WWW::Link::Tester::Complex::DEBUG
ABresult
1X1
011
000
605 0 print "Telnet HTTP retry...\n";
606 }
607 0 my ($host, $url, $request, $port) = @_;
608 0 my ($telnetAgent, @lines, @buffer, $statusLine, $line, $logfileHandle,
609 $httpVersion, $statusCode, $message, $contentStr, $new_url);
610 0 ▶ open(LOGFILE, "+>$TELNET_LOGFILE") || warn "Can't open $TELNET_LOGFILE.\n";
Branch: 1 branch
BranchTrueFalseTotal
open LOGFILE, "+>$WWW::Link::Tester::Complex::TELNET_LOGFILE" or warn "Can't open $WWW::Link::Tester::Complex::TELNET_LOGFILE.\n" 0 0 0
611 0 ▶ if (!$port || $port !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($port and $port =~ /^\d+$/) 0 0 0
Condition0%0/3
$port and $port =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$port and $port =~ /^\d+$/
$port $port =~ /^\d+$/
Truth table0%0/3
A and B
A= $portB= $port =~ /^\d+$/
ABresult
0X0
100
111
612 0 $port = $HTTP_DEFAULT_PORT;
613 }
614 # Create agent and open connection.
615 0 $telnetAgent = Net::Telnet->new(Host => $host,
616 Port => $port,
617 Input_log => $TELNET_LOGFILE,
618 Timeout => $AGENT_TIMEOUT,
619 Errmode => "return");
620 0 ▶ return 'FAIL' unless $telnetAgent; # Can't open telnet connection.
Branch: 1 branch
BranchTrueFalseTotal
unless $telnetAgent 0 0 0
621 0 $telnetAgent->max_buffer_length($AGENT_MAX_RESPONSE);
622  
623 # Send the request.
624 0 $telnetAgent->print($request->as_string, "\n");
625 # Get the response as array of lines.
626 0 ▶ while (@buffer = $telnetAgent->getlines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@buffer = $telnetAgent->getlines) 0 0 0
627 0 push (@lines, @buffer);
628 }
629 0 ▶ if ($telnetAgent->timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($telnetAgent->timed_out) 0 0 0
630 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
631 0 print "Telnet http timed out. Using input log...\n";
632 }
633 0 undef @lines;
634 0 ▶ while (<LOGFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline LOGFILE)) 0 0 0
635 0 push (@lines, $_);
636 }
637 0 ▶ close LOGFILE or warn "Problem closing $TELNET_LOGFILE.\n";
Branch: 1 branch
BranchTrueFalseTotal
close LOGFILE or warn "Problem closing $WWW::Link::Tester::Complex::TELNET_LOGFILE.\n" 0 0 0
638 }
639 0 ▶ if (!@lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@lines) 0 0 0
640 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
641 0 print "No data received.\n";
642 }
643 0 return 'FAIL';
644 }
645 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::DEBUG) 0 0 0
646 0 print @lines,"\n";
647 }
648 0 $statusLine = shift @lines;
649 # We can only process status line and headers if the response is HTTP/1.0 or
650 # better. This regexp copied from LWP::Protocol::http.pm.
651 0 ▶ if ($statusLine =~ /^(HTTP\/\d+\.\d+)[ \t]+(\d+)[ \t]*([^\012]*)\012/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statusLine =~ m[^(HTTP/\d+\.\d+)[ \t]+(\d+)[ \t]*([^\012]*)\012]) { } 0 0 0
652 # HTTP/1.0 response or better
653 0 ($httpVersion, $statusCode, $message) = ($1, $2, $3);
654 0 chomp $message;
655 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
656 0 print "Status line: $httpVersion $statusCode $message \n\n";
657 }
658  
659 0 ▶ if ($statusCode =~ /2../) {
Branch: 3 branches
BranchTrueFalseTotal
if ($statusCode =~ /2../) { } 0 0 0
elsif ($statusCode =~ /3../) { } 0 0 0
elsif ($statusCode =~ /4.. | 5../x) { } 0 0 0
660 0 ▶ while ($line = shift @lines) { # Flatten array of lines.
Branch: 1 branch
BranchTrueFalseTotal
if ($line = shift @lines) 0 0 0
661 0 $contentStr .= $line;
662 }
663 0 $new_url = check_for_meta_refresh($contentStr);
664 0 ▶ if ($new_url ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_url ne '') { } 0 0 0
665 0 ▶ if (exists($url_hash{$new_url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $WWW::Link::Tester::Complex::url_hash{$new_url}) { } 0 0 0
666 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
667 0 print "This url already visited ... returning $statusCode.\n";
668 }
669 0 return $statusCode;
670 } else {
671 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
672 0 print "Refresh to: $new_url\n";
673 }
674 # Return whatever status code we get from new url
675 0 return $self->follow_url($new_url, $url);
676 }
677 } else {
678 0 return $statusCode;
679 }
680 } elsif ($statusCode =~ /3../) {
681 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
682 0 print "Proper redirect...\n";
683 }
684 0 $new_url = get_location_header(\@lines);
685 0 ▶ if ($new_url ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_url ne '') { } 0 0 0
686 0 ▶ if (exists($url_hash{$new_url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $WWW::Link::Tester::Complex::url_hash{$new_url}) { } 0 0 0
687 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
688 0 print "This url already visited ... returning $statusCode.\n";
689 }
690 0 return $statusCode;
691 } else {
692 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
693 0 print "Redirect to: $new_url\n";
694 }
695 # Return whatever status code we get from new url
696 0 return $self->follow_url($new_url, $url);
697 }
698 } else {
699 0 return $statusCode;
700 }
701 } elsif ($statusCode =~ m{4.. | 5..}x) {
702 0 return $statusCode;
703 }
704 } # if valid status line
705 else {
706 0 unshift(@lines, $statusLine);
707 }
708 # If no status line, could be HTTP/0.9 server, which just sends
709 # back content. If it contains a tag like <html...>, assume it's
710 # okay.
711 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WWW::Link::Tester::Complex::VERBOSE) 0 0 0
712 0 print "Assuming HTTP/0.9 or less... \n";
713 }
714 0 ▶ while ($line = shift @lines) { # Flatten array of lines.
Branch: 1 branch
BranchTrueFalseTotal
if ($line = shift @lines) 0 0 0
715 0 $contentStr .= $line;
716 }
717 0 ▶ if ($contentStr =~ /<html /i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contentStr =~ /<html /i) { } 0 0 0
718 0 return $HTTP_0_9_OKAY;
719 } else {
720 0 return $HTTP_0_9_FAIL;
721 }
722  
723 } # end sub telnet_http_retry
724  
725 ###########################################
726 # END (Unused snippets and test results, below)
727 ###########################################
728  
729 # NOTES:
730  
731 # 1. It would be nice to have a robust facility for absolutizing
732 # URLs. I tried using URI.pm for this purpose and found it to be not
733 # robust. EG., it allows the construction of: http:/www.yahoo.com,
734 # which is not well-formed.
735 # 2. Tolerance of meta refresh tag match?
736 # 3. some duplicate code went from follow_url to the
737 # telnet_http_retry; could be factored.
738  
739  
740 3 1; #Spoilt children / happy / required even
↑ Summary Link/Selector.pm »