App-cdnget
view release on metacpan or search on metacpan
lib/App/cdnget/Downloader.pm view on Meta::CPAN
lock($terminating);
return 0 if $terminating;
$terminating = 1;
};
App::cdnget::log_info("Downloaders terminating...");
my $gracefully = 0;
while (not $gracefully and not $App::cdnget::terminating_force)
{
$gracefully = $downloaderSemaphore->down_timed(3, $maxCount);
}
lock($terminated);
$terminated = 1;
App::cdnget::log_info("Downloaders terminated".($gracefully? " gracefully": "").".");
return 1;
}
sub terminating
{
lock($terminating);
return $terminating;
}
sub terminated
{
if (@_ > 0)
{
my $self = shift;
lock($self);
return defined($self->tid)? 0: 1;
}
lock($terminated);
return $terminated;
}
sub new
{
my $class = shift;
my ($uid, $path, $url, $hook) = @_;
while (not $downloaderSemaphore->down_timed(1))
{
if (terminating())
{
return;
}
}
if (terminating())
{
$downloaderSemaphore->up();
return;
}
lock(%uids);
return if exists($uids{$uid});
my $self = $class->SUPER();
$self->uid = $uid;
$self->path = $path;
$self->url = $url;
$self->hook = $hook;
$self->tid = undef;
{
lock($self);
my $thr = threads->create(\&run, $self) or $self->throw($!);
cond_wait($self);
unless (defined($self->tid))
{
App::cdnget::Exception->throw($thr->join());
}
$thr->detach();
}
$uids{$uid} = $self;
return $self;
}
sub DESTROY
{
my $self = shift;
$self->SUPER::DESTROY;
}
sub throw
{
my $self = shift;
my ($msg) = @_;
unless (ref($msg))
{
$msg = "Unknown" unless $msg;
$msg = "Downloader ".
"uid=".$self->uid." ".
"url=\"".shellmeta($self->url)."\" ".
"hook=\"".shellmeta($self->hook)."\" ".
$msg;
}
App::cdnget::Exception->throw($msg, 1);
}
sub processHook_img
{
my $self = shift;
my ($hook, $response, @params) = @_;
my $headers = $response->{_headers};
my $img;
given ($headers->content_type)
{
when ("image/png")
{
$img = GD::Image->newFromPngData($response->decoded_content) or $self->throw($!);
}
when ("image/jpeg")
{
$img = GD::Image->newFromJpegData($response->decoded_content) or $self->throw($!);
}
default
{
$self->throw("Unsupported content type for image");
}
}
given ($hook)
{
when (/^imgresize$/i)
{
$params[0] = $img->width unless defined($params[0]) and $params[0] > 0 and $params[0] <= 10000;
$params[1] = $img->height unless defined($params[1]) and $params[1] > 0 and $params[1] <= 10000;
lib/App/cdnget/Downloader.pm view on Meta::CPAN
{
$self->throw("Unsupported css hook");
}
}
return;
}
sub processHook_js
{
my $self = shift;
my ($hook, $response, @params) = @_;
my $headers = $response->{_headers};
$self->throw("Unsupported content type for js") unless $headers->content_type =~ /^(text\/javascript|text\/ecmascript|application\/javascript|application\/ecmascript|application\/x\-javascript)$/;
given ($hook)
{
when (/^jsminify$/i)
{
my $data = JavaScript::Minifier::XS::minify($response->decoded_content);
return ("Status: 200\r\nContent-Type: ".$headers->content_type."\r\nContent-Length: ".length($data)."\r\n", $data);
}
default
{
$self->throw("Unsupported js hook");
}
}
return;
}
sub processHook
{
my $self = shift;
my ($response) = @_;
my @params = split /\s+/, $self->hook;
my $hook = shift @params;
return unless defined($hook);
given ($hook)
{
when (/^img/i)
{
return $self->processHook_img($hook, $response, @params);
}
when (/^css/i)
{
return $self->processHook_css($hook, $response, @params);
}
when (/^js/i)
{
return $self->processHook_js($hook, $response, @params);
}
default
{
$self->throw("Unsupported hook");
}
}
return;
}
sub run
{
my $self = shift;
my $tid = threads->tid();
my $fh;
eval
{
$fh = FileHandle->new($self->path, ">") or $self->throw($!);
};
if ($@)
{
lock($self);
cond_signal($self);
return $@;
}
$self->tid = $tid;
do
{
lock($self);
cond_signal($self);
};
eval
{
my $max_size = undef;
if ($self->hook)
{
$max_size = 20*1024*1024;
}
$fh->binmode(":bytes") or $self->throw($!);
my $ua = LWP::UserAgent->new(agent => "p5-cdnget/${App::cdnget::VERSION}",
max_redirect => 1,
max_size => $max_size,
requests_redirectable => [],
timeout => 5);
my $response_header = sub
{
my ($response, $ua) = @_;
local ($/, $\) = ("\r\n")x2;
my $status = $response->{_rc};
my $headers = $response->{_headers};
$fh->print("Status: ".$status) or $self->throw($!);
$fh->print("Client-URL: ".$self->url) or $self->throw($!);
$fh->print("Client-Date: ".POSIX::strftime($App::cdnget::DTF_RFC822_GMT, gmtime)) or $self->throw($!);
for my $header (sort grep /^(Content\-|Location\:)/i, $headers->header_field_names())
{
$fh->print("$header: ", $headers->header($header)) or $self->throw($!);
}
$fh->print("") or $self->throw($!);
return 1;
};
my $content_cb = sub
{
my ($data, $response) = @_;
$fh->write($data, length($data)) or $self->throw($!);
$self->throw("Terminating") if $self->terminating;
return 1;
};
my %matchspec = (':read_size_hint' => $App::cdnget::CHUNK_SIZE);
unless ($self->hook)
{
$ua->add_handler(
( run in 1.142 second using v1.01-cache-2.11-cpan-39bf76dae61 )