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

NAME

UniEvent::HTTP::UserAgent - non blocking HTTP user agent

SYNOPSIS

    my $ua = UniEvent::HTTP::UserAgent->new;
    my $uri = 'https://crazypanda.ru';
    my $next; $next = sub {
        $ua->request({
            uri               => $uri,
            response_callback => sub {
                my (undef, $res, $err) = @_;
                die $err if $err;
                my $body = say $response->body;
                # extract somehow next link
                $uri = ...;
                $next = undef unless $uri;
            },
        });
    };
    $next->(), UE::Loop->default->run while $uri;


    my $saved_cookies = $ua->to_string;
    # save cookies to file or database, and load them later

    my $ua = UniEvent::HTTP::UserAgent->new({
        serialized => $saved_cookies,
        identity   => 'my-identity',
    });

DESCRIPTION

The user agent binds together low-level http request API and Protocol::HTTP::CookieJar. That way it is possible to do proper browser-like navigation on the Web, correcly handling cookie on transitions between pages including redirects.

In other words with UserAgent it possible to have web browsing sessions which outlive perl process by persisting session (Protocol::HTTP::CookieJar) on a storage, or examining or modifying intermediate cookies.

METHODS

new(\%config, [$loop = default])

Creates new UserAgent object which will use $loop UniEvent::Loop.

%config includes:

serialized

String of the previously serialized Protocol::HTTP::CookieJar.

identity

How to identity UserAgent to the remote server. The default is something like

    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36 UniEvent-HTTP/1.0
ssl_ctx

SSL context, see Net::SSLeay and "use_ssl" in UniEvent::Stream.

proxy

The uri of proxy for making http requests. Currenlty, only socks-proxy is supported

request($uri [, $context_uri = $uri, $top_level = true])

Make a request to the specified $uri, possibly injecting the required cookies from the stored sesssion.

See "populate" in Protocol::HTTP::CookieJar for the meaning of $context_uri and $top_level.

UniEvent::HTTP::Client is returned.

to_string([$include_session = false])

Helper method to stringify current CookieJar. See "to_string" in Protocol::HTTP::CookieJar for details.

identity([$string])

Get/set UserAgent identity.

proxy([$uri])

Get/set proxy uri, see URI::XS.

ssl_ctx([$value])

Get/set ssl context, see Net::SSLeay and "use_ssl" in UniEvent::Stream.

Get/set CookieJar.

Return assiciated Protocol::HTTP::CookieJar object. It is possible to examine, query and modify current cookies via this object.