Re: lrequest() routine.. (Retrieves redirected documents)

Roy T. Fielding (fielding@simplon.ICS.UCI.EDU)
Wed, 13 Jul 1994 04:53:15 -0700


Brooks wrote:

> I'd like to submit the following code/interface to the libwww-perl
> distribution, but I'm having some problems passing the pointers to
> header and content..
> 
> The following code is equivalent to &www'request, except that it checks
> the returned headers to see if there is a location redirect, and if
> so it retrieves the Location/URI redirected document.

Try this (I haven't tested it):


sub www'lrequest
{
    local($method, $url, *headers, *content, $timeout) = @_;
    local($hd, $response);

    for (;;) 
    {
        $response = &www'request($method, $url, *headers, *content, $timeout);
        last unless ($response =~ /^30[12]$/);

        if ($url = $headers{'location'})
        {
              $url =~ s/, .*//;         # Get rid of multiple Location: entries
        }
        elsif ($url = $headers{'uri'})
        {
              $url =~ s/\s*;.*//;
              $url =~ s/, .*//;         # Get rid of any multiple URI: entries
        }
        else { last; }

        foreach $hd (keys(%headers))
        {
            next if ($hd =~ m#^[A-Z]#);
            delete $headers{$hd};
        }
    }
    return($response);
}



.......Roy