1URI(3)                User Contributed Perl Documentation               URI(3)
2
3
4

NAME

6       URI - Uniform Resource Identifiers (absolute and relative)
7

SYNOPSIS

9        $u1 = URI->new("http://www.perl.com");
10        $u2 = URI->new("foo", "http");
11        $u3 = $u2->abs($u1);
12        $u4 = $u3->clone;
13        $u5 = URI->new("HTTP://WWW.perl.com:80")->canonical;
14
15        $str = $u->as_string;
16        $str = "$u";
17
18        $scheme = $u->scheme;
19        $opaque = $u->opaque;
20        $path   = $u->path;
21        $frag   = $u->fragment;
22
23        $u->scheme("ftp");
24        $u->host("ftp.perl.com");
25        $u->path("cpan/");
26

DESCRIPTION

28       This module implements the "URI" class.  Objects of this class repre‐
29       sent "Uniform Resource Identifier references" as specified in RFC 2396
30       (and updated by RFC 2732).
31
32       A Uniform Resource Identifier is a compact string of characters that
33       identifies an abstract or physical resource.  A Uniform Resource Iden‐
34       tifier can be further classified as either a Uniform Resource Locator
35       (URL) or a Uniform Resource Name (URN).  The distinction between URL
36       and URN does not matter to the "URI" class interface. A "URI-reference"
37       is a URI that may have additional information attached in the form of a
38       fragment identifier.
39
40       An absolute URI reference consists of three parts:  a scheme, a scheme-
41       specific part and a fragment identifier.  A subset of URI references
42       share a common syntax for hierarchical namespaces.  For these, the
43       scheme-specific part is further broken down into authority, path and
44       query components.  These URIs can also take the form of relative URI
45       references, where the scheme (and usually also the authority) component
46       is missing, but implied by the context of the URI reference.  The three
47       forms of URI reference syntax are summarized as follows:
48
49         <scheme>:<scheme-specific-part>#<fragment>
50         <scheme>://<authority><path>?<query>#<fragment>
51         <path>?<query>#<fragment>
52
53       The components into which a URI reference can be divided depend on the
54       scheme.  The "URI" class provides methods to get and set the individual
55       components.  The methods available for a specific "URI" object depend
56       on the scheme.
57

CONSTRUCTORS

59       The following methods construct new "URI" objects:
60
61       $uri = URI->new( $str )
62       $uri = URI->new( $str, $scheme )
63           Constructs a new URI object.  The string representation of a URI is
64           given as argument, together with an optional scheme specification.
65           Common URI wrappers like "" and <>, as well as leading and trailing
66           white space, are automatically removed from the $str argument
67           before it is processed further.
68
69           The constructor determines the scheme, maps this to an appropriate
70           URI subclass, constructs a new object of that class and returns it.
71
72           The $scheme argument is only used when $str is a relative URI.  It
73           can be either a simple string that denotes the scheme, a string
74           containing an absolute URI reference, or an absolute "URI" object.
75           If no $scheme is specified for a relative URI $str, then $str is
76           simply treated as a generic URI (no scheme-specific methods avail‐
77           able).
78
79           The set of characters available for building URI references is
80           restricted (see URI::Escape).  Characters outside this set are
81           automatically escaped by the URI constructor.
82
83       $uri = URI->new_abs( $str, $base_uri )
84           Constructs a new absolute URI object.  The $str argument can denote
85           a relative or absolute URI.  If relative, then it is absolutized
86           using $base_uri as base. The $base_uri must be an absolute URI.
87
88       $uri = URI::file->new( $filename )
89       $uri = URI::file->new( $filename, $os )
90           Constructs a new file URI from a file name.  See URI::file.
91
92       $uri = URI::file->new_abs( $filename )
93       $uri = URI::file->new_abs( $filename, $os )
94           Constructs a new absolute file URI from a file name.  See
95           URI::file.
96
97       $uri = URI::file->cwd
98           Returns the current working directory as a file URI.  See
99           URI::file.
100
101       $uri->clone
102           Returns a copy of the $uri.
103

COMMON METHODS

105       The methods described in this section are available for all "URI"
106       objects.
107
108       Methods that give access to components of a URI always return the old
109       value of the component.  The value returned is "undef" if the component
110       was not present.  There is generally a difference between a component
111       that is empty (represented as "") and a component that is missing (rep‐
112       resented as "undef").  If an accessor method is given an argument, it
113       updates the corresponding component in addition to returning the old
114       value of the component.  Passing an undefined argument removes the com‐
115       ponent (if possible).  The description of each accessor method indi‐
116       cates whether the component is passed as an escaped or an unescaped
117       string.  A component that can be further divided into sub-parts are
118       usually passed escaped, as unescaping might change its semantics.
119
120       The common methods available for all URI are:
121
122       $uri->scheme
123       $uri->scheme( $new_scheme )
124           Sets and returns the scheme part of the $uri.  If the $uri is rela‐
125           tive, then $uri->scheme returns "undef".  If called with an argu‐
126           ment, it updates the scheme of $uri, possibly changing the class of
127           $uri, and returns the old scheme value.  The method croaks if the
128           new scheme name is illegal; a scheme name must begin with a letter
129           and must consist of only US-ASCII letters, numbers, and a few spe‐
130           cial marks: ".", "+", "-".  This restriction effectively means that
131           the scheme must be passed unescaped.  Passing an undefined argument
132           to the scheme method makes the URI relative (if possible).
133
134           Letter case does not matter for scheme names.  The string returned
135           by $uri->scheme is always lowercase.  If you want the scheme just
136           as it was written in the URI in its original case, you can use the
137           $uri->_scheme method instead.
138
139       $uri->opaque
140       $uri->opaque( $new_opaque )
141           Sets and returns the scheme-specific part of the $uri (everything
142           between the scheme and the fragment) as an escaped string.
143
144       $uri->path
145       $uri->path( $new_path )
146           Sets and returns the same value as $uri->opaque unless the URI sup‐
147           ports the generic syntax for hierarchical namespaces.  In that case
148           the generic method is overridden to set and return the part of the
149           URI between the host name and the fragment.
150
151       $uri->fragment
152       $uri->fragment( $new_frag )
153           Returns the fragment identifier of a URI reference as an escaped
154           string.
155
156       $uri->as_string
157           Returns a URI object to a plain string.  URI objects are also con‐
158           verted to plain strings automatically by overloading.  This means
159           that $uri objects can be used as plain strings in most Perl con‐
160           structs.
161
162       $uri->canonical
163           Returns a normalized version of the URI.  The rules for normaliza‐
164           tion are scheme-dependent.  They usually involve lowercasing the
165           scheme and Internet host name components, removing the explicit
166           port specification if it matches the default port, uppercasing all
167           escape sequences, and unescaping octets that can be better repre‐
168           sented as plain characters.
169
170           For efficiency reasons, if the $uri is already in normalized form,
171           then a reference to it is returned instead of a copy.
172
173       $uri->eq( $other_uri )
174       URI::eq( $first_uri, $other_uri )
175           Tests whether two URI references are equal.  URI references that
176           normalize to the same string are considered equal.  The method can
177           also be used as a plain function which can also test two string
178           arguments.
179
180           If you need to test whether two "URI" object references denote the
181           same object, use the '==' operator.
182
183       $uri->abs( $base_uri )
184           Returns an absolute URI reference.  If $uri is already absolute,
185           then a reference to it is simply returned.  If the $uri is rela‐
186           tive, then a new absolute URI is constructed by combining the $uri
187           and the $base_uri, and returned.
188
189       $uri->rel( $base_uri )
190           Returns a relative URI reference if it is possible to make one that
191           denotes the same resource relative to $base_uri.  If not, then $uri
192           is simply returned.
193

GENERIC METHODS

