URI - Uniform Resource Identifiers |
URI - Uniform Resource Identifiers (absolute and relative)
$u1 = URI->new("http://www.perl.com"); $u2 = URI->new("foo", "http"); $u3 = $u2->abs($u1); $u4 = $u3->clone; $u5 = URI->new("HTTP://WWW.perl.com:80")->canonical;
$str = $u->as_string; $str = "$u";
$scheme = $u->scheme; $opaque = $u->opaque; $path = $u->path; $frag = $u->fragment;
$u->scheme("ftp"); $u->host("ftp.perl.com"); $u->path("cpan/");
This module implements the URI
class. Objects of this class
represent ``Uniform Resource Identifier references'' as specified in RFC
2396 (and updated by RFC 2732).
A Uniform Resource Identifier is a compact string of characters for
identifying an abstract or physical resource. A Uniform Resource
Identifier can be further classified either a Uniform Resource Locator
(URL) or a Uniform Resource Name (URN). The distinction between URL
and URN does not matter to the URI
class interface. A
``URI-reference'' is a URI that may have additional information attached
in the form of a fragment identifier.
An absolute URI reference consists of three parts. A scheme, a scheme specific part and a fragment identifier. A subset of URI references share a common syntax for hierarchical namespaces. For these the scheme specific part is further broken down into authority, path and query components. These URI can also take the form of relative URI references, where the scheme (and usually also the authority) component is missing, but implied by the context of the URI reference. The three forms of URI reference syntax are summarized as follows:
<scheme>:<scheme-specific-part>#<fragment> <scheme>://<authority><path>?<query>#<fragment> <path>?<query>#<fragment>
The components that a URI reference can be divided into depend on the
scheme. The URI
class provides methods to get and set the
individual components. The methods available for a specific
URI
object depend on the scheme.
The following methods construct new URI
objects:
The constructor determines the scheme, maps this to an appropriate URI subclass, constructs a new object of that class and returns it.
The $scheme argument is only used when $str is a
relative URI. It can either be a simple string that
denotes the scheme, a string containing an absolute URI reference or
an absolute URI
object. If no $scheme is specified for a relative
URI $str, then $str is simply treated as a generic URI (no scheme
specific methods available).
The set of characters available for building URI references is restricted (see the URI::Escape manpage). Characters outside this set are automatically escaped by the URI constructor.
The methods described in this section are available for all URI
objects.
Methods that give access to components of a URI will always return the
old value of the component. The value returned will be undef
if the
component was not present. There is generally a difference between a
component that is empty (represented as ""
) and a component that is
missing (represented as undef
). If an accessor method is given an
argument it will update the corresponding component in addition to
returning the old value of the component. Passing an undefined
argument will remove the component (if possible). The description of
the various accessor methods will tell if the component is passed as
an escaped or an unescaped string. Components that can be futher
divided into sub-parts are usually passed escaped, as unescaping might
change its semantics.
The common methods available for all URI are:
undef
. If called with an
argument, it will update the scheme of $uri, possibly changing the
class of $uri, and return the old scheme value. The method croaks
if the new scheme name is illegal; scheme names must begin with a
letter and must consist of only US-ASCII letters, numbers, and a few
special marks: ``.'', ``+'', ``-''. This restriction effectively means
that scheme have to be passed unescaped. Passing an undefined
argument to the scheme method will make the URI relative (if possible).
Letter case does not matter for scheme names. The string returned by $uri->scheme is always lowercase. If you want the scheme just as it was written in the URI in its original case, you can use the $uri->_scheme method instead.
For efficiency reasons, if the $uri already was in normalized form, then a reference to it is returned instead of a copy.
If you need to test whether two URI
object references denote the
same object, use the '==' operator.
The following methods are available to schemes that use the common/generic syntax for hierarchical namespaces. The description of schemes below will tell which one these are. Unknown schemes are assumed to support the generic syntax, and therefore the following methods:
Schemes where the authority component denotes a Internet host will have the following methods available in addition to the generic methods.
For some schemes this will be a user name and a password separated by a colon. This practice is not recommended. Embedding passwords in clear text (such as URI) has proven to be a security risk in almost every case where it has been used.
If the $new_host string ends with a colon and a number, then this number will also set the port.
If no explicit port is specified in the URI, then the default port of the URI scheme is returned. If you don't want the default port substituted, then you can use the $uri->_port method instead.
The following URI schemes are specifically supported. For URI
objects not belonging to one of these you can only use the common and
generic methods.
URI
objects belonging to the data scheme support the common methods
and two new methods to access their scheme specific components;
$uri->media_type and $uri->data. See the URI::data manpage for details.
URI
objects belonging to the file scheme support the common and
generic methods. In addition they provide two methods to map file URI
back to local file names; $uri->file and $uri->dir. See the URI::file manpage
for details.
URI
objects belonging to the ftp scheme support the common,
generic and server methods. In addition they provide two methods to
access the userinfo sub-components: $uri->user and $uri->password.
URI
objects belonging to the gopher scheme support the common,
generic and server methods. In addition they support some methods to
access gopher specific path components: $uri->gopher_type,
$uri->selector, $uri->search, $uri->string.
URI
objects belonging to the http scheme support the common,
generic and server methods.
URI
objects belonging to the ldap scheme support the common,
generic and server methods as well as specific ldap methods; $uri->dn,
$uri->attributes, $uri->scope, $uri->filter, $uri->extensions. See
the URI::ldap manpage for details.
URI
objects belonging to the mailto scheme support the common
methods and the generic query methods. In addition they support the
following mailto specific methods: $uri->to, $uri->headers.
URI
objects belonging to the news scheme support the common,
generic and server methods. In addition they provide some methods to
access the path: $uri->group and $uri->message.
URI
objects belonging to the pop scheme support the common, generic
and server methods. In addition they provide two methods to access the
userinfo components: $uri->user and $uri->auth
URI
objects belonging to the rlogin scheme support the
common, generic and server methods.
URI
objects belonging to the rsync scheme support the common,
generic and server methods. In addition they provide methods to
access the userinfo sub-components: $uri->user and $uri->password.
URI
objects belonging to the telnet scheme support the
common, generic and server methods.
URI
objects belonging to the ssh scheme support the common,
generic and server methods. In addition they provide methods to
access the userinfo sub-components: $uri->user and $uri->password.
The following configuration variables influence how the class and it's methods behave:
URI->new("http:foo")->abs("http://host/a/b") ==> "http:foo"
local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1; URI->new("http:foo")->abs("http://host/a/b") ==> "http:/host/a/foo"
abs()
method ignore excess ``..''
segments in the relative URI by setting $URI::ABS_REMOTE_LEADING_DOTS
to a TRUE value. The difference is demonstrated by the following
examples:
URI->new("../../../foo")->abs("http://host/a/b") ==> "http://host/../../foo"
local $URI::URL::ABS_REMOTE_LEADING_DOTS = 1; URI->new("../../../foo")->abs("http://host/a/b") ==> "http://host/foo"
Using regexp variables like $1 directly as argument to the URI methods do not work too well with current perl implementations. I would argue that this is actually a bug in perl. The workaround is to quote them. E.g.:
/(...)/ || die; $u->query("$1");
As an alternative to this module, the following (official) regular expression can be used to decode a URI:
my($scheme, $authority, $path, $query, $fragment) = $uri =~ m|^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
the URI::file manpage, the URI::WithBase manpage, the URI::Escape manpage, the URI::Heuristic manpage
RFC 2396: ``Uniform Resource Identifiers (URI): Generic Syntax'', Berners-Lee, Fielding, Masinter, August 1998.
Copyright 1995-2002 Gisle Aas.
Copyright 1995 Martijn Koster.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This module is based on the URI::URL
module, which in turn was
(distantly) based on the wwwurl.pl
code in the libwww-perl for
perl4 developed by Roy Fielding, as part of the Arcadia project at the
University of California, Irvine, with contributions from Brooks
Cutter.
URI::URL
was developed by Gisle Aas, Tim Bunce, Roy Fielding and
Martijn Koster with input from other people on the libwww-perl mailing
list.
URI
and related subclasses was developed by Gisle Aas.
URI - Uniform Resource Identifiers |