srchulo
srchulo

Reputation: 5203

Getting the "base URL" of a URL

In my application I need to get the base URL of any URL. Up until now I've been getting it like so:

$resp->base;

Where $resp is a HTTP::Response object. However, now I need to know the base of URLs without actually requesting them and getting an HTTP::Response object back. Does anyone know of a way to do this?

Upvotes: 2

Views: 2099

Answers (2)

Borodin
Borodin

Reputation: 126722

The value returned by the HTTP::Response->base method is a URL obtained from the HTTP response message that specifies how to resolve relative URLs that appear in the content of the message. If the message doesn't specify such a value then the URL used to request the message is used instead.

If you want the URL that would come from an HTTP::Response assuming there was no information to the contrary in the response then just use the URL as it is. It will work fine as a base URL.

There is no single base for an arbitrary URL. A base URL is an absolute URL that is used to resolve relative URLS, and can have any number of path steps.

For instance,

http://news.bbc.co.uk/1/hi/programmes/click_online/9735140.stm

can be expressed as

9735140.stm

relative to base URL

http://news.bbc.co.uk/1/hi/programmes/click_online/

or as

1/hi/programmes/click_online/9735140.stm

relative to base URL

http://news.bbc.co.uk/

or, of course, the path can be divided between the base and relative URL at any point.

You don't say what it is you want to use the value for. The solution you have chosen simply strips off the last element of the path-component of the URL that isn't followed by a slash, so for the URL

http://news.bbc.co.uk/1/hi/programmes/click_online

it returns

http://news.bbc.co.uk/1/hi/programmes/

but for

http://news.bbc.co.uk/1/hi/programmes/click_online/

it returns the same value

http://news.bbc.co.uk/1/hi/programmes/click_online/

If that is the behaviour you want then all is well, but I wouldn't describe it as "taking the base URL".

Upvotes: 0

ikegami
ikegami

Reputation: 385917

You want to find the the absolute url to which "." expands, relative to the url you have. URI can do that.

$ perl -MURI -E'say URI->new_abs(".", $_) for @ARGV' \
   'http://www.example.org/dir/' \
   'http://www.example.org/dir/file.html' \
   'http://www.example.org/dir/file.cgi?foo=bar'
http://www.example.org/dir/
http://www.example.org/dir/
http://www.example.org/dir/

Upvotes: 3

Related Questions