- my $in;
- my $err = $env->{'psgi.errors'};
- if (fileno($input) >= 0) { # FIXME untested
- $in = $input;
- } else {
- $in = IO::File->new_tmpfile;
- while (1) {
- my $r = $input->read($buf, 8192);
- unless (defined $r) {
- $err->print('error reading input: ', $!, "\n");
- return r(500);
- }
- last if ($r == 0);
- $in->write($buf);
- }
- $in->flush;
- $in->sysseek(0, SEEK_SET);
- }
- my $out = IO::File->new_tmpfile;
- my $pid = fork; # TODO: vfork under Linux...
- unless (defined $pid) {
- $err->print('error forking: ', $!, "\n");
- return r(500);
- }
- if ($pid == 0) {
- # GIT_HTTP_EXPORT_ALL, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL
- # may be set in the server-process and are passed as-is
- foreach my $name (qw(QUERY_STRING
- REMOTE_USER REMOTE_ADDR
- HTTP_CONTENT_ENCODING
- CONTENT_TYPE
- SERVER_PROTOCOL
- REQUEST_METHOD)) {
- my $val = $env->{$name};
- $ENV{$name} = $val if defined $val;
- }
- # $ENV{GIT_PROJECT_ROOT} = $git->{git_dir};
- $ENV{GIT_HTTP_EXPORT_ALL} = '1';
- $ENV{PATH_TRANSLATED} = "$git->{git_dir}/$path";
- dup2(fileno($in), 0) or die "redirect stdin failed: $!\n";
- dup2(fileno($out), 1) or die "redirect stdout failed: $!\n";
- my @cmd = qw(git http-backend);
- exec(@cmd) or die 'exec `' . join(' ', @cmd). "' failed: $!\n";