Subject: | make redirection of POST requests work the same way that browsers do |
WWW-Mechanize-0.44 introduced the behaviour of following redirects after a POST. This is non-RFC compliant, but it's what most browsers do and we're attempting to mimic them so we do the same. However, when browsers do so they also change the method from POST to GET. Not doing so can cause problems with some web-server applications.
In order to fix this I have created a patch that overloads redirect_ok() from LWP::UserAgent. I have tested it using perl version v5.6.1 built for sun4-solaris-thread-multi with patch "ActivePerl Build 631" applied, on a SUNW,Ultra-250 running Solaris 8.
914a915,942
>
> =head2 redirect_ok($prospective_request)
>
> An overloaded version of C<redirect_ok()> in L<LWP::UserAgent>. This method is used to determine whether a redirection in the request should be followed.
>
> This version behaves like the original, except that if the redirection was from a POST it changes the HTTP method to GET. This does not conform with the RFCs, but it is how many browser user agent implementations behave. As we are trying to model them, we must unfortunately mimic their erroneously reaction. See L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3> for details on correct behaviour.
>
> =cut
>
> sub redirect_ok
> {
> 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;
> }
>
> # Mimic erroneously browser behaviour.
> $request->method("GET") if $request->method eq "POST";
>
> # Otherwise it's apparently okay...
> return 1;
> }
>