Skip to content

Commit

Permalink
LWP::UserAgent now implements a 'max_redirect' attribute with a
Browse files Browse the repository at this point in the history
default value of 7.  This should also fix the problem where
redirects to the same URL to get a cookie set did not work.
Based on a patch by Sean M. Burke C<sburke@cpan.org>.
  • Loading branch information
gisle committed Oct 15, 2003
1 parent 8b2d9c6 commit 0ba0cac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Changes
Expand Up @@ -27,6 +27,11 @@
method to be "GET". This is what most browsers do. Based on
a patch contributed by Tom Hughes <thh@cyberscience.com>.

LWP::UserAgent now implements a 'max_redirect' attribute with a
default value of 7. This should also fix the problem where
redirects to the same URL to get a cookie set did not work.
Based on a patch by Sean M. Burke C<sburke@cpan.org>.



2003-10-14 Gisle Aas <gisle@ActiveState.com>
Expand Down
53 changes: 35 additions & 18 deletions lib/LWP/UserAgent.pm
@@ -1,4 +1,4 @@
# $Id: UserAgent.pm,v 2.10 2003/10/15 13:16:10 gisle Exp $
# $Id: UserAgent.pm,v 2.11 2003/10/15 13:31:27 gisle Exp $

package LWP::UserAgent;
use strict;
Expand Down Expand Up @@ -117,7 +117,7 @@ use vars qw(@ISA $VERSION);

require LWP::MemberMixin;
@ISA = qw(LWP::MemberMixin);
$VERSION = sprintf("%d.%03d", q$Revision: 2.10 $ =~ /(\d+)\.(\d+)/);
$VERSION = sprintf("%d.%03d", q$Revision: 2.11 $ =~ /(\d+)\.(\d+)/);

use HTTP::Request ();
use HTTP::Response ();
Expand Down Expand Up @@ -155,6 +155,7 @@ methods described below:
use_eval 1
parse_head 1
max_size undef
max_redirect 7
cookie_jar undef
conn_cache undef
protocols_allowed undef
Expand Down Expand Up @@ -188,6 +189,8 @@ sub new
my $parse_head = delete $cnf{parse_head};
$parse_head = 1 unless defined $parse_head;
my $max_size = delete $cnf{max_size};
my $max_redirect = delete $cnf{max_redirect};
$max_redirect = 7 unless defined $max_redirect;
my $env_proxy = delete $cnf{env_proxy};

my $cookie_jar = delete $cnf{cookie_jar};
Expand Down Expand Up @@ -219,15 +222,16 @@ sub new
}

my $self = bless {
from => $from,
timeout => $timeout,
use_eval => $use_eval,
parse_head => $parse_head,
max_size => $max_size,
proxy => undef,
no_proxy => [],
protocols_allowed => $protocols_allowed,
protocols_forbidden => $protocols_forbidden,
from => $from,
timeout => $timeout,
use_eval => $use_eval,
parse_head => $parse_head,
max_size => $max_size,
max_redirect => $max_redirect,
proxy => undef,
no_proxy => [],
protocols_allowed => $protocols_allowed,
protocols_forbidden => $protocols_forbidden,
requests_redirectable => $requests_redirectable,
}, $class;

Expand Down Expand Up @@ -501,10 +505,9 @@ sub request
my $count = 0;
my $r = $response;
while ($r) {
if (++$count > 13 ||
$r->request->url->as_string eq $referral_uri->as_string) {
if (++$count > $self->{max_redirect}) {
$response->header("Client-Warning" =>
"Redirect loop detected");
"Redirect loop detected (max_redirect = $self->{max_redirect})");
return $response;
}
$r = $r->previous;
Expand Down Expand Up @@ -993,12 +996,26 @@ which means that there is no limit. If the returned response content
is only partial, because the size limit was exceeded, then a
"Client-Aborted" header will be added to the response.
=item $ua->max_redirect([$n])
This reads or sets the object's limit of how many times it will obey
redirection responses in a given C<< $ua->get(...) / $ua->put(...) /
$ua->head(...) / $ua->request(...) >> / etc. cycle.
By default, the value is 7. This means that if you call C<<
$ua->get($url) >> and the response is a redirect elsewhere which is in
turn a redirect, and so on seven times, then LWP gives up after that
seventh request. Otherwise (like if the seventh or earlier isn't a
redirect), then the response from C<< $ua->get($url) >> redirects
are followed.
=cut

sub timeout { shift->_elem('timeout', @_); }
sub from { shift->_elem('from', @_); }
sub parse_head { shift->_elem('parse_head',@_); }
sub max_size { shift->_elem('max_size', @_); }
sub timeout { shift->_elem('timeout', @_); }
sub from { shift->_elem('from', @_); }
sub parse_head { shift->_elem('parse_head', @_); }
sub max_size { shift->_elem('max_size', @_); }
sub max_redirect { shfit->_elem('max_redirect', @_); }

sub cookie_jar {
my $self = shift;
Expand Down

0 comments on commit 0ba0cac

Please sign in to comment.