]> Sergey Matveev's repositories - public-inbox.git/commitdiff
git-http-backend: extract input_to_file function
authorEric Wong <e@80x24.org>
Fri, 26 Feb 2016 01:57:54 +0000 (01:57 +0000)
committerEric Wong <e@80x24.org>
Fri, 26 Feb 2016 02:37:06 +0000 (02:37 +0000)
This will allow us to more easily read and test later.

lib/PublicInbox/GitHTTPBackend.pm

index 58799707af73c49bd806eeed6cdcb5e959223993..f8446aa0d16151fc120ff42332e21cbb07e385f3 100644 (file)
@@ -135,18 +135,7 @@ sub serve_smart {
        if (fileno($input) >= 0) {
                $in = $input;
        } else { # FIXME untested
-               $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);
+               $in = input_to_file($env) or return r(500);
        }
        my ($rpipe, $wpipe);
        unless (pipe($rpipe, $wpipe)) {
@@ -249,4 +238,25 @@ sub serve_smart {
        }
 }
 
+# FIXME: untested, our -httpd _always_ gives a real file handle
+sub input_to_file {
+       my ($env) = @_;
+       my $in = IO::File->new_tmpfile;
+       my $input = $env->{'psgi.input'};
+       my $buf;
+       while (1) {
+               my $r = $input->read($buf, 8192);
+               unless (defined $r) {
+                       my $err = $env->{'psgi.errors'};
+                       $err->print("error reading input: $!\n");
+                       return;
+               }
+               last if ($r == 0);
+               $in->write($buf);
+       }
+       $in->flush;
+       $in->sysseek(0, SEEK_SET);
+       return $in;
+}
+
 1;