blib/lib/LWP/Online.pm

filter: Statementstmt 76.6% 59 / 77 Branchbran 39.2% 11 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 15 / 18 Podpod 66.6% 2 / 3 totaltotal 66.4% 87 / 131 CC 16 SCAR 25.0
CC
16
Cov
65%
online
43.8
http_online
8.3
__ANON__
6.0
CRAP 12.2
↑ Summary
1 package LWP::Online;
2  
3 =pod
4  
5 =head1 NAME
6  
7 LWP::Online - Does your process have access to the web
8  
9 =head1 SYNOPSIS
10  
11 use LWP::Online 'online';
12
13 # "Is the internet working?"
14 die "NO INTARWWEB!!!" unless online();
15
16 # The above means something like this
17 unless ( online('http') ) {
18 die "No basic http access to the web";
19 }
20
21 # Special syntax for use in test scripts that need
22 # "real" access to the internet. Scripts will automatically
23 # skip if connection fails.
24 use LWP::Online ':skip_all';
25 use Test::More tests => 4; #after LWP::Online
26  
27 =head1 DESCRIPTION
28  
29 This module attempts to answer, as accurately as it can, one of the
30 nastiest technical questions there is.
31  
32 B<Am I on the internet?>
33  
34 The answer is useful in a wide range of decisions. For example...
35  
36 I<Should my test scripts run the online portion of the tests or
37 just skip them?>
38  
39 I<Do I try to fetch fresh data from the server?>
40  
41 I<If my request to the server breaks, is it because I'm offline, or
42 because the server is offline?>
43  
44 And so on, and so forth.
45  
46 But a host of networking and security issues make this problem
47 very difficult. There are firewalls, proxies (both well behaved and
48 badly behaved). We might not have DNS. We might not have a network
49 card at all!
50  
51 You might have network access, but only to a for-money wireless network
52 that responds to ever HTTP request with a page asking you to enter your
53 credit card details for paid access. Which means you don't "REALLY" have
54 access.
55  
56 The mere nature of the question makes it practically unsolvable.
57  
58 But with the answer being so useful, and the only other alternative being
59 to ask the user "duh... are you online?" (when you might not have a user
60 at all) it's my gut feeling that it is worthwhile at least making an
61 attempt to solve the problem, if only in a limited way.
62  
63 =head2 Why LWP::Online? Why not Net::Online?
64  
65 The nice thing about LWP::Online is that LWP deals with a whole range of
66 different transports, and is very commonly installed. HTTP, HTTPS, FTP,
67 and so on and so forth.
68  
69 Attempting to do a more generalised Net::Online that might also check for
70 SSH and so on would end up most likely having to install a whole bunch of
71 modules that you most likely will never use.
72  
73 So LWP forms a nice base on which to write a module that covers most of
74 the situations in which you might care, while keeping the dependency
75 overhead down to a minimum.
76  
77 =head2 Scope
78  
79 "Am I online?" is inherently an Open Problem.
80  
81 That is, it's a problem that had no clean permanent solution, and for
82 which you could just keep writing more and more functionality
83 indefinitely, asymtopically approaching 100% correctness but never
84 reaching it.
85  
86 And so this module is intended to do as good a job as possible, without
87 having to resort to asking any human questions (who may well get it wrong
88 anyway), and limiting itself to a finite amount of programming work and
89 a reasonable level of memory overhead to load the code.
90  
91 It is thus understood the module will B<never> be perfect, and that if
92 any new functionality is desired, it needs to be able to implemented by
93 the person that desires the new behaviour, and in a reasonably small
94 amount of additional code.
95  
96 This module is also B<not> intended to compensate for malicious behaviour
97 of any kind, it is quite possible that some malicious person might proxy
98 fake versions of sites that pass our content checks and then proceed
99 to show you other bad pages.
100  
101 =head2 Test Mode
102  
103 use LWP::Online ':skip_all';
104  
105 As a convenience when writing tests scripts base on L<Test::More>, the
106 special ':skip_all' param can be provided when loading B<LWP::Online>.
107  
108 This implements the functional equivalent of the following.
109  
110 BEGIN {
111 require Test::More;
112 unless ( LWP::Online::online() ) {
113 Test::More->import(
114 skip_all => 'Test requires a working internet connection'
115 );
116 }
117 }
118  
119 The :skip_all special import flag can be mixed with regular imports.
120  
121 =head1 FUNCTIONS
122  
123 =cut
124  
125 3 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
128 3 ▶use URI 1.35 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
129 3 ▶use LWP::Simple 5.805 qw{ get $ua };
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 3 ▶use vars qw{$VERSION @ISA @EXPORT_OK};
Subroutine BEGIN: called CC=1 SCAR=0.0
132 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
133 3 $VERSION = '1.08';
134  
135 # We are an Exporter
136 3 require Exporter;
137 3 @ISA = qw{ Exporter };
138 3 @EXPORT_OK = qw{ online offline };
139  
140 # Set the useragent timeout
141 3 $ua->timeout(30);
142 }
143  
144 # Set up configuration data
145 3 ▶use vars qw{%SUPPORTED @RELIABLE_HTTP};
Subroutine BEGIN: called CC=1 SCAR=0.0
146 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=1.2
147 # What transports do we support
148 3 %SUPPORTED = map { $_ => 1 } qw{ http };
149  
150 # (Relatively) reliable websites
151 @RELIABLE_HTTP = (
152 # These are some initial trivial checks.
153 # The regex are case-sensitive to at least
154 # deal with the "couldn't get site.com case".
155 4 ▶ 'http://www.msftncsi.com/ncsi.txt' => sub { $_ eq 'Microsoft NCSI' },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 4 ▶ 'http://google.com/' => sub { /About Google/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 0 ▶ 'http://yahoo.com/' => sub { /Yahoo!/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 ▶ 'http://amazon.com/' => sub { /Amazon/ and /Cart/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/Amazon/ and /Cart/ 0 0 0
159 0 ▶ 'http://cnn.com/' => sub { /CNN/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 3 );
161 }
162  
163 3 ▶sub import {
Subroutine import: called CC=3 SCAR=11.1
164 3 my $class = shift;
165  
166 # Handle the :skip_all special case
167 3 my @functions = grep { $_ ne ':skip_all' } @_;
168 3 ▶ if ( @functions != @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@functions != @_) 1 2 3
169 1 require Test::More;
170 1 ▶ unless ( online() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (online()) 0 1 1
171 0 Test::More->import( skip_all => 'Test requires a working internet connection' );
172 }
173 }
174  
175 # Hand the rest of the params off to Exporter
176 3 return $class->export_to_level( 1, $class, @functions );
177 }
178  
179  
180  
181  
182  
183 #####################################################################
184 # Exportable Functions
185  
186 =pod
187  
188 =head2 online
189  
190 # Default check (uses http)
191 online() or die "No Internet";
192
193 # The above is equivalent to
194 online('http') or die "No Internet";
195  
196 The importable B<online> function is the main functionality provided
197 by B<LWP::Online>. It takes a single optional transport name ('http'
198 by default) and checks that LWP connectivity is available for that
199 transport.
200  
201 Because it is intended as a Do What You Mean function, it checks not
202 only that a network connection is available, and http requests return
203 content, but also that it returns the CORRECT content instead of
204 unexpected content supplied by a man in the middle.
205  
206 For example, many wireless connections require login or payment, and
207 will return a service provider page for any URI that you attempt to
208 fetch.
209  
210 The set of websites used for the testing is the Google, Amazon,
211 Yahoo and CNN websites. The check is for a copyright statement on their
212 homepage, and the function returns true as soon as two of the website
213 return correctly, making the method relatively redundant.
214  
215 Returns true if the computer is "online" (has a working connection via
216 LWP) or false if not.
217  
218 =cut
219  
220 4 ▶sub online {
Subroutine online: called CC=7 SCAR=37.8
Pod documented
221 # Shortcut the default to plain http_online test
222 4 ▶ return http_online() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 0 4
223  
224 0 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
225 # Get the transport to test
226 0 my $transport = shift;
227 0 ▶ unless ( $transport and $SUPPORTED{$transport} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($transport and $LWP::Online::SUPPORTED{$transport}) 0 0 0
Condition0%0/3
$transport and $LWP::Online::SUPPORTED{$transport}
!ll&&!rl&&r
000
MC/DC0%0/2
$transport and $LWP::Online::SUPPORTED{$transport}
$transport $LWP::Online::SUPPORTED{$transport}
Truth table0%0/3
A and B
A= $transportB= $LWP::Online::SUPPORTED{$transport}
ABresult
0X0
100
111
228 0 Carp::croak("Invalid or unsupported transport");
229 }
230  
231 # Hand off to the transport function
232 0 ▶ if ( $transport eq 'http' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transport eq 'http') { } 0 0 0
233 0 ▶ http_online() or return '';
Branch: 1 branch
BranchTrueFalseTotal
http_online() or return "" 0 0 0
234 } else {
235 0 Carp::croak("Invalid or unsupported transport");
236 }
237 }
238  
239 # All required transports available
240 0 return 1;
241 }
242  
243 =pod
244  
245 =head2 offline
246  
247 The importable B<offline> function is provided as a convenience.
248  
249 It provides a simple pass-through (including any params) to the B<online>
250 function, but with a negated result.
251  
252 =cut
253  
254 1 ▶sub offline {
Subroutine offline: called CC=1 SCAR=0.0
Pod documented
255 1 ! online(@_);
256 }
257  
258  
259  
260  
261  
262 #####################################################################
263 # Transport Functions
264  
265 4 ▶sub http_online {
Subroutine http_online: called CC=7 SCAR=21.1
Pod undocumented
266 # Check the reliable websites list.
267 # If networking is offline, an error/paysite page might still
268 # give us a page that matches a page check, while any one or
269 # two of the reliable websites might be offline for some
270 # unknown reason (DDOS, earthquake, chinese firewall, etc)
271 # So we want 2 or more sites to pass checks to make the
272 # judgement call that we are online.
273 4 my $good = 0;
274 4 my $bad = 0;
275 4 my @reliable = @RELIABLE_HTTP;
276 4 ▶ while ( @reliable ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reliable) 12 0 12
277 # Check the current good/bad state and consider
278 # making the online/offline judgement call.
279 12 ▶ return 1 if $good > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $good > 1 4 8 12
280 8 ▶ return '' if $bad > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $bad > 2 0 8 8
281  
282 # Try the next reliable site
283 8 my $site = shift @reliable;
284 8 my $check = shift @reliable;
285  
286 # Try to fetch the site
287 8 my $content;
288 SCOPE: {
289 8 local $@;
290 8 $content = eval { get($site) };
291 8 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
292 # An exception is a simple failure
293 0 $bad++;
294 0 next;
295 }
296 }
297 8 ▶ unless ( defined $content ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 8 8
298 # get() returns undef on failure
299 0 $bad++;
300 0 next;
301 }
302  
303 # We got _something_.
304 # Check if it looks like what we want
305 SCOPE: {
306 8 local $_ = $content;
307 8 ▶ if ( $check->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$check()) { } 8 0 8
308 8 $good++;
309 } else {
310 0 $bad++;
311 }
312 }
313 }
314  
315 # We've run out of sites to check... erm... uh...
316 # We should probably fail conservatively and say not online.
317 0 return '';
318 }
319  
320 3 1;
321  
322 =pod
323  
324 =head1 TO DO
325  
326 - Add more transport types that can be checked, somehow keeping the
327 code growth under control.
328  
329 =head1 SUPPORT
330  
331 This module is stored in an Open Repository at the following address.
332  
333 L<http://svn.ali.as/cpan/trunk/LWP-Online>
334  
335 Write access to the repository is made available automatically to any
336 published CPAN author, and to most other volunteers on request.
337  
338 If you are able to submit your bug report in the form of new (failing)
339 unit tests (which for this module will be extremely difficult), or can
340 apply your fix directly instead of submitting a patch, you are B<strongly>
341 encouraged to do so as the author currently maintains over 100 modules
342 and it can take some time to deal with non-Critical bug reports or patches.
343  
344 This will guarentee that your issue will be addressed in the next
345 release of the module.
346  
347 If you cannot provide a direct test or fix, or don't have time to do so,
348 then regular bug reports are still accepted and appreciated via the CPAN
349 bug tracker.
350  
351 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LWP-Online>
352  
353 For other issues, for commercial enhancement or support, or to have your
354 write access enabled for the repository, contact the author at the email
355 address above.
356  
357 =head1 AUTHOR
358  
359 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
360  
361 =head1 SEE ALSO
362  
363 L<LWP::Simple>
364  
365 =head1 COPYRIGHT
366  
367 Copyright 2006 - 2011 Adam Kennedy.
368  
369 This program is free software; you can redistribute
370 it and/or modify it under the same terms as Perl itself.
371  
372 The full text of the license can be found in the
373 LICENSE file included with this module.
374  
375 =cut
↑ Summary