line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Acme::HTTP; |
2
|
|
|
|
|
|
|
$Acme::HTTP::VERSION = '0.10'; |
3
|
1
|
|
|
1
|
|
562
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
118
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
8
|
|
|
|
|
|
|
our @EXPORT = qw( |
9
|
|
|
|
|
|
|
get_url_act |
10
|
|
|
|
|
|
|
get_redir_act |
11
|
|
|
|
|
|
|
get_code |
12
|
|
|
|
|
|
|
get_message |
13
|
|
|
|
|
|
|
get_response |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
get_redir_max |
16
|
|
|
|
|
|
|
get_timeout |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
set_redir_max |
19
|
|
|
|
|
|
|
set_timeout |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [ @EXPORT ]); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT_OK = qw(); |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
551
|
use IO::Select; |
|
1
|
|
|
|
|
1189
|
|
|
1
|
|
|
|
|
519
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $Url_act = ''; |
29
|
|
|
|
|
|
|
our $Redir_act = 0; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $Code = -1; |
32
|
|
|
|
|
|
|
our $Message = '?'; |
33
|
|
|
|
|
|
|
our %Response = (); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $Redir_max; |
36
|
|
|
|
|
|
|
$Redir_max = 3 unless defined $Redir_max; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
our $TimeOut; |
39
|
|
|
|
|
|
|
$TimeOut = 10 unless defined $TimeOut; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
0
|
1
|
|
sub get_url_act { $Url_act } |
42
|
0
|
|
|
0
|
1
|
|
sub get_redir_act { $Redir_act } |
43
|
0
|
|
|
0
|
1
|
|
sub get_code { $Code } |
44
|
0
|
|
|
0
|
1
|
|
sub get_message { $Message } |
45
|
0
|
|
|
0
|
1
|
|
sub get_response { \%Response } |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
0
|
0
|
|
sub get_redir_max { $Redir_max } |
48
|
0
|
|
|
0
|
0
|
|
sub get_timeout { $TimeOut } |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
0
|
1
|
|
sub set_redir_max { $Redir_max = $_[0] } |
51
|
0
|
|
|
0
|
1
|
|
sub set_timeout { $TimeOut = $_[0] } |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub new { |
54
|
0
|
|
|
0
|
0
|
|
shift; |
55
|
0
|
|
|
|
|
|
my ($url) = @_; |
56
|
0
|
|
|
|
|
|
my $hdl; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
$Url_act = ''; |
59
|
0
|
|
|
|
|
|
$Redir_act = 0; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
while (defined $url) { |
62
|
0
|
|
|
|
|
|
$Redir_act++; |
63
|
0
|
0
|
|
|
|
|
if ($Redir_act > $Redir_max) { |
64
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Runaway iterations ('.$Redir_max.')'; |
65
|
0
|
|
|
|
|
|
return; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
$Url_act = $url; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my ($type, $host, $get) = |
71
|
|
|
|
|
|
|
$Url_act =~ m{\A ([^:]+) : // ([^/]+) \z}xms ? ($1, $2, '/') : |
72
|
|
|
|
|
|
|
$Url_act =~ m{\A ([^:]+) : // ([^/]+) (/ .*) \z}xms ? ($1, $2, $3) : |
73
|
0
|
0
|
|
|
|
|
do { |
|
|
0
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Invalid structure)'; |
75
|
0
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
}; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $net_http = |
79
|
|
|
|
|
|
|
$type eq 'http' ? 'Net::HTTP::NB' : |
80
|
|
|
|
|
|
|
$type eq 'https' ? 'Net::HTTPS::NB' : |
81
|
0
|
0
|
|
|
|
|
do { |
|
|
0
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Can\'t identify type'; |
83
|
0
|
|
|
|
|
|
return; |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if ($net_http eq 'Net::HTTP::NB') { |
|
|
0
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
require Net::HTTP::NB; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
elsif ($net_http eq 'Net::HTTPS::NB') { |
90
|
0
|
|
|
|
|
|
require Net::HTTPS::NB; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Internal error net_http = \''.$net_http.'\''; |
94
|
0
|
|
|
|
|
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
$hdl = $net_http->new(Host => $host) or do { |
98
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Can\'t Net::HTTP(S)->new(Host =>...)'; |
99
|
0
|
|
|
|
|
|
return; |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
$hdl->write_request(GET => $get, 'User-Agent' => 'Mozilla/5.0'); |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
1
|
|
5
|
use IO::Select; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
258
|
|
105
|
0
|
|
|
|
|
|
my $sel = IO::Select->new($hdl); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
READ_HEADER: { |
108
|
0
|
0
|
|
|
|
|
unless ($sel->can_read($TimeOut)) { |
|
0
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Header timeout('.$TimeOut.')'; |
110
|
0
|
|
|
|
|
|
return; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
($Code, $Message, %Response) = $hdl->read_response_headers; |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
redo READ_HEADER unless $Code; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
$url = $Response{'Location'}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
unless (defined $hdl) { |
122
|
0
|
|
|
|
|
|
$@ = 'Acme::HTTP - Internal error, hdl is undefined'; |
123
|
0
|
|
|
|
|
|
return; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
bless { hdl => $hdl }; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub read_entity_body { |
130
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my $hdl = $self->{'hdl'}; |
133
|
0
|
|
|
|
|
|
my $sel = IO::Select->new($hdl); |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
unless ($sel->can_read($Acme::HTTP::TimeOut)) { |
136
|
0
|
|
|
|
|
|
$@ = 'Timeout ('.$Acme::HTTP::TimeOut.' sec)'; |
137
|
0
|
|
|
|
|
|
return; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $bytes = $hdl->read_entity_body($_[0], $_[1]); |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
unless (defined $bytes) { |
143
|
0
|
|
|
|
|
|
$@ = "$!"; |
144
|
0
|
|
|
|
|
|
return; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
return $bytes; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
__END__ |