Hi tdelwis77,
I think what you need to do is fork a child process to do the HTTP request, so that your callback can kill the UA but not the parent process.
I am not an expert on multiprocess programming, but the code below seems to do what you want, if I understand your need correctly.
use strict; use warnings; use feature 'say';
use LWP::UserAgent;
my $foo = 'bar';
$SIG{'INT'} = sub {
$foo = 'baz';
};
my $ua = LWP::UserAgent->new;
$ua->add_handler("response_data", sub {
if ( $foo eq 'baz' ) {
say 'killing UA now';
croak();
}
return 1;
});
my $url = 'http://ipv4.download.thinkbroadband.com/10MB.zip';
my $filename = "$0.zip";
my $forked = fork // die 'Could not fork!';
if ( $forked == 0 ) { # child
say "In child with $$";
my $req = HTTP::Request->new( GET => $url );
my $res = $ua->request( $req, $filename );
exit;
}
my $done = wait;
say "In parent: $done done";
exit;
(Note that this "aborts the download" but will leave the partially downloaded file on disk, as written with the second arg to $ua->request.)
Hope this helps!
The way forward always starts with a minimal test.
|