Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: LWP::UserAgent : setting max_redirect to 0 yields 'Client-Warning: Redirect loop detected

by bliako (Vicar)
on Mar 14, 2018 at 11:27 UTC ( #1210882=note: print w/replies, xml ) Need Help??


in reply to LWP::UserAgent : setting max_redirect to 0 yields 'Client-Warning: Redirect loop detected

In order to test LWP::UserAgent behaviour on server response 302 Found, I have created the following scripts and uploaded them to a server. If you want to do a quick test then use the last script below to hit http://fotomazi.eu5.net/bin/302.cgi You should get redirected to 200.cgi, and have a cookie set.

script name: 302.cgi

# perl CGI to send a 302 Found response to caller plus setting # a cookie with name=XYZ123 and value 1. # the REDIRECT_URL points to the script described further below # hosted at my server. use strict; use warnings; use CGI; use CGI::Cookie; my $REDIRECT_URL = 'http://fotomazi.eu5.net/bin/200.cgi'; my $cgi = CGI->new; my $cookie = $cgi->cookie( -name=>'COOKIE302', -value=>'1', -expires=>'+4h', -path=>'/' ); print $cgi->redirect( -uri=> $REDIRECT_URL, -cookie => $cookie, -status=>302 ); exit(0); 1; __END__

script name: 200.cgi

# perl CGI which expects a cookie with name=XYZ123, if received # then cookie's value is incremented by 1 and returned. # this script can act as the final destination of the 302 Found redire +ct script above. use strict; use warnings; use CGI; use CGI::Cookie; my $cgi = CGI->new; my $cookies = fetch CGI::Cookie; my $cookie = $cookies->{'COOKIE302'}; if( ! defined($cookie) ){ print $cgi->header() . "No cookie received.<br>"; exit(1) } my $cookie_value = $cookie->value; if( ! ($cookie_value =~ /^([0-9]+)$/) ){ print $cgi->header() . "Invalid cookie received, value is '$cookie +_value'.<br>"; exit(1) } $cookie->value($cookie_value+1); print $cgi->header( -charset => "utf-8", -cookie => $cookie, ) . "this is 200 OK, you have been redirected OK, your cookie is valid +, value was '".$cookie_value."' and now is '".$cookie->value."'.<br>" +; exit(0); 1; __END__
The following script can be run locally at command line to make a request to the 302.cgi and check how it handles redirects with various values for LWP::UserAgent's max_redirect and requests_redirectable.

script name: test_redirect.pl

#!/usr/bin/env perl # create a UserAgent and send a request to 302.cgi to see how # it handles redirects. use LWP::UserAgent; use LWP::ConsoleLogger::Easy qw( debug_ua ); use HTTP::Response; use HTTP::Request::Common; use HTTP::Cookies; use URI; my $URLobj = URI->new('http://fotomazi.eu5.net/bin/302.cgi'); my $URLstr = $URLobj->as_string; my $ua = LWP::UserAgent->new( # 'requests_redirectable' => [], ); LWP::ConsoleLogger::Easy::debug_ua($ua, 6); $ua->cookie_jar(HTTP::Cookies->new( 'file' => './cookies.txt', 'autosave' => 1, 'ignore_discard' => 1, )); my %extra_headers = (); my $a_request = HTTP::Request::Common::GET( $URLstr, %extra_headers ); if( ! defined($a_request) ){ print STDERR "$0 : call to ".'HTTP::Reque +st::Common::GET()'." has failed.\n"; exit(1) } my $a_response = $ua->request($a_request); print $a_response->as_string."\n";

The first two scripts are already hosted on a server so one can use the last script only to send a request lto http://fotomazi.eu5.net/bin/302.cgi

The two CGI scripts do not keep any logs.

bw b
  • Comment on Re: LWP::UserAgent : setting max_redirect to 0 yields 'Client-Warning: Redirect loop detected
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210882]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2019-12-08 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?