At Wed, 17 Apr 2002 08:06:56 +0200,
F. Xavier Noria <[EMAIL PROTECTED]> wrote:

> You have to say following POST redirections is OK with you:
> 
>    push @{ $ua->requests_redirectable }, 'POST'; 
> 
> (from perldoc LWP::UserAgent). This is so, I guess, becacuse the RFC
> says POST redirections must not be followed by user agents unless the
> user says so explicitly.

Yes. But even if you add the statement above in your client code, it
might not work the same as "standard" browsers. They (= IE, NN and
many others) follow POST redirects as GET.

What you should do is subclass LWP::UserAgent and override request()
method like this:

  sub request {
      my($self, $request, $arg, $size, $previous) = @_;
      my $response = $self->SUPER::request($request, $arg, $size, $previous);
      if ($response->is_redirect && $request->method eq 'POST') {
          # XXX: copy-and-paste from LWP::UserAgent
  
          # Make a copy of the request and initialize it with the new URI
          my $referral = $request->clone;
  
          # And then we update the URL based on the Location:-header.
          my($referral_uri) = $response->header('Location');
          {
              # Some servers erroneously return a relative URL for redirects,
              # so make it absolute if it not already is.
              local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
              my $base = $response->base;
              $referral_uri = $HTTP::URI_CLASS->new($referral_uri, $base)
                              ->abs($base);
          }
  
          $referral->url($referral_uri);
          $referral->remove_header('Host', 'Cookie');
  
          # switch to GET
          $referral->method('GET');
          $referral->content('');
          $referral->remove_header('Content-Length');
          return $self->request($referral, $arg, $size, $response);
      }
      return $response;
  }
  

--
Tatsuhiko Miyagawa <[EMAIL PROTECTED]>

Reply via email to