Re: HTTP::Request DIE error

Gisle Aas (gisle@activestate.com)
11 Apr 2001 15:18:13 -0700


Tim Allwine <tallwine@oreilly.com> writes:

> Consider the following code:
> 
> use LWP;
> 
> $SIG{__DIE__} = \&myCode;
> 
> my $ua = LWP::UserAgent->new();
> my $base = 'http://www.perl.com';
> my $path = '/pub';
> my $uri = URI->new_abs($path,$base);
> my $req = HTTP::Request->new(GET => $uri);
> my $res = $ua->request($req);
> 
> print $res->status_line,"\n";
> 
> sub myCode {
>     print "@_","\n";
> }
> 
> When I run this I get the following from my DIE handler:
> Missing base argument at
> /usr/local/lib/perl5/site_perl/5.6.0/HTTP/Request.pm line 107
> and then prints
>     200 OK
> as expected.
> 
> The offending lines read like this:
> 
>     # Argh!! Hate this... old LWP legacy!
>     eval { $uri = $uri->abs; };
>     die $@ if $@ && $@ !~ /Missing base argument/;
> 
> As far as I can tell $uri->abs should take an argument, shouldn't it?

If $uri is a URI::URL object then it does not.  The reason for that
code is that the old interface that relied on URI::URL used to
absolutize the URI.

> Look in _generic.pm,
> sub abs {
>     my $self = shift;
>     my $base = shift || Carp::croak("Missing base argument");
>     ...
> }
> 
> How about changing the eval to:
>     eval{ $uri = $uri->abs($uri->host) };

Not all URIs has a ->host.

> If I leave out the die handler you would never see this but
> I shouldn't have too.

I checked in this patch instead :-)

Index: lib/HTTP/Request.pm
===================================================================
RCS file: /cvsroot/libwww-perl/lwp5/lib/HTTP/Request.pm,v
retrieving revision 1.27
diff -u -p -u -r1.27 Request.pm
--- lib/HTTP/Request.pm	1999/11/17 20:38:14	1.27
+++ lib/HTTP/Request.pm	2001/04/11 22:11:37
@@ -104,7 +104,7 @@ sub uri
 	    $uri = $uri->clone;
 	    unless ($HTTP::URI_CLASS eq "URI") {
 		# Argh!! Hate this... old LWP legacy!
-		eval { $uri = $uri->abs; };
+		eval { local $SIG{__DIE__}; $uri = $uri->abs; };
 		die $@ if $@ && $@ !~ /Missing base argument/;
 	    }
 	} else {