on_bodyrepeatedly called upon receipt of pieces. Returning false from on_bodycompletes the download.
sub my_http_request {
my $cb = pop;
my ($method, $url, %args) = @_;
croak("Unsupported: on_body") if $args{on_body};
croak("Unsupported: want_body_handle") if $args{want_body_handle};
my $max_to_read = delete($args{max_to_read});
my $data;
return http_request(
$method => $url,
%args,
on_body => sub {
$data .= $_[0];
return !defined($max_to_read) || length($data) < $max_to_read;
},
sub {
my (undef, $headers) = @_;
$cb->($data, $headers);
},
);
}
Use my_http_requestin the same way as http_request, except that it takes an optional parameter max_to_read.
For instance,
my $cb = AnyEvent->condvar();
my_http_request(
GET => 'http://...',
...
max_to_read => ...,
$cb,
);
my ($data, $headers) = $cb->recv();
For instance,
my $done = AnyEvent->condvar();
my_http_request(
GET => 'http://...',
...
max_to_read => ...,
sub {
my ($data, $headers) = @_;
...
$done->send();
},
);
$done->recv();
source
share