195       The following methods are available to schemes that use the com‐
196       mon/generic syntax for hierarchical namespaces.  The descriptions of
197       schemes below indicate which these are.  Unknown schemes are assumed to
198       support the generic syntax, and therefore the following methods:
199
200       $uri->authority
201       $uri->authority( $new_authority )
202           Sets and returns the escaped authority component of the $uri.
203
204       $uri->path
205       $uri->path( $new_path )
206           Sets and returns the escaped path component of the $uri (the part
207           between the host name and the query or fragment).  The path can
208           never be undefined, but it can be the empty string.
209
210       $uri->path_query
211       $uri->path_query( $new_path_query )
212           Sets and returns the escaped path and query components as a single
213           entity.  The path and the query are separated by a "?" character,
214           but the query can itself contain "?".
215
216       $uri->path_segments
217       $uri->path_segments( $segment, ... )
218           Sets and returns the path.  In a scalar context, it returns the
219           same value as $uri->path.  In a list context, it returns the
220           unescaped path segments that make up the path.  Path segments that
221           have parameters are returned as an anonymous array.  The first ele‐
222           ment is the unescaped path segment proper;  subsequent elements are
223           escaped parameter strings.  Such an anonymous array uses overload‐
224           ing so it can be treated as a string too, but this string does not
225           include the parameters.
226
227           Note that absolute paths have the empty string as their first
228           path_segment, i.e. the path "/foo/bar" have 3 path_segments; "",
229           "foo" and "bar".
230
231       $uri->query
232       $uri->query( $new_query )
233           Sets and returns the escaped query component of the $uri.
234
235       $uri->query_form
236       $uri->query_form( $key1 => $val1, $key2 => $val2, ... )
237       $uri->query_form( \@key_value_pairs )
238       $uri->query_form( \%hash )
239           Sets and returns query components that use the applica‐
240           tion/x-www-form-urlencoded format.  Key/value pairs are separated
241           by "&", and the key is separated from the value by a "=" character.
242
243           The form can be set either by passing separate key/value pairs, or
244           via an array or hash reference.  Passing an empty array or an empty
245           hash removes the query component, whereas passing no arguments at
246           all leaves the component unchanged.  The order of keys is undefined
247           if a hash reference is passed.  The old value is always returned as
248           a list of separate key/value pairs.  Assigning this list to a hash
249           is unwise as the keys returned might repeat.
250
251           The values passed when setting the form can be plain strings or
252           references to arrays of strings.  Passing an array of values has
253           the same effect as passing the key repeatedly with one value at a
254           time.  All the following statements have the same effect:
255
256               $uri->query_form(foo => 1, foo => 2);
257               $uri->query_form(foo => [1, 2]);
258               $uri->query_form([ foo => 1, foo => 2 ]);
259               $uri->query_form([ foo => [1, 2] ]);
260               $uri->query_form({ foo => [1, 2] });
261
262           The "URI::QueryParam" module can be loaded to add further methods
263           to manipulate the form of a URI.  See URI::QueryParam for details.
264
265       $uri->query_keywords
266       $uri->query_keywords( $keywords, ... )
267       $uri->query_keywords( \@keywords )
268           Sets and returns query components that use the keywords separated
269           by "+" format.
270
271           The keywords can be set either by passing separate keywords
272           directly or by passing a reference to an array of keywords.  Pass‐
273           ing an empty array removes the query component, whereas passing no
274           arguments at all leaves the component unchanged.  The old value is
275           always returned as a list of separate words.
276

SERVER METHODS

278       For schemes where the authority component denotes an Internet host, the
279       following methods are available in addition to the generic methods.
280
281       $uri->userinfo
282       $uri->userinfo( $new_userinfo )
283           Sets and returns the escaped userinfo part of the authority compo‐
284           nent.
285
286           For some schemes this is a user name and a password separated by a
287           colon.  This practice is not recommended. Embedding passwords in
288           clear text (such as URI) has proven to be a security risk in almost
289           every case where it has been used.
290
291       $uri->host
292       $uri->host( $new_host )
293           Sets and returns the unescaped hostname.
294
295           If the $new_host string ends with a colon and a number, then this
296           number also sets the port.
297
298       $uri->port
299       $uri->port( $new_port )
300           Sets and returns the port.  The port is a simple integer that
301           should be greater than 0.
302
303           If a port is not specified explicitly in the URI, then the URI
304           scheme's default port is returned. If you don't want the default
305           port substituted, then you can use the $uri->_port method instead.
306
307       $uri->host_port
308       $uri->host_port( $new_host_port )
309           Sets and returns the host and port as a single unit.  The returned
310           value includes a port, even if it matches the default port.  The
311           host part and the port part are separated by a colon: ":".
312
313       $uri->default_port
314           Returns the default port of the URI scheme to which $uri belongs.
315           For http this is the number 80, for ftp this is the number 21, etc.
316           The default port for a scheme can not be changed.
317

SCHEME-SPECIFIC SUPPORT

319       Scheme-specific support is provided for the following URI schemes.  For
320       "URI" objects that do not belong to one of these, you can only use the
321       common and generic methods.
322
323       data:
324           The data URI scheme is specified in RFC 2397.  It allows inclusion
325           of small data items as "immediate" data, as if it had been included
326           externally.
327
328           "URI" objects belonging to the data scheme support the common meth‐
329           ods and two new methods to access their scheme-specific components:
330           $uri->media_type and $uri->data.  See URI::data for details.
331
332       file:
333           An old specification of the file URI scheme is found in RFC 1738.
334           A new RFC 2396 based specification in not available yet, but file
335           URI references are in common use.
336
337           "URI" objects belonging to the file scheme support the common and
338           generic methods.  In addition, they provide two methods for mapping
339           file URIs back to local file names; $uri->file and $uri->dir.  See
340           URI::file for details.
341
342       ftp:
343           An old specification of the ftp URI scheme is found in RFC 1738.  A
344           new RFC 2396 based specification in not available yet, but ftp URI
345           references are in common use.
346
347           "URI" objects belonging to the ftp scheme support the common,
348           generic and server methods.  In addition, they provide two methods
349           for accessing the userinfo sub-components: $uri->user and
350           $uri->password.
351
352       gopher:
353           The gopher URI scheme is specified in
354           <draft-murali-url-gopher-1996-12-04> and will hopefully be avail‐
355           able as a RFC 2396 based specification.
356
357           "URI" objects belonging to the gopher scheme support the common,
358           generic and server methods. In addition, they support some methods
359           for accessing gopher-specific path components: $uri->gopher_type,
360           $uri->selector, $uri->search, $uri->string.
361
362       http:
363           The http URI scheme is specified in RFC 2616.  The scheme is used
364           to reference resources hosted by HTTP servers.
365
366           "URI" objects belonging to the http scheme support the common,
367           generic and server methods.
368
369       https:
370           The https URI scheme is a Netscape invention which is commonly
371           implemented.  The scheme is used to reference HTTP servers through
372           SSL connections.  Its syntax is the same as http, but the default
373           port is different.
374
375       ldap:
376           The ldap URI scheme is specified in RFC 2255.  LDAP is the Light‐
377           weight Directory Access Protocol.  An ldap URI describes an LDAP
378           search operation to perform to retrieve information from an LDAP
379           directory.
380
381           "URI" objects belonging to the ldap scheme support the common,
382           generic and server methods as well as ldap-specific methods:
383           $uri->dn, $uri->attributes, $uri->scope, $uri->filter, $uri->exten‐
384           sions.  See URI::ldap for details.
385
386       ldapi:
387           Like the ldap URI scheme, but uses a UNIX domain socket.  The
388           server methods are not supported, and the local socket path is
389           available as $uri->un_path.  The ldapi scheme is used by the OpenL‐
390           DAP package.  There is no real specification for it, but it is men‐
391           tioned in various OpenLDAP manual pages.
392
393       ldaps:
394           Like the ldap URI scheme, but uses an SSL connection.  This scheme
395           is deprecated, as the preferred way is to use the start_tls mecha‐
396           nism.
397
398       mailto:
399           The mailto URI scheme is specified in RFC 2368.  The scheme was
400           originally used to designate the Internet mailing address of an
401           individual or service.  It has (in RFC 2368) been extended to allow
402           setting of other mail header fields and the message body.
403
404           "URI" objects belonging to the mailto scheme support the common
405           methods and the generic query methods.  In addition, they support
406           the following mailto-specific methods: $uri->to, $uri->headers.
407
408       mms:
409           The mms URL specification can be found at <http://sdp.ppona.com/>
410           "URI" objects belonging to the mms scheme support the common,
411           generic, and server methods, with the exception of userinfo and
412           query-related sub-components.
413
414       news:
415           The news, nntp and snews URI schemes are specified in
416           <draft-gilman-news-url-01> and will hopefully be available as an
417           RFC 2396 based specification soon.
418
419           "URI" objects belonging to the news scheme support the common,
420           generic and server methods.  In addition, they provide some methods
421           to access the path: $uri->group and $uri->message.
422
423       nntp:
424           See news scheme.
425
426       pop:
427           The pop URI scheme is specified in RFC 2384. The scheme is used to
428           reference a POP3 mailbox.
429
430           "URI" objects belonging to the pop scheme support the common,
431           generic and server methods.  In addition, they provide two methods
432           to access the userinfo components: $uri->user and $uri->auth
433
434       rlogin:
435           An old specification of the rlogin URI scheme is found in RFC 1738.
436           "URI" objects belonging to the rlogin scheme support the common,
437           generic and server methods.
438
439       rtsp:
440           The rtsp URL specification can be found in section 3.2 of RFC 2326.
441           "URI" objects belonging to the rtsp scheme support the common,
442           generic, and server methods, with the exception of userinfo and
443           query-related sub-components.
444
445       rtspu:
446           The rtspu URI scheme is used to talk to RTSP servers over UDP
447           instead of TCP.  The syntax is the same as rtsp.
448
449       rsync:
450           Information about rsync is available from http://rsync.samba.org.
451           "URI" objects belonging to the rsync scheme support the common,
452           generic and server methods.  In addition, they provide methods to
453           access the userinfo sub-components: $uri->user and $uri->password.
454
455       sip:
456           The sip URI specification is described in sections 19.1 and 25 of
457           RFC 3261.  "URI" objects belonging to the sip scheme support the
458           common, generic, and server methods with the exception of path
459           related sub-components.  In addition, they provide two methods to
460           get and set sip parameters: $uri->params_form and $uri->params.
461
462       sips:
463           See sip scheme.  Its syntax is the same as sip, but the default
464           port is different.
465
466       snews:
467           See news scheme.  Its syntax is the same as news, but the default
468           port is different.
469
470       telnet:
471           An old specification of the telnet URI scheme is found in RFC 1738.
472           "URI" objects belonging to the telnet scheme support the common,
473           generic and server methods.
474
475       tn3270:
476           These URIs are used like telnet URIs but for connections to IBM
477           mainframes.  "URI" objects belonging to the tn3270 scheme support
478           the common, generic and server methods.
479
480       ssh:
481           Information about ssh is available at http://www.openssh.com/.
482           "URI" objects belonging to the ssh scheme support the common,
483           generic and server methods. In addition, they provide methods to
484           access the userinfo sub-components: $uri->user and $uri->password.
485
486       urn:
487           The syntax of Uniform Resource Names is specified in RFC 2141.
488           "URI" objects belonging to the urn scheme provide the common meth‐
489           ods, and also the methods $uri->nid and $uri->nss, which return the
490           Namespace Identifier and the Namespace-Specific String respec‐
491           tively.
492
493           The Namespace Identifier basically works like the Scheme identifier
494           of URIs, and further divides the URN namespace.  Namespace Identi‐
495           fier assignments are maintained at <http://www.iana.org/assign
496           ments/urn-namespaces>.
497
498           Letter case is not significant for the Namespace Identifier.  It is
499           always returned in lower case by the $uri->nid method.  The
500           $uri->_nid method can be used if you want it in its original case.
501
502       urn:isbn:
503           The "urn:isbn:" namespace contains International Standard Book Num‐
504           bers (ISBNs) and is described in RFC 3187.  A "URI" object belong‐
505           ing to this namespace has the following extra methods (if the Busi‐
506           ness::ISBN module is available): $uri->isbn, $uri->isbn_pub‐
507           lisher_code, $uri->isbn_country_code, $uri->isbn_as_ean.
508
509       urn:oid:
510           The "urn:oid:" namespace contains Object Identifiers (OIDs) and is
511           described in RFC 3061.  An object identifier consists of sequences
512           of digits separated by dots.  A "URI" object belonging to this
513           namespace has an additional method called $uri->oid that can be
514           used to get/set the oid value.  In a list context, oid numbers are
515           returned as separate elements.
516

