line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::SaveRequest; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1011
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
10
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
233
|
use IO::File; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
349
|
|
6
|
1
|
|
|
1
|
|
20
|
use POSIX 'strftime'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
11
|
|
7
|
1
|
|
|
1
|
|
84
|
use Time::HiRes; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
1
|
|
|
1
|
1
|
53
|
my ($self, $app) = @_; |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
|
|
35
|
$app->routes->add_condition(save => \&_save); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _save { |
18
|
0
|
|
|
0
|
|
|
my ($r, $c, $conf, $state_dir) = @_; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $abs_path = $c->app->home->abs_path; |
21
|
0
|
|
|
|
|
|
my $now = POSIX::strftime("%Y-%d-%m", localtime(time)); |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $path = "$abs_path/$state_dir/$now"; |
24
|
0
|
0
|
|
|
|
|
if (!-d $path) { |
25
|
0
|
0
|
|
|
|
|
mkdir($path) or return 1;; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $t0 = join(".", Time::HiRes::gettimeofday()); |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my $handle = IO::File->new(); |
31
|
0
|
|
|
|
|
|
my $count = 0; |
32
|
0
|
|
|
|
|
|
my $name = sprintf("$path/go.%s.%d.%08d.pl", $t0, $$, $count); |
33
|
0
|
|
|
|
|
|
until ($handle->open($name, O_CREAT | O_EXCL | O_RDWR)) { |
34
|
0
|
|
|
|
|
|
++$count; |
35
|
0
|
0
|
|
|
|
|
if (1_000_000 <= $count) { |
36
|
0
|
|
|
|
|
|
die("Too many open attempts"); |
37
|
|
|
|
|
|
|
} |
38
|
0
|
|
|
|
|
|
$name = sprintf("$path/go.%s.%d.%08d.pl", $t0, $$, $count); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
print($handle "\#\!$^X\n\n"); |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $req = $c->req; |
44
|
0
|
|
|
|
|
|
my $headers = $req->headers->to_hash; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
print($handle "my \%headers = (\n"); |
47
|
0
|
|
|
|
|
|
foreach my $header (sort keys %{ $headers }) { |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
print($handle "\tqq($header) => qq($$headers{$header}),\n"); |
49
|
|
|
|
|
|
|
} |
50
|
0
|
|
|
|
|
|
print($handle ");\n\n"); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
print($handle "my \$h = join(\"-H \", map({ \"\$_:\$headers{\$_}\" } keys \%headers));\n\n"); |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
print($handle "my \$method = '" . $req->method . "';\n"); |
55
|
0
|
|
|
|
|
|
print($handle "my \$query_params = '" . $req->query_params . "';\n"); |
56
|
0
|
|
|
|
|
|
print($handle "my \$url = '" . $req->url->to_string . "';\n"); |
57
|
0
|
|
|
|
|
|
print($handle "my \$body = '" . $req->body . "';\n"); |
58
|
0
|
|
|
|
|
|
print($handle "\n"); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
print($handle qq(die("Need a Mojo script as first argument.") unless -x \$ARGV[0];\n\n)); |
61
|
0
|
|
|
|
|
|
print($handle qq(my \@runme = (shift(\@ARGV));\n)); |
62
|
0
|
|
|
|
|
|
print($handle qq(\@runme = (\$^X, "-d", \@runme) if "-d" eq \$ARGV[0];\n)); |
63
|
0
|
|
|
|
|
|
print($handle "\n"); |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
print($handle qq(my \@exec = ( |
66
|
|
|
|
|
|
|
\@runme, |
67
|
|
|
|
|
|
|
"get", |
68
|
|
|
|
|
|
|
"-v", |
69
|
|
|
|
|
|
|
"-M", |
70
|
|
|
|
|
|
|
\$method, |
71
|
|
|
|
|
|
|
"-c", |
72
|
|
|
|
|
|
|
\$body, |
73
|
|
|
|
|
|
|
map({ ("-H", \"\$_:\$headers{\$_}\") } keys \%headers), |
74
|
|
|
|
|
|
|
\$url |
75
|
|
|
|
|
|
|
);\n)); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
print($handle qq(print("exec: " . join(" ", \@exec), "\\n");\n)); |
78
|
0
|
|
|
|
|
|
print($handle qq(exec(\@exec);\n)); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
close($handle); |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
$c->app->log->debug("SaveRequest: $name"); |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
return 1; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |