AnyEvent-UserAgent

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

         }
      },
      "configure" : {
         "requires" : {
            "ExtUtils::MakeMaker" : "6.64"
         }
      },
      "runtime" : {
         "requires" : {
            "AnyEvent::HTTP" : "0",
            "HTTP::Cookies" : "0",
            "HTTP::Message" : "0",
            "Moo" : "0",
            "namespace::clean" : "0"
         }
      },
      "test" : {
         "requires" : {
            "AnyEvent" : "0",
            "HTTP::Request::Common" : "0",
            "Test::Deep" : "0",

META.yml  view on Meta::CPAN

  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: '1.4'
name: AnyEvent-UserAgent
no_index:
  directory:
    - t
    - inc
    - t
requires:
  AnyEvent::HTTP: '0'
  HTTP::Cookies: '0'
  HTTP::Message: '0'
  Moo: '0'
  namespace::clean: '0'
resources:
  bugtracker: http://github.com/AdCampRu/anyevent-useragent/issues
  license: http://dev.perl.org/licenses/
  repository: http://github.com/AdCampRu/anyevent-useragent
version: '0.09'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'

Makefile.PL  view on Meta::CPAN

	},
	TEST_REQUIRES      => {
		'AnyEvent'              => 0,
		'HTTP::Request::Common' => 0,
		'Test::Deep'            => 0,
		'Test::More'            => 0,
	},
	PREREQ_PM          => {
		'namespace::clean'      => 0,
		'AnyEvent::HTTP'        => 0,
		'HTTP::Cookies'         => 0,
		'HTTP::Message'         => 0,
		'Moo'                   => 0,
	},
	META_MERGE         => {
		resources => {
			license     => 'http://dev.perl.org/licenses/',
			repository  => 'http://github.com/AdCampRu/anyevent-useragent',
			bugtracker  => 'http://github.com/AdCampRu/anyevent-useragent/issues',
		},
		no_index  => {directory => ['t']},

README  view on Meta::CPAN

NAME

AnyEvent::UserAgent - AnyEvent::HTTP OO-wrapper.

DESCRIPTION

AnyEvent::UserAgent is a OO-wrapper around AnyEvent::HTTP with cookies
support by HTTP::Cookies. Also request callback receives response as
HTTP::Response object.

INSTALLATION

To install this module, run the following commands:

    perl Makefile.PL
    make
    make test
    make install

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN

package AnyEvent::UserAgent;

# This module based on original AnyEvent::HTTP::Simple module by punytan
# (punytan@gmail.com): http://github.com/punytan/AnyEvent-HTTP-Simple

use Moo;

use AnyEvent::HTTP ();
use HTTP::Cookies ();
use HTTP::Request ();
use HTTP::Request::Common ();
use HTTP::Response ();

use namespace::clean;

our $VERSION = '0.09';


has agent              => (is => 'rw', default => sub { $AnyEvent::HTTP::USERAGENT . ' AnyEvent-UserAgent/' . $VERSION });

has cookie_jar         => (is => 'rw', default => sub { HTTP::Cookies->new });

has max_redirects      => (is => 'rw', default => sub { 5 });

has inactivity_timeout => (is => 'rw', default => sub { 20 });

has request_timeout    => (is => 'rw', default => sub { 0 });

my @OPTIONS = qw(
	proxy tls_ctx session timeout on_prepare tcp_connect on_header on_body
	want_body_handle persistent keepalive handle_params

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN

	my $res = HTTP::Response->new(delete($hdrs->{Status}), delete($hdrs->{Reason}));

	$res->request($req);
	$res->previous($prev) if $prev;

	delete($hdrs->{URL});
	if (defined($hdrs->{HTTPVersion})) {
		$res->protocol('HTTP/' . delete($hdrs->{HTTPVersion}));
	}
	if (my $hdr = $hdrs->{'set-cookie'}) {
		# Split comma-concatenated "Set-Cookie" values.
		# Based on RFC 6265, section 4.1.1.
		local @_ = split(/,([\w.!"'%\$&*+-^`]+=)/, ',' . $hdr);
		shift();
		my @val;
		push(@val, join('', shift(), shift())) while @_;
		$hdrs->{'set-cookie'} = \@val;
	}
	if (keys(%$hdrs)) {
		$res->header(%$hdrs);
	}

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN

    $ua->get('http://example.com/', sub {
        my ($res) = @_;
        print(Dumper($res, $ua->cookie_jar));
        $cv->send();
    });
    $cv->recv();

=head1 DESCRIPTION

AnyEvent::UserAgent is a OO-wrapper around L<AnyEvent::HTTP> with cookies
support by L<HTTP::Cookies>. Also request callback receives response as
L<HTTP::Response> object.

=head1 ATTRIBUTES

=head2 agent

The product token that is used to identify the user agent on the network. The
agent value is sent as the C<User-Agent> header in the requests.

=head2 cookie_jar

The cookie jar object to use. The only requirement is that the cookie jar object
must implement the C<extract_cookies($req)> and C<add_cookie_header($res)>
methods. These methods will then be invoked by the user agent as requests are
sent and responses are received. Normally this will be a L<HTTP::Cookies> object
or some subclass. Default cookie jar is the L<HTTP::Cookies> object.

=head2 inactivity_timeout

Maximum time in seconds in which connection can be inactive before getting
closed. Default timeout value is C<20>. Setting the value to C<0> will allow
connections to be inactive indefinitely.

=head2 max_redirects

Maximum number of redirects the user agent will follow before it gives up.

lib/AnyEvent/UserAgent.pm  view on Meta::CPAN

=head1 LIMITATIONS

Because of the handling of redirections done by this module as well as
L<AnyEvent::HTTP>, if you are connecting to a web server that does not implement
persistent connections (which is common for test servers) then you should pass a
C<persistent => 0> option to the C<AnyEvent::UserAgent> constructor otherwise
some requests might fail.

=head1 SEE ALSO

L<AnyEvent::HTTP>, L<HTTP::Cookies>, L<HTTP::Request::Common>, L<HTTP::Request>,
L<HTTP::Response>.

=head1 SUPPORT

=over 4

=item * Repository

L<http://github.com/AdCampRu/anyevent-useragent>

t/06-cookies.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;
use Test::Deep;

use AnyEvent ();
use AnyEvent::UserAgent ();


my $SetCookieHeader = '';

{
	no warnings 'prototype';
	no warnings 'redefine';

	*AnyEvent::HTTP::http_request = sub {
		my $cb = pop();

		$cb->('', {
			Status       => 200,
			Reason       => 'OK',
			'set-cookie' => $SetCookieHeader,
		});
	};
}

my $ua = AnyEvent::UserAgent->new;
my $cv;
my $val;

# One simple cookie
$cv  = AE::cv;
$val = 'key=val; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/; domain=.example.com';

$SetCookieHeader = $val;
$ua->get('http://example.com/', sub {
	my ($res) = @_;

	is $res->header('set-cookie'), $val;

	$cv->send();
});

$cv->recv();

# Two simple cookies
$val = ['key1=val1; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/; domain=.example.com',
        'key2=val2; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/; domain=.example.com'];
$cv  = AE::cv;

$SetCookieHeader = join(',', @$val);
$ua->get('http://example.com/', sub {
	my ($res) = @_;

	cmp_bag [$res->header('set-cookie')], $val;

	$cv->send();
});
$cv->recv();

# One cookie with non-alphanumeric name
$cv  = AE::cv;
$val = 'key1.key2=val; expires=Tue, 19-Jan-2038 03:14:07 GMT; path=/; domain=.example.com';

$SetCookieHeader = $val;
$ua->get('http://example.com/', sub {
	my ($res) = @_;

	is $res->header('set-cookie'), $val;

	$cv->send();
});

$cv->recv();



( run in 0.740 second using v1.01-cache-2.11-cpan-e9199f4ba4c )