Re: URI handling of "//hostname.foo.com/" as base_uri

Gisle Aas (gisle@activestate.com)
18 Apr 2001 17:32:21 -0700


Ben Scott <scotsman@euphorion.com> writes:

> On Wed, Apr 18, 2001 at 04:50:51PM -0700, Gisle Aas wrote:
> > The $base argument should be an absolute URI.  "//a248.e.akamai.net/.../" is not.
> > The doc for the method says:
> > 
> >        $uri = URI->new_abs( $str, $base_uri )
> >            This constructs a new absolute URI object.  The $str
> >            argument can denote a relative or absolute URI.  If
> >            relative, then it will be absolutized using $base_uri
> >            as base. The $base_uri must be an absolute URI.
> > 
> > This patch also makes your code work, but that is more by accident
> > then anything else.  The second argument to URI->new_abs() should
> > really still be an absolute URI.
> > 
> > I would suggest you use code like this:
> > 
> > sub akamize {
> >    my($uri, $base) = @_;
> >    $base = URI->new($base, "http");
> >    $base->scheme("http") unless $base->scheme;  # ensure absoluteness
> >    $uri = URI->new_abs($uri, $base);
> >    $uri->scheme(undef);                         # make it relative
> >    return $uri;
> > }
> > 
> > It should work nicely both with and without the URI.pm patch.
> 
> I submit that a handler for relative URIs would be as handy as one for
> absolute URIs.  Perhaps URI->new_rel needs to be added.  In the case
> of sourcing images/javascript in a page fetched in  both SSL and nonSSL 
> (and given some time, I'd give others), the ability of using
> //hostname/path/file or /path/file in the URI module would be useful.

There is actually no problem with using "//hostname/path/file" or
"/path/file" with the URI module.  They are just relative URIs.  But
there are problems using them as $base arguments to the abs() method.

The URI->new_rel name is not a good one since it does not relate to
the $uri->rel method as URI->new_abs relates to $uri->abs.

Perhaps we can simply relax the requirement that the ->abs wants an
absolute URI as second argument.  It might be possible to have it just
make the URI as close to absolute as the "most absolute" of its
arguments are:

   URI::abs("#foo",  "/path")   -->  "/path#foo"
   URI::abs("//foo", "/path")   -->  "//foo"
   URI::abs("foo",   "/path")   -->  "/foo"
   URI::abs("?foo",  "/path")   -->  "/path?foo"

etc.

Regards,
Gisle