CONFIGURATION VARIABLES

518       The following configuration variables influence how the class and its
519       methods behave:
520
521       $URI::ABS_ALLOW_RELATIVE_SCHEME
522           Some older parsers used to allow the scheme name to be present in
523           the relative URL if it was the same as the base URL scheme.  RFC
524           2396 says that this should be avoided, but you can enable this old
525           behaviour by setting the $URI::ABS_ALLOW_RELATIVE_SCHEME variable
526           to a TRUE value.  The difference is demonstrated by the following
527           examples:
528
529             URI->new("http:foo")->abs("http://host/a/b")
530                 ==>  "http:foo"
531
532             local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
533             URI->new("http:foo")->abs("http://host/a/b")
534                 ==>  "http:/host/a/foo"
535
536       $URI::ABS_REMOTE_LEADING_DOTS
537           You can also have the abs() method ignore excess ".."  segments in
538           the relative URI by setting $URI::ABS_REMOTE_LEADING_DOTS to a TRUE
539           value.  The difference is demonstrated by the following examples:
540
541             URI->new("../../../foo")->abs("http://host/a/b")
542                 ==> "http://host/../../foo"
543
544             local $URI::ABS_REMOTE_LEADING_DOTS = 1;
545             URI->new("../../../foo")->abs("http://host/a/b")
546                 ==> "http://host/foo"
547

BUGS

549       Using regexp variables like $1 directly as arguments to the URI methods
550       does not work too well with current perl implementations.  I would
551       argue that this is actually a bug in perl.  The workaround is to quote
552       them. Example:
553
554          /(...)/ ⎪⎪ die;
555          $u->query("$1");
556

PARSING URIs WITH REGEXP

558       As an alternative to this module, the following (official) regular
559       expression can be used to decode a URI:
560
561         my($scheme, $authority, $path, $query, $fragment) =
562         $uri =~ m⎪(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?⎪;
563
564       The "URI::Split" module provides the function uri_split() as a readable
565       alternative.
566

SEE ALSO

568       URI::file, URI::WithBase, URI::QueryParam, URI::Escape, URI::Split,
569       URI::Heuristic
570
571       RFC 2396: "Uniform Resource Identifiers (URI): Generic Syntax", Bern‐
572       ers-Lee, Fielding, Masinter, August 1998.
573
574       http://www.iana.org/assignments/uri-schemes
575
576       http://www.iana.org/assignments/urn-namespaces
577
578       http://www.w3.org/Addressing/
579
581       Copyright 1995-2003 Gisle Aas.
582
583       Copyright 1995 Martijn Koster.
584
585       This program is free software; you can redistribute it and/or modify it
586       under the same terms as Perl itself.
587

AUTHORS / ACKNOWLEDGMENTS

589       This module is based on the "URI::URL" module, which in turn was (dis‐
590       tantly) based on the "wwwurl.pl" code in the libwww-perl for perl4
591       developed by Roy Fielding, as part of the Arcadia project at the Uni‐
592       versity of California, Irvine, with contributions from Brooks Cutter.
593
594       "URI::URL" was developed by Gisle Aas, Tim Bunce, Roy Fielding and Mar‐
595       tijn Koster with input from other people on the libwww-perl mailing
596       list.
597
598       "URI" and related subclasses was developed by Gisle Aas.
599
600
601
602perl v5.8.8                       2004-01-14                            URI(3)
Impressum