line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Alien::Build::Plugin::Fetch::CurlCommand; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
366224
|
use strict; |
|
2
|
|
|
|
|
19
|
|
|
2
|
|
|
|
|
86
|
|
4
|
2
|
|
|
2
|
|
15
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
5
|
2
|
|
|
2
|
|
48
|
use 5.008001; |
|
2
|
|
|
|
|
9
|
|
6
|
2
|
|
|
2
|
|
604
|
use Alien::Build::Plugin; |
|
2
|
|
|
|
|
4854
|
|
|
2
|
|
|
|
|
16
|
|
7
|
2
|
|
|
2
|
|
651
|
use File::Which qw( which ); |
|
2
|
|
|
|
|
1216
|
|
|
2
|
|
|
|
|
176
|
|
8
|
2
|
|
|
2
|
|
637
|
use Path::Tiny qw( path ); |
|
2
|
|
|
|
|
13967
|
|
|
2
|
|
|
|
|
174
|
|
9
|
2
|
|
|
2
|
|
540
|
use Capture::Tiny qw( capture ); |
|
2
|
|
|
|
|
28022
|
|
|
2
|
|
|
|
|
180
|
|
10
|
2
|
|
|
2
|
|
21
|
use File::Temp qw( tempdir ); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
131
|
|
11
|
2
|
|
|
2
|
|
773
|
use File::chdir; |
|
2
|
|
|
|
|
5896
|
|
|
2
|
|
|
|
|
1844
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ABSTRACT: Curl command line plugin for fetching files |
14
|
|
|
|
|
|
|
our $VERSION = '0.01_01'; # TRIAL VERSION |
15
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has curl_command => sub { defined $ENV{CURL} ? which($ENV{CURL}) : which('curl') }; |
19
|
|
|
|
|
|
|
has ssl => 0; |
20
|
|
|
|
|
|
|
has _see_headers => 0; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub init |
23
|
|
|
|
|
|
|
{ |
24
|
1
|
|
|
1
|
1
|
31
|
my($self, $meta) = @_; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$meta->register_hook( |
27
|
|
|
|
|
|
|
fetch => sub { |
28
|
3
|
|
|
3
|
|
15397
|
my($build, $url) = @_; |
29
|
3
|
|
66
|
|
|
15
|
$url ||= $meta->prop->{start_url}; |
30
|
|
|
|
|
|
|
|
31
|
3
|
|
|
|
|
26
|
my($scheme) = $url =~ /^([a-z0-9]+):/i; |
32
|
|
|
|
|
|
|
|
33
|
3
|
50
|
|
|
|
16
|
if($scheme =~ /^https?$/) |
34
|
|
|
|
|
|
|
{ |
35
|
3
|
|
|
|
|
13
|
local $CWD = tempdir( CLEANUP => 1 ); |
36
|
|
|
|
|
|
|
|
37
|
3
|
|
|
|
|
856
|
path('writeout')->spew( |
38
|
|
|
|
|
|
|
join("\\n", |
39
|
|
|
|
|
|
|
"ab-filename :%{filename_effective}", |
40
|
|
|
|
|
|
|
"ab-content_type :%{content_type}", |
41
|
|
|
|
|
|
|
"ab-url :%{url_effective}", |
42
|
|
|
|
|
|
|
), |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
3
|
|
|
|
|
961
|
my @command = ( |
46
|
|
|
|
|
|
|
$self->curl_command, |
47
|
|
|
|
|
|
|
'-L', '-f', -o => 'content', |
48
|
|
|
|
|
|
|
-w => '@writeout', |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
3
|
50
|
|
|
|
37
|
push @command, -D => 'head' if $self->_see_headers; |
52
|
|
|
|
|
|
|
|
53
|
3
|
|
|
|
|
23
|
push @command, $url; |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
12
|
my($stdout, $stderr) = $self->_execute($build, @command); |
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
9
|
my %h = map { my($k,$v) = m/^ab-(.*?)\s*:(.*)$/; $k => $v } split /\n/, $stdout; |
|
6
|
|
|
|
|
32
|
|
|
6
|
|
|
|
|
18
|
|
58
|
|
|
|
|
|
|
|
59
|
2
|
50
|
|
|
|
18
|
if($h{url} =~ m{/([^/]+)$}) |
60
|
|
|
|
|
|
|
{ |
61
|
2
|
|
|
|
|
8
|
$h{filename} = $1; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
else |
64
|
|
|
|
|
|
|
{ |
65
|
0
|
|
|
|
|
0
|
$h{filename} = 'index.html'; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
|
|
43
|
rename 'content', $h{filename}; |
69
|
|
|
|
|
|
|
|
70
|
2
|
50
|
|
|
|
18
|
if(-e 'head') |
71
|
|
|
|
|
|
|
{ |
72
|
0
|
|
|
|
|
0
|
$build->log(" ~ $_ => $h{$_}") for sort keys %h; |
73
|
0
|
|
|
|
|
0
|
$build->log(" header: $_") for path('headers')->lines; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
2
|
|
|
|
|
8
|
my($type) = split ';', $h{content_type}; |
77
|
|
|
|
|
|
|
|
78
|
2
|
100
|
|
|
|
8
|
if($type eq 'text/html') |
79
|
|
|
|
|
|
|
{ |
80
|
|
|
|
|
|
|
return { |
81
|
|
|
|
|
|
|
type => 'html', |
82
|
|
|
|
|
|
|
base => $h{url}, |
83
|
1
|
|
|
|
|
7
|
content => scalar path($h{filename})->slurp, |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else |
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
return { |
89
|
|
|
|
|
|
|
type => 'file', |
90
|
|
|
|
|
|
|
filename => $h{filename}, |
91
|
1
|
|
|
|
|
6
|
path => path($h{filename})->absolute->stringify, |
92
|
|
|
|
|
|
|
}; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
# elsif($scheme eq 'ftp') |
96
|
|
|
|
|
|
|
# { |
97
|
|
|
|
|
|
|
# if($url =~ m{/$}) |
98
|
|
|
|
|
|
|
# { |
99
|
|
|
|
|
|
|
# my($stdout, $stderr) = $self->_execute($build, $self->curl_command, -l => $url); |
100
|
|
|
|
|
|
|
# chomp $stdout; |
101
|
|
|
|
|
|
|
# return { |
102
|
|
|
|
|
|
|
# type => 'list', |
103
|
|
|
|
|
|
|
# list => [ |
104
|
|
|
|
|
|
|
# map { { filename => $_, url => "$url$_" } } sort split /\n/, $stdout, |
105
|
|
|
|
|
|
|
# ], |
106
|
|
|
|
|
|
|
# }; |
107
|
|
|
|
|
|
|
# } |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
# my $first_error; |
110
|
|
|
|
|
|
|
# |
111
|
|
|
|
|
|
|
# { |
112
|
|
|
|
|
|
|
# local $CWD = tempdir( CLEANUP => 1 ); |
113
|
|
|
|
|
|
|
# |
114
|
|
|
|
|
|
|
# my($filename) = $url =~ m{/([^/]+)$}; |
115
|
|
|
|
|
|
|
# $filename = 'unknown' if (! defined $filename) || ($filename eq ''); |
116
|
|
|
|
|
|
|
# $DB::single = 1; |
117
|
|
|
|
|
|
|
# my($stdout, $stderr) = eval { $self->_execute($build, $self->curl_command, -o => $filename, $url) }; |
118
|
|
|
|
|
|
|
# $first_error = $@; |
119
|
|
|
|
|
|
|
# if($first_error eq '') |
120
|
|
|
|
|
|
|
# { |
121
|
|
|
|
|
|
|
# return { |
122
|
|
|
|
|
|
|
# type => 'file', |
123
|
|
|
|
|
|
|
# filename => $filename, |
124
|
|
|
|
|
|
|
# path => path($filename)->absolute->stringify, |
125
|
|
|
|
|
|
|
# }; |
126
|
|
|
|
|
|
|
# } |
127
|
|
|
|
|
|
|
# } |
128
|
|
|
|
|
|
|
# |
129
|
|
|
|
|
|
|
# { |
130
|
|
|
|
|
|
|
# my($stdout, $stderr) = eval { $self->_execute($build, $self->curl_command, -l => "$url/") }; |
131
|
|
|
|
|
|
|
# if($@ eq '') |
132
|
|
|
|
|
|
|
# { |
133
|
|
|
|
|
|
|
# chomp $stdout; |
134
|
|
|
|
|
|
|
# return { |
135
|
|
|
|
|
|
|
# type => 'list', |
136
|
|
|
|
|
|
|
# list => [ |
137
|
|
|
|
|
|
|
# map { { filename => $_, url => "$url/$_" } } sort split /\n/, $stdout, |
138
|
|
|
|
|
|
|
# ], |
139
|
|
|
|
|
|
|
# }; |
140
|
|
|
|
|
|
|
# }; |
141
|
|
|
|
|
|
|
# } |
142
|
|
|
|
|
|
|
# |
143
|
|
|
|
|
|
|
# $first_error ||= 'unknown error'; |
144
|
|
|
|
|
|
|
# die $first_error; |
145
|
|
|
|
|
|
|
# |
146
|
|
|
|
|
|
|
# } |
147
|
|
|
|
|
|
|
else |
148
|
|
|
|
|
|
|
{ |
149
|
0
|
|
|
|
|
0
|
die "scheme $scheme is not supported by the Fetch::CurlCommand plugin"; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
}, |
153
|
1
|
50
|
|
|
|
4
|
) if $self->curl_command; |
154
|
|
|
|
|
|
|
|
155
|
1
|
|
|
|
|
37
|
$self; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _execute |
159
|
|
|
|
|
|
|
{ |
160
|
3
|
|
|
3
|
|
11
|
my($self, $build, @command) = @_; |
161
|
3
|
|
|
|
|
25
|
$build->log("+ @command"); |
162
|
|
|
|
|
|
|
my($stdout, $stderr, $err) = capture { |
163
|
3
|
|
|
3
|
|
2103
|
system @command; |
164
|
3
|
|
|
|
|
801
|
$?; |
165
|
3
|
|
|
|
|
148
|
}; |
166
|
3
|
100
|
|
|
|
1407
|
if($err) |
167
|
|
|
|
|
|
|
{ |
168
|
1
|
|
|
|
|
4
|
chomp $stderr; |
169
|
1
|
|
|
|
|
6
|
$stderr = [split /\n/, $stderr]->[-1]; |
170
|
1
|
|
|
|
|
15
|
die "error in curl fetch: $stderr"; |
171
|
|
|
|
|
|
|
} |
172
|
2
|
|
|
|
|
9
|
($stdout, $stderr); |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
1; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__END__ |