line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Client; |
2
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
6
|
use base qw(Object); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
91
|
|
5
|
1
|
|
|
1
|
|
5
|
use SHashtable; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
6
|
1
|
|
|
1
|
|
610
|
use HTTP::DOM; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
7
|
1
|
|
|
1
|
|
5779
|
use AnyEvent::HTTP; |
|
1
|
|
|
|
|
56499
|
|
|
1
|
|
|
|
|
120
|
|
8
|
1
|
|
|
1
|
|
11
|
use AnyEvent; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
278
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
0
|
|
|
0
|
0
|
|
my $pkg = shift; |
12
|
0
|
|
|
|
|
|
return bless {}, $pkg; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 web_get |
16
|
|
|
|
|
|
|
my $url = 'http://www.baidu.com/'; |
17
|
|
|
|
|
|
|
my $content = web_get($url); |
18
|
|
|
|
|
|
|
say "Scalar: " . $content->xml; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my @urls = qw(http://www.baidu.com http://www.sina.com.cn); |
21
|
|
|
|
|
|
|
web_get(@urls, sub { |
22
|
|
|
|
|
|
|
#web_get($url, sub { |
23
|
|
|
|
|
|
|
my ($content, $code, $res_headers) = @_; # $res_headers 是 SHashtable 类型 |
24
|
|
|
|
|
|
|
say "HTTP: " . $code; |
25
|
|
|
|
|
|
|
say "Body: " . $content->text; |
26
|
|
|
|
|
|
|
$res_headers->each( |
27
|
|
|
|
|
|
|
sub { |
28
|
|
|
|
|
|
|
my ($header_k, $header_v) = @_; |
29
|
|
|
|
|
|
|
say "$header_k: $header_v"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
}); |
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub web_get { |
36
|
0
|
|
|
0
|
0
|
|
my $cb; |
37
|
0
|
0
|
|
|
|
|
if ( ref( $_[-1] ) eq 'CODE' ) { |
38
|
0
|
|
|
|
|
|
$cb = pop @_; |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
|
my @urls = @_; |
41
|
0
|
|
|
|
|
|
my $content; |
42
|
0
|
|
|
|
|
|
my $w = AnyEvent->condvar; |
43
|
0
|
|
|
|
|
|
for my $url (@urls) { |
44
|
0
|
|
|
|
|
|
$w->begin; |
45
|
|
|
|
|
|
|
http_get $url, sub { |
46
|
0
|
|
|
0
|
|
|
my ( $data, $headers ) = @_; |
47
|
0
|
|
|
|
|
|
my $code = delete $headers->{Status}; |
48
|
0
|
|
|
|
|
|
my $res_headers = SHashtable->new(%$headers); |
49
|
0
|
|
|
|
|
|
$content = HTTP::DOM->new($data); |
50
|
0
|
|
|
|
|
|
$w->end; |
51
|
0
|
0
|
|
|
|
|
if ( defined $cb ) { |
52
|
0
|
|
|
|
|
|
$cb->( $content, $code, $res_headers ); |
53
|
|
|
|
|
|
|
}; |
54
|
0
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
$w->recv; |
57
|
0
|
0
|
|
|
|
|
if ( !defined $cb ) { |
58
|
0
|
|
|
|
|
|
return $content; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |