AnyEvent-Curl-Multi
view release on metacpan or search on metacpan
lib/AnyEvent/Curl/Multi.pm view on Meta::CPAN
=item timeout => PERIOD
Specified a timeout for the request. If your WWW::Curl is linked against
libcurl 7.16.2 or later, this value can be specified in fractional seconds (ms
resolution). Otherwise, the value must be specified in whole seconds.
=item proxy => HOST[:PORT]
Specifies a proxy host/port, separated by a colon. (The port number is optional.)
=item max_redirects => COUNT
Specifies the maximum number of HTTP redirects that will be followed. Set to
0 to disable following redirects.
=back
The request() method returns an object of class AnyEvent::Curl::Multi::Handle.
This object can be used later to cancel the request; see "Canceling requests",
below.
Calling $handle->cv() will return an AnyEvent condvar that you can use as usual
(e.g., recv() or cb()) to retrieve response results, or that will croak if an
error occurs. See L<AnyEvent> for details on condvars.
=head2 Callbacks
Instead of using condvars, you may register interest in the following events
using the client's reg_cb() method (see Object::Event for more details on
reg_cb()):
=over
=item response => $cb->($client, $request, $response, $stats);
Fired when a response is received. (This doesn't imply that the response is
HTTP OK, so you should examine the response to determine whether there was
an HTTP error of some sort.)
The arguments sent to your callback will be the client object, the original
request (untampered with), the response (as an HTTP::Response object), and a
hashref containing some interesting statistics.
=item error => $cb->($client, $request, $errmsg, $stats);
Fired when an error is received.
The arguments sent to your callback will be the client object, the original
request (untampered with), the error message, and a hashref containing some
interesting statistics. (If the error was other than a timeout, the statistics
may be invalid.)
=back
=cut
sub new {
my $class = shift;
my $self = $class->SUPER::new(
multi_h => WWW::Curl::Multi->new,
state => {},
timer_w => undef,
io_w => {},
queue => [],
max_concurrency => 0,
max_redirects => 0,
timeout => undef,
proxy => undef,
debug => undef,
ipresolve => undef,
@_
);
if (! $MS_TIMEOUT_SUPPORTED
&& $self->{timeout}
&& $self->{timeout} != int($self->{timeout})) {
croak "Subsecond timeout resolution is not supported by your " .
"libcurl version. Upgrade to 7.16.2 or later.";
}
return bless $self, $class;
}
sub request {
my $self = shift;
my ($req, %opts) = @_;
my $easy_h;
if ($req->isa("HTTP::Request")) {
# Convert to WWW::Curl::Easy
$easy_h = $self->_gen_easy_h($req, %opts);
} else {
croak "Unsupported request type";
}
# Initialize easy curl handle
my $id = refaddr $easy_h;
my ($response, $header);
$easy_h->setopt(CURLOPT_WRITEDATA, \$response);
$easy_h->setopt(CURLOPT_WRITEHEADER, \$header);
$easy_h->setopt(CURLOPT_PRIVATE, $id);
my $obj = {
easy_h => $easy_h,
req => $req,
response => \$response,
header => \$header,
cv => AE::cv,
};
push @{$self->{queue}}, $obj;
$self->_dequeue;
return bless $obj, 'AnyEvent::Curl::Multi::Handle';
}
sub _dequeue {
( run in 2.048 seconds using v1.01-cache-2.11-cpan-0bb4e1dffa6 )