blib/lib/HTTP/Online.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 60.0% 6 / 10 Conditioncond 41.6% 5 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 78.7% 63 / 80 CC 10 SCAR 11.5
CC
10
Cov
79%
online
6.0
import
4.2
new
4.1
CRAP 3.2
↑ Summary
1 package HTTP::Online;
2  
3 =pod
4  
5 =head1 NAME
6  
7 HTTP::Online - Detect full "Internet" (HTTP) access using Microsoft NCSI
8  
9 =head1 SYNOPSIS
10  
11 if ( HTTP::Online->new->online ) {
12 print "Confirmed internet connection\n";
13 } else {
14 print "Internet is not available\n";
15 exit(0);
16 }
17
18 # Now do your task that needs the internet...
19  
20 =head1 DESCRIPTION
21  
22 B<HTTP::Online> is a port of the older L<LWP::Online> module to L<HTTP::Tiny>
23 that uses only the (most accurate) methodology,
24 L<Microsoft NCSI|http://technet.microsoft.com/en-us/library/cc766017.aspx>.
25  
26 =head2 Test Mode
27  
28 use LWP::Online ':skip_all';
29  
30 As a convenience when writing tests scripts base on L<Test::More>, the
31 special ':skip_all' param can be provided when loading B<LWP::Online>.
32  
33 This implements the functional equivalent of the following.
34  
35 BEGIN {
36 unless ( HTTP::Online->new->online ) {
37 require Test::More;
38 Test::More->import(
39 skip_all => 'Test requires a working internet connection'
40 );
41 }
42 }
43  
44 =head1 METHODS
45  
46 =cut
47  
48 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use HTTP::Tiny 0.019 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 3 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
54 3 $VERSION = '0.02';
55 }
56  
57 2 ▶sub import {
Subroutine import: called CC=4 SCAR=14.5
58 2 my $class = shift;
59 2 ▶ if ( $_[0] and $_[0] eq ':skip_all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $_[0] eq ":skip_all") 1 1 2
Condition66%2/3
$_[0] and $_[0] eq ":skip_all"
!ll&&!rl&&r
101
MC/DC50%1/2
$_[0] and $_[0] eq ":skip_all"
$_[0] $_[0] eq ":skip_all"
Truth table66%2/3
A and B
A= $_[0]B= $_[0] eq ":skip_all"
ABresult
0X0
100
111
60 1 require Test::More;
61 1 ▶ unless ( HTTP::Online->new->online ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("HTTP::Online"->new->online) 0 1 1
62 0 Test::More->import( skip_all => 'Test requires a working internet connection' );
63 }
64 }
65 }
66  
67  
68  
69  
70  
71 ######################################################################
72 # Constructor and Accessors
73  
74 =pod
75  
76 =head2 new
77  
78 my $internet = HTTP::Online->new;
79  
80 my $custom = HTTP::Online->new(
81 http => $custom_http_client,
82 url => 'http://my-ncsi-server.com/',
83 content => 'Our Custom NCSI Server',
84 );
85  
86 The C<new> constructor creates a query object.
87  
88 By default, it will be configured to use the same Microsoft NCSI service that
89 the Windows Network Awareness system does (from Windows Vista onwards).
90  
91 Returns a L<HTTP::Online> object.
92  
93 =cut
94  
95 3 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
96 3 my $class = shift;
97 3 my $self = bless { @_ }, $class;
98  
99 # Apply defaults
100 3 ▶ unless ( defined $self->{http} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"http"}) 3 0 3
101 3 $self->{http} = HTTP::Tiny->new(
102 agent => "$class/$VERSION",
103 );
104 }
105 3 ▶ unless ( defined $self->{url} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"url"}) 3 0 3
106 3 $self->{url} = 'http://www.msftncsi.com/ncsi.txt';
107 }
108 3 ▶ unless ( defined $self->{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"content"}) 3 0 3
109 3 $self->{content} = 'Microsoft NCSI';
110 }
111  
112 3 return $self;
113 }
114  
115 =pod
116  
117 =head2 http
118  
119 The C<http> method returns the HTTP client that will be used for the query.
120  
121 =cut
122  
123 5 ▶sub http {
Subroutine http: called CC=1 SCAR=0.0
Pod documented
124 5 $_[0]->{http};
125 }
126  
127 =pod
128  
129 =head2 url
130  
131 The C<url> method returns a string with the location URL of the NCSI file.
132  
133 =cut
134  
135 9 ▶sub url {
Subroutine url: called CC=1 SCAR=0.0
Pod documented
136 9 $_[0]->{url};
137 }
138  
139 =pod
140  
141 =head2 content
142  
143 The C<content> method returns a string with the expected string to be returned
144 from the NCSI server.
145  
146 =cut
147  
148 5 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
149 5 $_[0]->{content};
150 }
151  
152  
153  
154  
155  
156 ######################################################################
157 # Main Methods
158  
159 =pod
160  
161 =head2 online
162  
163 The C<online> method issues a C<Pragma: no-cache> request to the server, and
164 examines the response to confirm that no redirects have occurred, and that the
165 returned content matches the expected value.
166  
167 Returns true if full HTTP internet access is available, or false otherwise.
168  
169 =cut
170  
171 4 ▶sub online {
Subroutine online: called CC=4 SCAR=17.9
Pod documented
172 4 my $self = shift;
173 4 my $response = $self->http->get( $self->url, {
174 headers => {
175 Pragma => 'no-cache',
176 },
177 } );
178  
179 return (
180 $response
181 and
182 $response->{success}
183 and
184 $response->{url} eq $self->url
185 and
186 4 ▶ $response->{content} eq $self->content
Condition33%3/9
$response && $response->{'success'} && $response->{'url'} eq $self->url && $response->{'content'} eq $self->content
!ll&&!rl&&r
004
$response and $response->{"success"} and $response->{"url"} eq $self->url
!ll&&!rl&&r
004
$response and $response->{"success"}
!ll&&!rl&&r
004
MC/DC0%0/4
$response && $response->{'success'} && $response->{'url'} eq $self->url && $response->{'content'} eq $self->content
$response $response->{"success"} $response->{"url"} eq $self->url $response->{'content'} eq $self->content
Truth table20%1/5
A and B and C && D
A= $responseB= $response->{"success"}C= $response->{"url"} eq $self->urlD= $response->{'content'} eq $self->content
ABCDresult
0XXX0
10XX0
110X0
11100
11111
187 );
188 }
189  
190 =pod
191  
192 =head2 offline
193  
194 The C<offline> method is a convenience which currently returns the opposite of
195 the C<online> method, returning false if full HTTP internet access is available,
196 or true otherwise.
197  
198 This may change in future to only return true if we are completely offline, and
199 true in situations where we have partial internet access or the user needs to
200 fill out some web form or view advertising to get full internet access.
201  
202 =cut
203  
204 1 ▶sub offline {
Subroutine offline: called CC=1 SCAR=0.0
Pod documented
205 1 not $_[0]->online;
206 }
207  
208 3 1;
209  
210 =pod
211  
212 =head1 SUPPORT
213  
214 Bugs should be reported via the CPAN bug tracker at
215  
216 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTTP-Online>
217  
218 For other issues, contact the author.
219  
220 =head1 AUTHOR
221  
222 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
223  
224 =head1 SEE ALSO
225  
226 L<LWP::Online>
227  
228 L<HTTP::Tiny>
229  
230 L<http://technet.microsoft.com/en-us/library/cc766017.aspx>
231  
232 =head1 COPYRIGHT
233  
234 Copyright 2012 Adam Kennedy.
235  
236 This program is free software; you can redistribute
237 it and/or modify it under the same terms as Perl itself.
238  
239 The full text of the license can be found in the
240 LICENSE file included with this module.
241  
242 =cut
↑ Summary