in reply to LWP redirect problem, produces malformed URL?

I believe this could be considered an LWP bug. It is triggered by the double "Location:" header.

When LWP is processing the redirect request, it gets the new URI from the Location header with this bit of code:

# And then we update the URL based on the Location:-header. my $referral_uri = $response->header('Location');
and since the "header()" method (in scalar mode) helpfully returns a comma separated list of all instances of the requested header, you end up with a weird URI.

There are several possible solutions:

  1. Fix the server to only give one Location header (probably hard), or
  2. Fix LWP and distribute to world immediately (really hard), or
  3. Fiddle with the headers in the middle of the redirect process (easy).
I would go with option #3.
Create a new class based on LWP::UserAgent and override the redirect_ok() method.

Here is some untested code that should do the trick:

package FixRedirectLWPUserAgent; ########################################################## # Derived from LWP::UserAgent # Handle redirect_ok method to fix double Location header. ########################################################## use strict; use warnings; use LWP::UserAgent; our @ISA = qw( LWP::UserAgent ); sub redirect_ok { my ($self, $prospective_request, $response) = @_; # Check response for multiple location headers. my @locations = $response->header('Location'); if (scalar(@locations) > 1) { my $uri = $prospective_request->uri(); my @uris = split /, /,$uri; $prospective_request->uri($uris[0]) if scalar @uris; } # Call parent to do all the boring work. my $ok = $self->SUPER::redirect_ok($prospective_request, $response +); return $ok; } 1;