PERL CGI SCRIPT killed by apache on client abort

2011-03-27 Thread Przemysław Rejf

Hi,
My upload script which uses upload hook works really nice. After the 
upload ends it writes some debug stuff to log file and sends some info 
to other host.
When client aborts the upload the reminder of the script after hook is 
not executed and i get an error in my apache log:

[Sat Mar 26 14:53:09 2011] [error] [client xxx.xxx.xxx.xxx] (70014)End
+ of file found: Error reading request entity data, referer: http://lo
+calhost/upload
[download]
I'm thinking its the fault of apache, because without abort the script 
works just fine. Does anyone have any clue about it ? The script:

#!/usr/bin/perl -w
use strict;
use CGI;
use warnings;
use Digest::MD5 qw(md5_hex);
use POSIX qw/ceil/;
use POSIX 'setsid';
use LWP::Simple;

$CGI::DISABLE_UPLOADS = 0;
$CGI::POST_MAX = 1024 * 1024 * 200;

our $php_exec = '/usr/bin/php';
our $www_root = 'www_root';
our $SERVER_NUMBER = '1';

our $pid = 666;
our $data;
our $first_hook = 1;
our $sum_bytes = 0;
our $approx_file_size = 0;
our $original_filename = "";
our $storage_name = "";
our $db_path = "path where the file with number of stored files is";
our $upload_path = "path to storage folder";
our $debug_path = "path to log folder";
our $url = '';
our $content = '';

   #zmienna identyfikująca urzytkownika wyciągnięta z
+pola GET
   my $query_string = $ENV{QUERY_STRING};
   my $sessid = $query_string;
   my $upload_session = $query_string;
   $sessid =~ m/userid=([0-9]+)/i;
   our $userid = $1;

   $upload_session =~ m/us=([a-zA-Z0-9]+)/i;
   our $upload_sess = $1;

   my $line = 0;
   #zmiana aktualnej ilości plików przechowywanych na serwerze
   open(UPLOAD_DB, "+<", $db_path."upload_db") or die "Cannot open :
+$!";
   flock(UPLOAD_DB, 2);
   $line = ;
   our $filenumber = $line + 1;
   seek(UPLOAD_DB, 0, 0); truncate(UPLOAD_DB, 0);
   print UPLOAD_DB $filenumber;
   close UPLOAD_DB;

   our $dir = ceil($filenumber/5);
   unless (-d $upload_path.$dir)
   {
   mkdir $upload_path.$dir, 0777;
   #print $!;
   }
   $upload_path = $upload_path.$dir."/";
   our $logfile = $debug_path."hook_debug.txt";
   our $q = CGI->new(\&hook, $logfile, 0);

   sub hook
   {
   our ($filename, $buffer, $bytes_read, $logfile) = @_;
+
   $sum_bytes = $bytes_read;
   if($first_hook)
   {
   $approx_file_size = $ENV{CONTENT_LENGTH};
   #print $approx_file_size.'\n';
   $approx_file_size -= 197;
   $original_filename = $filename;
   $filename =~ s/^\s+|\s+$//g ;
   $storage_name = md5_hex($filename."salt666".$filenumbe
+r);

   open(UPLOADFILE, ">>", $upload_path.$storage_name );
   chmod 0777, $upload_path.$storage_name;
   binmode UPLOADFILE;
   print UPLOADFILE $buffer;
   close UPLOADFILE;
   $first_hook = 0;
   }else{
   open(DEBUG, ">>", $debug_path."abort_debug.txt" );
   binmode DEBUG;
   print DEBUG "BR: ";
   print DEBUG "$bytes_read\n";
   close DEBUG;

   open(UPLOADFILE, ">>", $upload_path.$storage_name );
   binmode UPLOADFILE;
   print UPLOADFILE $buffer;
   close UPLOADFILE;

   if (($buffer eq '') == 1)
   {
   unlink($upload_path.$storage_name);
   open(DEBUG, ">>", $debug_path."abort_debug.txt" );
   binmode DEBUG;
   print DEBUG "error file upload aborted\n";
   close DEBUG;
   exit 0;
   }
   }
   }
   open(DEBUG, ">>", $debug_path."end_debug.txt" );
   binmode DEBUG;
   print DEBUG "END\n#\n";
   close DEBUG;
   print "Content-type: text/html\n\n END";
   exit;

Thank for replies,
Best regards.


--
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




Re: PERL CGI SCRIPT killed by apache on client abort

2011-03-27 Thread Mike Williams
2011/3/26 Przemysław Rejf :
> Hi,
> My upload script which uses upload hook works really nice. After the upload
> ends it writes some debug stuff to log file and sends some info to other
> host.
> When client aborts the upload the reminder of the script after hook is not
> executed and i get an error in my apache log:
> [Sat Mar 26 14:53:09 2011] [error] [client xxx.xxx.xxx.xxx] (70014)End
> + of file found: Error reading request entity data, referer: http://lo
> +calhost/upload
> [download]
> I'm thinking its the fault of apache, because without abort the script works
> just fine. Does anyone have any clue about it ? The script:

Excuse me if I've grabbed the wrong end of the stick here, but it
looks to me like apache is doing the right thing.  If the user aborts
the upload then there is an error reading the data, which apache
reports.

>From a practical standpoint, if the user aborts the upload, they
should know that they aborted the upload and expect to see an error.
Furthermore, if they aborted the upload, it seems most likely that
they canceled the upload because they realized that they had selected
the wrong file and will try again with the correct file.

What difference does it make whether they get an error from your
script or from apache, if they expected an error and intend to try
again?

Mike

--
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




Re: PERL CGI SCRIPT killed by apache on client abort

2011-03-28 Thread Peter Scott
On Sat, 26 Mar 2011 15:56:08 +0100, Przemysław Rejf
wrote:

> My upload script which uses upload hook works really nice. After the
> upload ends it writes some debug stuff to log file and sends some info
> to other host.
> When client aborts the upload the reminder of the script after hook is
> not executed and i get an error in my apache log: [Sat Mar 26 14:53:09
> 2011] [error] [client xxx.xxx.xxx.xxx] (70014)End + of file found: Error
> reading request entity data, referer: http://lo +calhost/upload
> [download]
> I'm thinking its the fault of apache, because without abort the script
> works just fine. Does anyone have any clue about it ? The script:

Your script is invoked to read a file from the client.  If the client 
doesn't send it what else would you expect to happen?  How can it read a 
data stream that's not there?  It's not apache's fault, it's the client's 
fault for not finishing what it said it would do or your script's fault 
for not trapping an exception it needs to handle.




-- 
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=0137001274
http://www.oreillyschool.com/courses/perl3/

-- 
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/