line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Enbld::HTTP; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
3040
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
150
|
|
4
|
4
|
|
|
4
|
|
27
|
use warnings; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
114
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
23
|
use File::Spec; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
102
|
|
7
|
4
|
|
|
4
|
|
32
|
use Carp; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
276
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
14392
|
use HTTP::Tiny; |
|
4
|
|
|
|
|
217223
|
|
|
4
|
|
|
|
|
3254
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $ua; |
12
|
|
|
|
|
|
|
our $download_hook; |
13
|
|
|
|
|
|
|
our $get_hook; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Enbld::Message; |
16
|
|
|
|
|
|
|
require Enbld::Error; |
17
|
|
|
|
|
|
|
require Enbld::Exception; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub initialize_ua { |
20
|
0
|
|
|
0
|
0
|
0
|
$ua = HTTP::Tiny->new; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub download { |
24
|
54
|
|
|
54
|
0
|
242
|
my ( $pkg, $url, $path ) = @_; |
25
|
|
|
|
|
|
|
|
26
|
54
|
|
|
|
|
1306
|
my ( undef, undef, $file ) = File::Spec->splitpath( $path ); |
27
|
|
|
|
|
|
|
|
28
|
54
|
100
|
|
|
|
1738
|
if ( -e $path ) { |
29
|
2
|
|
|
|
|
17
|
my $msg = "--> Use file '$file' that is previously downloaded."; |
30
|
2
|
|
|
|
|
24
|
Enbld::Message->notify( $msg ); |
31
|
2
|
|
|
|
|
9
|
return $path; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# for debug hook |
35
|
52
|
50
|
|
|
|
360
|
if ( $download_hook ) { |
36
|
52
|
|
|
|
|
351
|
$download_hook->( $pkg, $url, $path ); |
37
|
52
|
|
|
|
|
29399
|
return $path; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
0
|
Enbld::Message->notify( "--> Download '$file' from '$url'." ); |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
0
|
my $curl = `which curl`; |
43
|
0
|
|
|
|
|
0
|
chomp $curl; |
44
|
0
|
0
|
|
|
|
0
|
if ( $curl ) { |
45
|
0
|
|
|
|
|
0
|
system("$curl -L $url -o $path -s"); |
46
|
|
|
|
|
|
|
} else { |
47
|
0
|
0
|
|
|
|
0
|
initialize_ua() unless $ua; |
48
|
0
|
|
|
|
|
0
|
my $res = $ua->mirror( $url, $path ); |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
0
|
if ( ! $res->{success} ) { |
51
|
0
|
|
|
|
|
0
|
Enbld::Error->throw( $res->{reason} ); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
0
|
return $path; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub download_archivefile { |
59
|
29
|
|
|
29
|
0
|
151
|
my ( $pkg, $url, $path ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
29
|
|
|
|
|
145
|
my $downloaded = Enbld::HTTP->download( $url, $path ); |
62
|
|
|
|
|
|
|
|
63
|
29
|
|
|
|
|
951
|
require Enbld::Archivefile; |
64
|
29
|
|
|
|
|
710
|
return Enbld::Archivefile->new( $downloaded ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub get { |
68
|
35
|
|
|
35
|
0
|
154
|
my ( $pkg, $url ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
35
|
50
|
|
|
|
116
|
if ( $get_hook ) { |
71
|
35
|
|
|
|
|
346
|
return $get_hook->( $pkg, $url ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
0
|
initialize_ua() unless $ua; |
75
|
0
|
|
|
|
|
0
|
my $res = $ua->get( $url ); |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
0
|
if ( ! $res->{success} ) { |
78
|
0
|
|
|
|
|
0
|
Enbld::Error->throw( $res->{reason} ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
0
|
return $res->{content}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub get_html { |
85
|
35
|
|
|
35
|
0
|
159
|
my ( $pkg, $url ) = @_; |
86
|
|
|
|
|
|
|
|
87
|
35
|
|
|
|
|
268
|
my $content = Enbld::HTTP->get( $url ); |
88
|
|
|
|
|
|
|
|
89
|
35
|
|
|
|
|
2700
|
require Enbld::HTML; |
90
|
35
|
|
|
|
|
495
|
return Enbld::HTML->new( $content ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# For debug methods. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub register_get_hook { |
96
|
3
|
|
|
3
|
0
|
1352
|
my ( $pkg, $coderef ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
3
|
50
|
|
|
|
18
|
if ( ref( $coderef ) eq 'CODE' ) { |
99
|
3
|
|
|
|
|
8
|
$get_hook = $coderef; |
100
|
3
|
|
|
|
|
10
|
return $pkg; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
my $err = "register get hook requires CODE reference parameter."; |
104
|
0
|
|
|
|
|
0
|
require Enbld::Exception; |
105
|
0
|
|
|
|
|
0
|
Enbld::Exception->throw( $err, $coderef ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub register_download_hook { |
109
|
1
|
|
|
1
|
0
|
11
|
my ( $pkg, $coderef ) = @_; |
110
|
|
|
|
|
|
|
|
111
|
1
|
50
|
|
|
|
4
|
if ( ref( $coderef ) eq 'CODE' ) { |
112
|
1
|
|
|
|
|
2
|
$download_hook = $coderef; |
113
|
1
|
|
|
|
|
3
|
return $pkg; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $err = "register download hook requires CODE reference parameter."; |
117
|
0
|
|
|
|
|
|
require Enbld::Exception; |
118
|
0
|
|
|
|
|
|
Enbld::Exception->throw( $err, $coderef ); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |