line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Tiny; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16
|
use 5.008; |
|
1
|
|
|
|
|
3
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
6
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
118
|
|
9
|
1
|
|
|
1
|
|
772
|
use Storable (); |
|
1
|
|
|
|
|
3518
|
|
|
1
|
|
|
|
|
42
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.010_01'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
7
|
use constant HASH_REF => ref {}; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
121
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
BEGIN { |
16
|
1
|
|
|
1
|
|
4
|
local $@ = undef; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
eval { |
19
|
1
|
|
|
|
|
859
|
require HTTP::Status; |
20
|
0
|
|
|
|
|
0
|
*_status_message = \&HTTP::Status::status_message; |
21
|
0
|
|
|
|
|
0
|
1; |
22
|
|
|
|
|
|
|
} or *_status_message = sub { |
23
|
15
|
|
|
15
|
|
22
|
my ( $status ) = @_; |
24
|
15
|
100
|
|
|
|
22
|
return _is_success( $status ) ? 'OK' : 'Failed'; |
25
|
1
|
50
|
|
|
|
2
|
}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub new { |
29
|
13
|
|
|
13
|
1
|
29
|
my ( $class, %arg ) = @_; |
30
|
|
|
|
|
|
|
defined $arg{fn} |
31
|
13
|
50
|
|
|
|
42
|
or $arg{fn} = 't/data/_http/status'; |
32
|
13
|
50
|
|
|
|
26
|
if ( defined $arg{agent} ) { |
33
|
|
|
|
|
|
|
$arg{agent} =~ m/ \s \z /smx |
34
|
0
|
0
|
|
|
|
0
|
and $arg{agent} .= $class->_default_agent(); |
35
|
|
|
|
|
|
|
} else { |
36
|
13
|
|
|
|
|
28
|
$arg{agent} = $class->_default_agent(); |
37
|
|
|
|
|
|
|
} |
38
|
13
|
|
33
|
|
|
90
|
return bless \%arg, ref $class || $class; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub agent { |
42
|
1
|
|
|
1
|
1
|
3
|
my ( $invocant ) = @_; |
43
|
1
|
50
|
|
|
|
2
|
ref $invocant |
44
|
|
|
|
|
|
|
or return $invocant->_default_agent(); |
45
|
1
|
|
|
|
|
4
|
return $invocant->{agent}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _default_agent { |
49
|
13
|
|
|
13
|
|
22
|
my ( $self ) = @_; |
50
|
13
|
|
33
|
|
|
83
|
( my $agent = ref $self || $self ) =~ s/ :: /-/smxg; |
51
|
13
|
|
|
|
|
158
|
return join '/', "Mock $agent", $self->VERSION(); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# NOTE this is only the static functionality. |
55
|
|
|
|
|
|
|
sub can_ssl { |
56
|
11
|
|
|
11
|
1
|
21
|
local $@ = undef; |
57
|
11
|
|
|
|
|
19
|
my ( $ok, $reason ) = ( 1, '' ); |
58
|
11
|
50
|
|
|
|
31
|
eval { |
59
|
11
|
|
|
|
|
952
|
require IO::Socket::SSL; |
60
|
11
|
|
|
|
|
65282
|
IO::Socket::SSL->VERSION( 1.42 ); |
61
|
11
|
|
|
|
|
54
|
1; |
62
|
|
|
|
|
|
|
} or ( $ok, $reason ) = |
63
|
|
|
|
|
|
|
( 0, "IO::Socket::SSL 1.42 must be installed for https support\n" ); |
64
|
|
|
|
|
|
|
wantarray |
65
|
11
|
50
|
|
|
|
88
|
or return $ok; |
66
|
11
|
|
|
|
|
39
|
return ( $ok, $reason ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub head { |
70
|
15
|
|
|
15
|
1
|
55
|
my ( $self, $url ) = @_; |
71
|
15
|
|
|
|
|
29
|
return $self->request( HEAD => $url ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub request { |
75
|
15
|
|
|
15
|
1
|
25
|
my ( $self, undef, $url ) = @_; |
76
|
15
|
|
66
|
|
|
55
|
$self->{status} ||= Storable::retrieve( $self->{fn} ); |
77
|
15
|
|
|
|
|
1139
|
my $resp = $self->{status}{$url}; |
78
|
15
|
100
|
50
|
|
|
62
|
HASH_REF eq ref $resp |
79
|
|
|
|
|
|
|
or $resp = { |
80
|
|
|
|
|
|
|
status => $resp || 404, |
81
|
|
|
|
|
|
|
}; |
82
|
15
|
|
|
|
|
38
|
$resp->{success} = _is_success( $resp->{status} ); |
83
|
|
|
|
|
|
|
defined $resp->{reason} |
84
|
15
|
50
|
|
|
|
71
|
or $resp->{reason} = _status_message( $resp->{status} ); |
85
|
|
|
|
|
|
|
defined $resp->{url} |
86
|
15
|
100
|
|
|
|
41
|
or $resp->{url} = $url; |
87
|
|
|
|
|
|
|
defined $resp->{content} |
88
|
15
|
50
|
|
|
|
33
|
or $resp->{content} = ''; |
89
|
15
|
|
50
|
|
|
55
|
$resp->{headers} ||= {}; |
90
|
15
|
|
|
|
|
56
|
return $resp; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _is_success { |
94
|
30
|
|
|
30
|
|
44
|
my ( $status ) = @_; |
95
|
30
|
|
66
|
|
|
149
|
return $status >= 200 && $status < 300; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |