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

In reply to Re: LWP::UserAgent : setting max_redirect to 0 yields 'Client-Warning: Redirect loop detected by bliako
in thread LWP::UserAgent : setting max_redirect to 0 yields 'Client-Warning: Redirect loop detected by bliako

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.