Here's a simple way to address this.

Save the following code to MyUA.pm and then use MyUA
rather than LWP::UserAgent.

package MyUA;
use LWP;
@ISA = ("LWP::UserAgent");
1;  


sub redirect_ok
{
    # RFC 2068, section 10.3.2 and 10.3.3 say:
    #  If the 30[12] status code is received in
response to a request other
    #  than GET or HEAD, the user agent MUST NOT
automatically redirect the
    #  request unless it can be confirmed by the user,
since this might
    #  change the conditions under which the request
was issued.

    # Note that this routine used to be just:
    #  return 0 if $_[1]->method eq "POST";  return 1;

    my($self, $request) = @_;
    my $method = $request->method;
    return 0 unless grep $_ eq $method,
      @{ $self->requests_redirectable || [] };
    
    if($request->url->scheme eq 'file') {
      LWP::Debug::trace("Can't redirect to a file://
URL!");
      return 0;
    }
    
    # added to fix-up redirects of POST requests:
    # most (all?) browsers send a GET in response to a 
    # redirect received from a POST request.
    if ($method eq 'POST') {
        $request->method('GET');
    }
    
    # Otherwise it's apparently okay...
    return 1;
}


-David



On Fri, 13 Jun 2003 06:31:54 -0800, "Sean M. Burke"
wrote:

> 
> At 09:56 AM 2003-06-13 -0400, Alan Olegario wrote:
> >I've tried using the same type script against an http
> site, and still got 
> >the following response:
> >
> >HTTP/1.1 405 Method Not Allowed
> 
> I bet that what's happening is this:
> Your POST gets a redirection response.  The HTTP spec
> says this should 
> result in another POST, but, alas, "When automatically
> redirecting a POST 
> request after receiving a 301 status code, some
> existing HTTP/1.0 user 
> agents will erroneously change it into a GET
request." 
> So "many" (probably 
> all) browsers do something else -- they do a GET in
> this circumstance, and 
> that's what the programmers of the CGI you're posting
> to are assuming will 
> happen.  But since LWP does a POST instead, you get
> that error.
> 
> Gisle, haven't people filed patches for making LWP act
> like every other 
> user-agent, instead of following the spec?  If you
> don't have a patch for 
> it, I could probably whip one up for you without too
> much bother.
> 
> --
> Sean M. Burke    http://search.cpan.org/~sburke/

Reply via email to