line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::Curlizer; |
2
|
2
|
|
|
2
|
|
202112
|
use strict; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
56
|
|
3
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
47
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
891
|
use ShellQuote::Any; |
|
2
|
|
|
|
|
1078
|
|
|
2
|
|
|
|
|
12
|
|
6
|
2
|
|
|
2
|
|
1041
|
use Plack::Request; |
|
2
|
|
|
|
|
142682
|
|
|
2
|
|
|
|
|
69
|
|
7
|
2
|
|
|
2
|
|
16
|
use parent 'Plack::Middleware'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
9
|
|
8
|
2
|
|
|
|
|
15
|
use Plack::Util::Accessor qw/ |
9
|
|
|
|
|
|
|
callback |
10
|
2
|
|
|
2
|
|
6155
|
/; |
|
2
|
|
|
|
|
5
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub prepare_app { |
15
|
1
|
|
|
1
|
1
|
820
|
my ($self) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
50
|
|
|
|
4
|
unless ($self->callback) { |
18
|
|
|
|
|
|
|
$self->callback(sub{ |
19
|
0
|
|
|
0
|
|
0
|
my ($curl) = @_; |
20
|
0
|
|
|
|
|
0
|
print "$curl\n"; |
21
|
0
|
|
|
|
|
0
|
}); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub call { |
26
|
2
|
|
|
2
|
1
|
16453
|
my($self, $env) = @_; |
27
|
|
|
|
|
|
|
|
28
|
2
|
|
|
|
|
26
|
my $req = Plack::Request->new($env); |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
29
|
my $curl = $self->_build_curl_cmd($req); |
31
|
|
|
|
|
|
|
|
32
|
2
|
50
|
33
|
|
|
1752
|
if ($self->callback && ref($self->callback) eq 'CODE') { |
33
|
2
|
|
|
|
|
34
|
$self->callback->($curl, $req, $env); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
2
|
|
|
|
|
34
|
my $res = $self->app->($env); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _build_curl_cmd { |
40
|
2
|
|
|
2
|
|
4
|
my ($self, $req) = @_; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
7
|
my @cmd = ('curl', $req->uri); |
43
|
|
|
|
|
|
|
|
44
|
2
|
100
|
|
|
|
641
|
unless ($req->method eq 'GET') { |
45
|
1
|
|
|
|
|
9
|
push @cmd, '-X', $req->method; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
2
|
|
|
|
|
20
|
my $http_header_str = $req->headers->as_string; |
49
|
2
|
|
|
|
|
505
|
my @headers = split "\n", $http_header_str; |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
6
|
for my $h (@headers) { |
52
|
5
|
|
|
|
|
22
|
my ($k, $v) = split /:\s+/, $h; |
53
|
5
|
|
|
|
|
18
|
push @cmd, '-H', qq|$k: $v|; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
2
|
100
|
|
|
|
7
|
if ($req->method eq 'POST') { |
57
|
1
|
|
|
|
|
9
|
push @cmd, '--data', $req->content; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
2
|
|
|
|
|
467
|
return shell_quote \@cmd; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
__END__ |