line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
3
|
|
44619
|
use 5.006; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
129
|
|
49
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
122
|
|
50
|
3
|
|
|
3
|
|
6026
|
use HTTP::Tiny 0.019 (); |
|
3
|
|
|
|
|
322544
|
|
|
3
|
|
|
|
|
121
|
|
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
3
|
|
35
|
use vars qw{$VERSION}; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
192
|
|
53
|
|
|
|
|
|
|
BEGIN { |
54
|
3
|
|
|
3
|
|
1373
|
$VERSION = '0.02'; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub import { |
58
|
2
|
|
|
2
|
|
19
|
my $class = shift; |
59
|
2
|
100
|
66
|
|
|
26
|
if ( $_[0] and $_[0] eq ':skip_all' ) { |
60
|
1
|
|
|
|
|
1332
|
require Test::More; |
61
|
1
|
50
|
|
|
|
21861
|
unless ( HTTP::Online->new->online ) { |
62
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
sub new { |
96
|
3
|
|
|
3
|
1
|
29
|
my $class = shift; |
97
|
3
|
|
|
|
|
13
|
my $self = bless { @_ }, $class; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Apply defaults |
100
|
3
|
50
|
|
|
|
31
|
unless ( defined $self->{http} ) { |
101
|
3
|
|
|
|
|
63
|
$self->{http} = HTTP::Tiny->new( |
102
|
|
|
|
|
|
|
agent => "$class/$VERSION", |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
} |
105
|
3
|
50
|
|
|
|
257
|
unless ( defined $self->{url} ) { |
106
|
3
|
|
|
|
|
13
|
$self->{url} = 'http://www.msftncsi.com/ncsi.txt'; |
107
|
|
|
|
|
|
|
} |
108
|
3
|
50
|
|
|
|
15
|
unless ( defined $self->{content} ) { |
109
|
3
|
|
|
|
|
12
|
$self->{content} = 'Microsoft NCSI'; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
3
|
|
|
|
|
16
|
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
|
|
|
|
|
|
|
sub http { |
124
|
5
|
|
|
5
|
1
|
494
|
$_[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
|
|
|
|
|
|
|
sub url { |
136
|
9
|
|
|
9
|
1
|
240
|
$_[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
|
|
|
|
|
|
|
sub content { |
149
|
5
|
|
|
5
|
1
|
621
|
$_[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
|
|
|
|
|
|
|
sub online { |
172
|
4
|
|
|
4
|
1
|
23
|
my $self = shift; |
173
|
4
|
|
|
|
|
17
|
my $response = $self->http->get( $self->url, { |
174
|
|
|
|
|
|
|
headers => { |
175
|
|
|
|
|
|
|
Pragma => 'no-cache', |
176
|
|
|
|
|
|
|
}, |
177
|
|
|
|
|
|
|
} ); |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
return ( |
180
|
4
|
|
33
|
|
|
45494
|
$response |
181
|
|
|
|
|
|
|
and |
182
|
|
|
|
|
|
|
$response->{success} |
183
|
|
|
|
|
|
|
and |
184
|
|
|
|
|
|
|
$response->{url} eq $self->url |
185
|
|
|
|
|
|
|
and |
186
|
|
|
|
|
|
|
$response->{content} eq $self->content |
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
|
|
|
|
|
|
|
sub offline { |
205
|
1
|
|
|
1
|
1
|
8598
|
not $_[0]->online; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
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 |