The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AnyEvent::Net::Curl::Queued::Easy - Net::Curl::Easy wrapped by Any::Moose

VERSION

version 0.019

SYNOPSIS

    package MyIEDownloader;
    use common::sense;

    use Any::Moose;
    use Net::Curl::Easy qw(/^CURLOPT_/);

    extends 'AnyEvent::Net::Curl::Queued::Easy';

    after init => sub {
        my ($self) = @_;

        $self->setopt(CURLOPT_ENCODING,         '');
        $self->setopt(CURLOPT_FOLLOWLOCATION,   1);
        $self->setopt(CURLOPT_USERAGENT,        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)');
        $self->setopt(CURLOPT_VERBOSE,          1);
    };

    after finish => sub {
        my ($self, $result) = @_;

        if ($self->has_error) {
            printf "error downloading %s: %s\n", $self->final_url, $result;
        } else {
            printf "finished downloading %s: %d bytes\n", $self->final_url, length ${$self->data};
        }
    };

    around has_error => sub {
        my $orig = shift;
        my $self = shift;

        return 1 if $self->$orig(@_);
        return 1 if $self->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE) =~ m{^5[0-9]{2}$};
    };

    no Any::Moose;
    __PACKAGE__->meta->make_immutable;

    1;

DESCRIPTION

The class you should overload to fetch stuff your own way.

ATTRIBUTES

curl_result

libcurl return code (Net::Curl::Easy::Code).

data

Receive buffer.

force

Force request processing, despite uniqueness signature.

Header buffer.

http_response

Optionally encapsulate the response in HTTP::Response.

initial_url

URL to fetch (string).

final_url

Final URL (after redirections).

queue

AnyEvent::Net::Curl::Queued circular reference.

sha

Uniqueness detection helper. Setup via sign and access through unique.

res

Encapsulated HTTP::Response instance, if "http_response" was set.

retry

Number of retries (default: 10).

stats

AnyEvent::Net::Curl::Queued::Stats instance.

use_stats

Set to true to enable stats computation. Note that extracting libcurl time/size data degrades performance slightly.

on_init

Callback you can define instead of extending the init method. Almost the same as after init => sub { ... }

on_finish

Callback you can define instead of extending the finish method. Almost the same as after finish => sub { ... }

METHODS

unique()

Returns the unique signature of the request. By default, the signature is derived from Digest::SHA of the initial_url.

sign($str)

Use $str to compute the unique value. Useful to successfully enqueue POST parameters.

init()

Initialize the instance. We can't use the default BUILD method as we need the initialization to be done after the instance is in the queue.

You are supposed to build your own stuff after/around/before this method using method modifiers.

has_error()

Error handling: if has_error returns true, the request is re-enqueued (until the retries number is exhausted).

You are supposed to build your own stuff after/around/before this method using method modifiers. For example, to retry on server error (HTTP 5xx response code):

    around has_error => sub {
        my $orig = shift;
        my $self = shift;

        return 1 if $self->$orig(@_);
        return 1 if $self->getinfo('response_code') =~ m{^5[0-9]{2}$};
    };

finish($result)

Called when the download is finished. $result holds the Net::Curl::Easy::Code.

You are supposed to build your own stuff after/around/before this method using method modifiers.

clone()

Clones the instance, for re-enqueuing purposes.

You are supposed to build your own stuff after/around/before this method using method modifiers. For example, to implement proper POST re-enqueuing:

    has method => (is => 'ro', isa => 'Str', default => 'GET');
    has post_content => (is => 'ro', isa => 'Str');

    ...;

    around clone => sub {
        my $orig = shift;
        my $self = shift;
        my $param = shift;

        $param->{method} = $self->method;
        $param->{post_content} = $self->post_content;

        return $self->$orig($param);
    };

setopt(OPTION => VALUE [, OPTION => VALUE])

Extends Net::Curl::Easy setopt(), allowing option lists:

    $self->setopt(
        CURLOPT_ENCODING,         '',
        CURLOPT_FOLLOWLOCATION,   1,
        CURLOPT_USERAGENT,        'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        CURLOPT_VERBOSE,          1,
    );

Or even shorter:

    $self->setopt(
        encoding            => '',
        followlocation      => 1,
        useragent           => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        verbose             => 1,
    );

Complete list of options: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

getinfo(VAR_NAME [, VAR_NAME])

Extends Net::Curl::Easy getinfo() so it is able to get several variables at once; HashRef parameter under void context will fill respective values in the HashRef:

    my $x = {
        content_type    => 0,
        speed_download  => 0,
        primary_ip      => 0,
    };
    $self->getinfo($x);

HashRef parameter will return another HashRef:

    my $x = $self->getinfo({
        content_type    => 0,
        speed_download  => 0,
        primary_ip      => 0,
    });

ArrayRef parameter will return a list:

    my ($content_type, $speed_download, $primary_ip) =
        $self->getinfo([qw(content_type speed_download primary_ip)]);

Complete list of options: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

SEE ALSO

AUTHOR

Stanislaw Pusep <stas@sysd.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Stanislaw Pusep.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.