possibly fix intermittent "could not receive data from server: Bad file
[freeside.git] / FS / FS / Mason / Request.pm
index 95c8027..0d21df4 100644 (file)
@@ -24,7 +24,7 @@ sub new {
 
     my %opt = @_;
     my $mode = $superclass =~ /Apache/i ? 'apache' : 'standalone';
-    freeside_setup($opt{'comp'}, $mode);
+    $class->freeside_setup($opt{'comp'}, $mode);
 
     $class->SUPER::new(@_);
 
@@ -33,9 +33,27 @@ sub new {
 #override alter_superclass ala RT::Interface::Web::Request ??
 # for Mason 1.39 vs. Perl 5.10.0
 
-sub freeside_setup {
+my $protect_fds;
 
-    my( $filename, $mode ) = @_;
+sub freeside_setup {
+    my( $class, $filename, $mode ) = @_;
+
+    #from rt/bin/webmux.pl(.in)
+    if ( !$protect_fds && $ENV{'MOD_PERL'} && exists $ENV{'MOD_PERL_API_VERSION'}
+        && $ENV{'MOD_PERL_API_VERSION'} >= 2
+    ) {
+        # under mod_perl2, STDIN and STDOUT get closed and re-opened,
+        # however they are not on FD 0 and 1.  In this case, the next
+        # socket that gets opened will occupy one of these FDs, and make
+        # all system() and open "|-" calls dangerous; for example, the
+        # DBI handle can get this FD, which later system() calls will
+        # close by putting garbage into the socket.
+        $protect_fds = [];
+        push @{$protect_fds}, IO::Handle->new_from_fd(0, "r")
+            if fileno(STDIN) != 0;
+        push @{$protect_fds}, IO::Handle->new_from_fd(1, "w")
+            if fileno(STDOUT) != 1;
+    }
 
     if ( $filename =~ qr(/REST/\d+\.\d+/NoAuth/) ) {
 
@@ -52,7 +70,8 @@ sub freeside_setup {
     } else {
 
       package HTML::Mason::Commands;
-      use vars qw( $cgi $p $fsurl );
+      use vars qw( $cgi $p $fsurl ); # $lh ); #not using /mt
+      use Encode;
       use FS::UID qw( cgisuidsetup );
       use FS::CGI qw( popurl rooturl );
 
@@ -72,6 +91,15 @@ sub freeside_setup {
         die "unknown mode $mode";
       }
 
+    #
+    foreach my $param ( $cgi->param ) {
+      my @values = $cgi->param($param);
+      next if $cgi->uploadInfo($values[0]);
+      #warn $param;
+      @values = map decode(utf8=>$_), @values;
+      $cgi->param($param, @values);
+    }
+    
   }
 
 }