How to tell if download is cancelled
am 26.11.2008 16:05:34 von Mark KnoopHi
I have a CGI script running on IIS 6.0 which handles downloads by reading in
the file and printing it to STDOUT.
Part of the reason for doing this was to keep track of the bytes actually
downloaded.
This works fine but I have found that under usual circumstances the CGI
script will continue to send the whole file even if the download is
cancelled.
If I put a 1 second sleep in between prints then it will stop before the
whole file is sent but it still takes quite a while to stop.
Ideally I would like the CGI script to die as soon as the download is
cancelled.
I have tried using an IO::Socket connected to \*STDOUT and checking
peername() but that just hung even when the browser was connected.
It also appears there is some IIS isClientConnected property but I am not
sure how to get at that from Perl without using Server-Side PerlScript and I
know nothing about that.
Any ideas as to the best approach?
Thanks
Mark
Example Code follows:
#########################
use strict;
use bytes;
use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
use Log::Log4perl;
#use IO::Socket;
$|=1;
Log::Log4perl->init("D:\\conf\\log4perl.conf");
my $log = Log::Log4perl->get_logger("testbyte");
my $testfile = 'D:\path\to\content\Some_Content.3gp';
#my $stdout = IO::Socket->new;
#if ($stdout->fdopen(fileno(STDOUT),"w")) { $log->info("Connected IO::Socket
to STDOUT : " . $stdout->peername() ); }
open(FILE, "< $testfile") or $log->logdie ("Couldn't open file $testfile");
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat(FILE);
my $tmpfile = 'Some_Content.3gp';
print "Content-type: video/3gpp\n";
print "Content-length: $size\n";
print "Content-disposition: attachment;filename=$tmpfile\n\n";
binmode(FILE);
my $bytes_delivered = 0;
while(
$bytes_delivered += length $_;
$log->info("Bytes delivered = " . $bytes_delivered);
#if ( $stdout->peername() ) { $log->info("Still connected");
$stdout->print($_); } else { $log->info("Not connected");}
print;
#sleep (1);
}
close FILE;
exit;
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs