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;


In reply to Re: LWP redirect problem, produces malformed URL? by gmargo
in thread LWP redirect problem, produces malformed URL? by FloydATC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.