AnyEvent-UserAgent
view release on metacpan or search on metacpan
lib/AnyEvent/UserAgent.pm view on Meta::CPAN
if ($code == 301 || $code == 302 || $code == 303 || $code == 307 || $code == 308) {
$self->_redirect($req, $opts, $code, $res, $count, $cb);
}
else {
$cb->($res);
}
}
sub _redirect {
my ($self, $req, $opts, $code, $prev, $count, $cb) = @_;
unless (defined($count) ? $count : ($count = $opts->{max_redirects})) {
$prev->header('client-warning' => 'Redirect loop detected (max_redirects = ' . $opts->{max_redirects} . ')');
$cb->($prev);
return;
}
my $meth = $req->method;
my $proto = $req->uri->scheme;
my $uri = $prev->header('location');
$req = $req->clone();
$req->remove_header('cookie');
if (($code == 302 || $code == 303) && !($meth eq 'GET' || $meth eq 'HEAD')) {
$req->method('GET');
$req->content('');
$req->remove_content_headers();
}
{
# Support for relative URL for redirect.
# Not correspond to RFC.
local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
my $base = $prev->base;
$uri = $HTTP::URI_CLASS->new(defined($uri) ? $uri : '', $base)->abs($base);
}
$req->uri($uri);
if ($proto eq 'https' && $uri->scheme eq 'http') {
# Suppress 'Referer' header for HTTPS to HTTP redirect.
# RFC 2616, section 15.1.3.
$req->remove_header('referer');
}
$self->_request($req, $opts, sub {
$self->_response($req, @_, $prev, $count - 1, sub { return $cb->(@_); });
});
}
1;
__END__
=head1 NAME
AnyEvent::UserAgent - AnyEvent::HTTP OO-wrapper
=head1 SYNOPSIS
use AnyEvent::UserAgent;
use Data::Dumper;
my $ua = AnyEvent::UserAgent->new;
my $cv = AE::cv;
$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.
The default value is C<5>.
=head2 request_timeout
Maximum time in seconds to establish a connection, send the request and receive
a response. The request will be canceled when that time expires. Default timeout
value is C<0>. Setting the value to C<0> will allow the user agent to wait
indefinitely. The timeout will reset for every followed redirect.
=head2 Other attributes
The following attributes are supported and they are all passed as options to the
L<C<AnyEvent::HTTP::http_request>|AnyEvent::HTTP/METHODS> calls made by this
module: C<proxy>, C<tls_ctx>, C<session>, C<timeout>, C<on_prepare>,
C<tcp_connect>, C<on_header>, C<on_body>, C<want_body_handle>, C<persistent>,
C<keepalive>, C<handle_params>.
=head1 METHODS
( run in 2.044 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )