1use 5.008;
2use strict;
3use warnings;
4
5my $body_filename = $ARGV[0];
6my @command = @ARGV[1 .. $#ARGV];
7
8# read data
9my $body_size = -s $body_filename;
10$ENV{"CONTENT_LENGTH"} = $body_size;
11open(my $body_fh, "<", $body_filename) or die "Cannot open $body_filename: $!";
12my $body_data;
13defined read($body_fh, $body_data, $body_size) or die "Cannot read $body_filename: $!";
14close($body_fh);
15
16my $exited = 0;
17$SIG{"CHLD"} = sub {
18 $exited = 1;
19};
20
21# write data
22my $pid = open(my $out, "|-", @command);
23{
24 # disable buffering at $out
25 my $old_selected = select;
26 select $out;
27 $| = 1;
28 select $old_selected;
29}
30print $out $body_data or die "Cannot write data: $!";
31
32sleep 60; # is interrupted by SIGCHLD
33if (!$exited) {
34 close($out);
35 die "Command did not exit after reading whole body";
36}