Subject: | Redirects do not update the URI |
If an URL request gets redirected (302), $agent->uri() does return the original and not the redirected value. The attached lines need to be added to Mechanize.pm :
# Keep track of the last uri redirected to :
sub redirect_ok {
$_[0]->{redirected_uri} = $_[1]->uri;
1
};
sub request {
my $self = shift;
my $request = shift;
$request->header( Referer => $self->{last_uri} ) if $self->{last_uri};
while ( my($key,$value) = each %WWW::Mechanize::headers ) {
$request->header( $key => $value );
}
$self->{req} = $request;
# added the line below
$self->{redirected_uri} = $request->uri;
$self->{res} = $self->SUPER::request( $request, @_ );
# These internal hash elements should be dropped in favor of
# the accessors soon. -- 1/19/03
$self->{status} = $self->{res}->code;
$self->{base} = $self->{res}->base;
$self->{ct} = $self->{res}->content_type || "";
$self->{content} = $self->{res}->content;
if ( $self->{res}->is_success ) {
$self->{last_uri} = $self->{uri};
# commented out the line below
#$self->{uri} = $request->uri;
# added the line below
$self->{uri} = $self->{redirected_uri};
}
if ( $self->is_html ) {
$self->{forms} = [ HTML::Form->parse($self->{content}, $self->{res}->base) ];
$self->{form} = @{$self->{forms}} ? $self->{forms}->[0] : undef;
$self->{links} = $self->extract_links();
}
return $self->{res};
}