joyproxy.pm: enforce size restrictions before actual downloading
This commit is contained in:
@@ -3,7 +3,7 @@ package joyproxy;
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings "all";
|
use warnings "all";
|
||||||
use lib qw(./lib ./vendor_perl);
|
use lib qw(./lib ./vendor_perl);
|
||||||
use Fcntl qw(O_RDONLY O_WRONLY);
|
use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
|
||||||
use HTTP::Tiny;
|
use HTTP::Tiny;
|
||||||
use URI::URL;
|
use URI::URL;
|
||||||
|
|
||||||
@@ -48,6 +48,7 @@ sub __urlencode($) {
|
|||||||
my $str = shift;
|
my $str = shift;
|
||||||
my $urlobj = url $str;
|
my $urlobj = url $str;
|
||||||
$str = $urlobj->as_string;
|
$str = $urlobj->as_string;
|
||||||
|
$urlobj = undef;
|
||||||
undef $urlobj;
|
undef $urlobj;
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
@@ -67,10 +68,10 @@ sub __dlfunc($) {
|
|||||||
$file = "/tmp/joyproxy/" . $file;
|
$file = "/tmp/joyproxy/" . $file;
|
||||||
$url = __urlencode($url);
|
$url = __urlencode($url);
|
||||||
|
|
||||||
my $http = HTTP::Tiny->new();
|
my $http = HTTP::Tiny->new(max_size => 10485760);
|
||||||
my $response = $http->mirror(
|
|
||||||
|
my $response = $http->get(
|
||||||
$url,
|
$url,
|
||||||
$file,
|
|
||||||
{
|
{
|
||||||
headers => {
|
headers => {
|
||||||
"Accept" => '*/*',
|
"Accept" => '*/*',
|
||||||
@@ -84,15 +85,27 @@ sub __dlfunc($) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if ($response->{success}) {
|
if ($response->{success}) {
|
||||||
my $filesize = (stat($file))[7];
|
sysopen (FILE, $file, O_WRONLY|O_CREAT|O_TRUNC) or do {
|
||||||
|
$http = undef;
|
||||||
if ($filesize > 10485760) {
|
$response = undef;
|
||||||
unlink $file;
|
|
||||||
return (undef, undef);
|
return (undef, undef);
|
||||||
} else {
|
};
|
||||||
return ($file, $filesize);
|
|
||||||
}
|
my $filesize = length($response->{content});
|
||||||
|
|
||||||
|
syswrite (FILE, $response->{content}, $filesize) or do {
|
||||||
|
$http = undef;
|
||||||
|
$response = undef;
|
||||||
|
return (undef, undef);
|
||||||
|
};
|
||||||
|
|
||||||
|
close FILE;
|
||||||
|
$http = undef;
|
||||||
|
$response = undef;
|
||||||
|
return ($file, $filesize);
|
||||||
} else {
|
} else {
|
||||||
|
$http = undef;
|
||||||
|
$response = undef;
|
||||||
return (undef, undef);
|
return (undef, undef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user