1URI(3) User Contributed Perl Documentation URI(3)
2
3
4
6 URI - Uniform Resource Identifiers (absolute and relative)
7
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
28 This module implements the "URI" class. Objects of this class
29 represent "Uniform Resource Identifier references" as specified in RFC
30 2396 (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
34 Identifier can be further classified as either a Uniform Resource
35 Locator (URL) or a Uniform Resource Name (URN). The distinction
36 between URL and URN does not matter to the "URI" class interface. A
37 "URI-reference" is a URI that may have additional information attached
38 in the form of a 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
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
77 available).
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
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
112 (represented as "undef"). If an accessor method is given an argument,
113 it updates the corresponding component in addition to returning the old
114 value of the component. Passing an undefined argument removes the
115 component (if possible). The description of each accessor method
116 indicates 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
125 relative, then $uri->scheme returns "undef". If called with an
126 argument, it updates the scheme of $uri, possibly changing the
127 class of $uri, and returns the old scheme value. The method croaks
128 if the new scheme name is illegal; a scheme name must begin with a
129 letter and must consist of only US-ASCII letters, numbers, and a
130 few special marks: ".", "+", "-". This restriction effectively
131 means that the scheme must be passed unescaped. Passing an
132 undefined argument to the scheme method makes the URI relative (if
133 possible).
134
135 Letter case does not matter for scheme names. The string returned
136 by $uri->scheme is always lowercase. If you want the scheme just
137 as it was written in the URI in its original case, you can use the
138 $uri->_scheme method instead.
139
140 $uri->opaque
141 $uri->opaque( $new_opaque )
142 Sets and returns the scheme-specific part of the $uri (everything
143 between the scheme and the fragment) as an escaped string.
144
145 $uri->path
146 $uri->path( $new_path )
147 Sets and returns the same value as $uri->opaque unless the URI
148 supports the generic syntax for hierarchical namespaces. In that
149 case the generic method is overridden to set and return the part of
150 the URI between the host name and the fragment.
151
152 $uri->fragment
153 $uri->fragment( $new_frag )
154 Returns the fragment identifier of a URI reference as an escaped
155 string.
156
157 $uri->as_string
158 Returns a URI object to a plain string. URI objects are also
159 converted to plain strings automatically by overloading. This
160 means that $uri objects can be used as plain strings in most Perl
161 constructs.
162
163 $uri->canonical
164 Returns a normalized version of the URI. The rules for
165 normalization are scheme-dependent. They usually involve
166 lowercasing the scheme and Internet host name components, removing
167 the explicit port specification if it matches the default port,
168 uppercasing all escape sequences, and unescaping octets that can be
169 better represented as plain characters.
170
171 For efficiency reasons, if the $uri is already in normalized form,
172 then a reference to it is returned instead of a copy.
173
174 $uri->eq( $other_uri )
175 URI::eq( $first_uri, $other_uri )
176 Tests whether two URI references are equal. URI references that
177 normalize to the same string are considered equal. The method can
178 also be used as a plain function which can also test two string
179 arguments.
180
181 If you need to test whether two "URI" object references denote the
182 same object, use the '==' operator.
183
184 $uri->abs( $base_uri )
185 Returns an absolute URI reference. If $uri is already absolute,
186 then a reference to it is simply returned. If the $uri is
187 relative, then a new absolute URI is constructed by combining the
188 $uri and the $base_uri, and returned.
189
190 $uri->rel( $base_uri )
191 Returns a relative URI reference if it is possible to make one that
192 denotes the same resource relative to $base_uri. If not, then $uri
193 is simply returned.
194
196 The following methods are available to schemes that use the
197 common/generic syntax for hierarchical namespaces. The descriptions of
198 schemes below indicate which these are. Unknown schemes are assumed to
199 support the generic syntax, and therefore the following methods:
200
201 $uri->authority
202 $uri->authority( $new_authority )
203 Sets and returns the escaped authority component of the $uri.
204
205 $uri->path
206 $uri->path( $new_path )
207 Sets and returns the escaped path component of the $uri (the part
208 between the host name and the query or fragment). The path can
209 never be undefined, but it can be the empty string.
210
211 $uri->path_query
212 $uri->path_query( $new_path_query )
213 Sets and returns the escaped path and query components as a single
214 entity. The path and the query are separated by a "?" character,
215 but the query can itself contain "?".
216
217 $uri->path_segments
218 $uri->path_segments( $segment, ... )
219 Sets and returns the path. In a scalar context, it returns the
220 same value as $uri->path. In a list context, it returns the
221 unescaped path segments that make up the path. Path segments that
222 have parameters are returned as an anonymous array. The first
223 element is the unescaped path segment proper; subsequent elements
224 are escaped parameter strings. Such an anonymous array uses
225 overloading so it can be treated as a string too, but this string
226 does not include the parameters.
227
228 Note that absolute paths have the empty string as their first
229 path_segment, i.e. the path "/foo/bar" have 3 path_segments; "",
230 "foo" and "bar".
231
232 $uri->query
233 $uri->query( $new_query )
234 Sets and returns the escaped query component of the $uri.
235
236 $uri->query_form
237 $uri->query_form( $key1 => $val1, $key2 => $val2, ... )
238 $uri->query_form( $key1 => $val1, $key2 => $val2, ..., $delim )
239 $uri->query_form( \@key_value_pairs )
240 $uri->query_form( \@key_value_pairs, $delim )
241 $uri->query_form( \%hash )
242 $uri->query_form( \%hash, $delim )
243 Sets and returns query components that use the
244 application/x-www-form-urlencoded format. Key/value pairs are
245 separated by "&", and the key is separated from the value by a "="
246 character.
247
248 The form can be set either by passing separate key/value pairs, or
249 via an array or hash reference. Passing an empty array or an empty
250 hash removes the query component, whereas passing no arguments at
251 all leaves the component unchanged. The order of keys is undefined
252 if a hash reference is passed. The old value is always returned as
253 a list of separate key/value pairs. Assigning this list to a hash
254 is unwise as the keys returned might repeat.
255
256 The values passed when setting the form can be plain strings or
257 references to arrays of strings. Passing an array of values has
258 the same effect as passing the key repeatedly with one value at a
259 time. All the following statements have the same effect:
260
261 $uri->query_form(foo => 1, foo => 2);
262 $uri->query_form(foo => [1, 2]);
263 $uri->query_form([ foo => 1, foo => 2 ]);
264 $uri->query_form([ foo => [1, 2] ]);
265 $uri->query_form({ foo => [1, 2] });
266
267 The $delim parameter can be passed as ";" to force the key/value
268 pairs to be delimited by ";" instead of "&" in the query string.
269 This practice is often recommended for URLs embedded in HTML or XML
270 documents as this avoids the trouble of escaping the "&" character.
271 You might also set the $URI::DEFAULT_QUERY_FORM_DELIMITER variable
272 to ";" for the same global effect.
273
274 The "URI::QueryParam" module can be loaded to add further methods
275 to manipulate the form of a URI. See URI::QueryParam for details.
276
277 $uri->query_keywords
278 $uri->query_keywords( $keywords, ... )
279 $uri->query_keywords( \@keywords )
280 Sets and returns query components that use the keywords separated
281 by "+" format.
282
283 The keywords can be set either by passing separate keywords
284 directly or by passing a reference to an array of keywords.
285 Passing an empty array removes the query component, whereas passing
286 no arguments at all leaves the component unchanged. The old value
287 is always returned as a list of separate words.
288
290 For schemes where the authority component denotes an Internet host, the
291 following methods are available in addition to the generic methods.
292
293 $uri->userinfo
294 $uri->userinfo( $new_userinfo )
295 Sets and returns the escaped userinfo part of the authority
296 component.
297
298 For some schemes this is a user name and a password separated by a
299 colon. This practice is not recommended. Embedding passwords in
300 clear text (such as URI) has proven to be a security risk in almost
301 every case where it has been used.
302
303 $uri->host
304 $uri->host( $new_host )
305 Sets and returns the unescaped hostname.
306
307 If the $new_host string ends with a colon and a number, then this
308 number also sets the port.
309
310 $uri->port
311 $uri->port( $new_port )
312 Sets and returns the port. The port is a simple integer that
313 should be greater than 0.
314
315 If a port is not specified explicitly in the URI, then the URI
316 scheme's default port is returned. If you don't want the default
317 port substituted, then you can use the $uri->_port method instead.
318
319 $uri->host_port
320 $uri->host_port( $new_host_port )
321 Sets and returns the host and port as a single unit. The returned
322 value includes a port, even if it matches the default port. The
323 host part and the port part are separated by a colon: ":".
324
325 $uri->default_port
326 Returns the default port of the URI scheme to which $uri belongs.
327 For http this is the number 80, for ftp this is the number 21, etc.
328 The default port for a scheme can not be changed.
329
331 Scheme-specific support is provided for the following URI schemes. For
332 "URI" objects that do not belong to one of these, you can only use the
333 common and generic methods.
334
335 data:
336 The data URI scheme is specified in RFC 2397. It allows inclusion
337 of small data items as "immediate" data, as if it had been included
338 externally.
339
340 "URI" objects belonging to the data scheme support the common
341 methods and two new methods to access their scheme-specific
342 components: $uri->media_type and $uri->data. See URI::data for
343 details.
344
345 file:
346 An old specification of the file URI scheme is found in RFC 1738.
347 A new RFC 2396 based specification in not available yet, but file
348 URI references are in common use.
349
350 "URI" objects belonging to the file scheme support the common and
351 generic methods. In addition, they provide two methods for mapping
352 file URIs back to local file names; $uri->file and $uri->dir. See
353 URI::file for details.
354
355 ftp:
356 An old specification of the ftp URI scheme is found in RFC 1738. A
357 new RFC 2396 based specification in not available yet, but ftp URI
358 references are in common use.
359
360 "URI" objects belonging to the ftp scheme support the common,
361 generic and server methods. In addition, they provide two methods
362 for accessing the userinfo sub-components: $uri->user and
363 $uri->password.
364
365 gopher:
366 The gopher URI scheme is specified in
367 <draft-murali-url-gopher-1996-12-04> and will hopefully be
368 available as a RFC 2396 based specification.
369
370 "URI" objects belonging to the gopher scheme support the common,
371 generic and server methods. In addition, they support some methods
372 for accessing gopher-specific path components: $uri->gopher_type,
373 $uri->selector, $uri->search, $uri->string.
374
375 http:
376 The http URI scheme is specified in RFC 2616. The scheme is used
377 to reference resources hosted by HTTP servers.
378
379 "URI" objects belonging to the http scheme support the common,
380 generic and server methods.
381
382 https:
383 The https URI scheme is a Netscape invention which is commonly
384 implemented. The scheme is used to reference HTTP servers through
385 SSL connections. Its syntax is the same as http, but the default
386 port is different.
387
388 ldap:
389 The ldap URI scheme is specified in RFC 2255. LDAP is the
390 Lightweight Directory Access Protocol. An ldap URI describes an
391 LDAP search operation to perform to retrieve information from an
392 LDAP directory.
393
394 "URI" objects belonging to the ldap scheme support the common,
395 generic and server methods as well as ldap-specific methods:
396 $uri->dn, $uri->attributes, $uri->scope, $uri->filter,
397 $uri->extensions. See URI::ldap for details.
398
399 ldapi:
400 Like the ldap URI scheme, but uses a UNIX domain socket. The
401 server methods are not supported, and the local socket path is
402 available as $uri->un_path. The ldapi scheme is used by the
403 OpenLDAP package. There is no real specification for it, but it is
404 mentioned in various OpenLDAP manual pages.
405
406 ldaps:
407 Like the ldap URI scheme, but uses an SSL connection. This scheme
408 is deprecated, as the preferred way is to use the start_tls
409 mechanism.
410
411 mailto:
412 The mailto URI scheme is specified in RFC 2368. The scheme was
413 originally used to designate the Internet mailing address of an
414 individual or service. It has (in RFC 2368) been extended to allow
415 setting of other mail header fields and the message body.
416
417 "URI" objects belonging to the mailto scheme support the common
418 methods and the generic query methods. In addition, they support
419 the following mailto-specific methods: $uri->to, $uri->headers.
420
421 mms:
422 The mms URL specification can be found at <http://sdp.ppona.com/>
423 "URI" objects belonging to the mms scheme support the common,
424 generic, and server methods, with the exception of userinfo and
425 query-related sub-components.
426
427 news:
428 The news, nntp and snews URI schemes are specified in
429 <draft-gilman-news-url-01> and will hopefully be available as an
430 RFC 2396 based specification soon.
431
432 "URI" objects belonging to the news scheme support the common,
433 generic and server methods. In addition, they provide some methods
434 to access the path: $uri->group and $uri->message.
435
436 nntp:
437 See news scheme.
438
439 pop:
440 The pop URI scheme is specified in RFC 2384. The scheme is used to
441 reference a POP3 mailbox.
442
443 "URI" objects belonging to the pop scheme support the common,
444 generic and server methods. In addition, they provide two methods
445 to access the userinfo components: $uri->user and $uri->auth
446
447 rlogin:
448 An old specification of the rlogin URI scheme is found in RFC 1738.
449 "URI" objects belonging to the rlogin scheme support the common,
450 generic and server methods.
451
452 rtsp:
453 The rtsp URL specification can be found in section 3.2 of RFC 2326.
454 "URI" objects belonging to the rtsp scheme support the common,
455 generic, and server methods, with the exception of userinfo and
456 query-related sub-components.
457
458 rtspu:
459 The rtspu URI scheme is used to talk to RTSP servers over UDP
460 instead of TCP. The syntax is the same as rtsp.
461
462 rsync:
463 Information about rsync is available from http://rsync.samba.org.
464 "URI" objects belonging to the rsync scheme support the common,
465 generic and server methods. In addition, they provide methods to
466 access the userinfo sub-components: $uri->user and $uri->password.
467
468 sip:
469 The sip URI specification is described in sections 19.1 and 25 of
470 RFC 3261. "URI" objects belonging to the sip scheme support the
471 common, generic, and server methods with the exception of path
472 related sub-components. In addition, they provide two methods to
473 get and set sip parameters: $uri->params_form and $uri->params.
474
475 sips:
476 See sip scheme. Its syntax is the same as sip, but the default
477 port is different.
478
479 snews:
480 See news scheme. Its syntax is the same as news, but the default
481 port is different.
482
483 telnet:
484 An old specification of the telnet URI scheme is found in RFC 1738.
485 "URI" objects belonging to the telnet scheme support the common,
486 generic and server methods.
487
488 tn3270:
489 These URIs are used like telnet URIs but for connections to IBM
490 mainframes. "URI" objects belonging to the tn3270 scheme support
491 the common, generic and server methods.
492
493 ssh:
494 Information about ssh is available at http://www.openssh.com/.
495 "URI" objects belonging to the ssh scheme support the common,
496 generic and server methods. In addition, they provide methods to
497 access the userinfo sub-components: $uri->user and $uri->password.
498
499 urn:
500 The syntax of Uniform Resource Names is specified in RFC 2141.
501 "URI" objects belonging to the urn scheme provide the common
502 methods, and also the methods $uri->nid and $uri->nss, which return
503 the Namespace Identifier and the Namespace-Specific String
504 respectively.
505
506 The Namespace Identifier basically works like the Scheme identifier
507 of URIs, and further divides the URN namespace. Namespace
508 Identifier assignments are maintained at
509 <http://www.iana.org/assignments/urn-namespaces>.
510
511 Letter case is not significant for the Namespace Identifier. It is
512 always returned in lower case by the $uri->nid method. The
513 $uri->_nid method can be used if you want it in its original case.
514
515 urn:isbn:
516 The "urn:isbn:" namespace contains International Standard Book
517 Numbers (ISBNs) and is described in RFC 3187. A "URI" object
518 belonging to this namespace has the following extra methods (if the
519 Business::ISBN module is available): $uri->isbn,
520 $uri->isbn_publisher_code, $uri->isbn_group_code (formerly
521 isbn_country_code, which is still supported by issues a deprecation
522 warning), $uri->isbn_as_ean.
523
524 urn:oid:
525 The "urn:oid:" namespace contains Object Identifiers (OIDs) and is
526 described in RFC 3061. An object identifier consists of sequences
527 of digits separated by dots. A "URI" object belonging to this
528 namespace has an additional method called $uri->oid that can be
529 used to get/set the oid value. In a list context, oid numbers are
530 returned as separate elements.
531
533 The following configuration variables influence how the class and its
534 methods behave:
535
536 $URI::ABS_ALLOW_RELATIVE_SCHEME
537 Some older parsers used to allow the scheme name to be present in
538 the relative URL if it was the same as the base URL scheme. RFC
539 2396 says that this should be avoided, but you can enable this old
540 behaviour by setting the $URI::ABS_ALLOW_RELATIVE_SCHEME variable
541 to a TRUE value. The difference is demonstrated by the following
542 examples:
543
544 URI->new("http:foo")->abs("http://host/a/b")
545 ==> "http:foo"
546
547 local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
548 URI->new("http:foo")->abs("http://host/a/b")
549 ==> "http:/host/a/foo"
550
551 $URI::ABS_REMOTE_LEADING_DOTS
552 You can also have the abs() method ignore excess ".." segments in
553 the relative URI by setting $URI::ABS_REMOTE_LEADING_DOTS to a TRUE
554 value. The difference is demonstrated by the following examples:
555
556 URI->new("../../../foo")->abs("http://host/a/b")
557 ==> "http://host/../../foo"
558
559 local $URI::ABS_REMOTE_LEADING_DOTS = 1;
560 URI->new("../../../foo")->abs("http://host/a/b")
561 ==> "http://host/foo"
562
563 $URI::DEFAULT_QUERY_FORM_DELIMITER
564 This value can be set to ";" to have the query form "key=value"
565 pairs delimited by ";" instead of "&" which is the default.
566
568 Using regexp variables like $1 directly as arguments to the URI methods
569 does not work too well with current perl implementations. I would
570 argue that this is actually a bug in perl. The workaround is to quote
571 them. Example:
572
573 /(...)/ || die;
574 $u->query("$1");
575
577 As an alternative to this module, the following (official) regular
578 expression can be used to decode a URI:
579
580 my($scheme, $authority, $path, $query, $fragment) =
581 $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
582
583 The "URI::Split" module provides the function uri_split() as a readable
584 alternative.
585
587 URI::file, URI::WithBase, URI::QueryParam, URI::Escape, URI::Split,
588 URI::Heuristic
589
590 RFC 2396: "Uniform Resource Identifiers (URI): Generic Syntax",
591 Berners-Lee, Fielding, Masinter, August 1998.
592
593 http://www.iana.org/assignments/uri-schemes
594
595 http://www.iana.org/assignments/urn-namespaces
596
597 http://www.w3.org/Addressing/
598
600 Copyright 1995-2004,2008 Gisle Aas.
601
602 Copyright 1995 Martijn Koster.
603
604 This program is free software; you can redistribute it and/or modify it
605 under the same terms as Perl itself.
606
608 This module is based on the "URI::URL" module, which in turn was
609 (distantly) based on the "wwwurl.pl" code in the libwww-perl for perl4
610 developed by Roy Fielding, as part of the Arcadia project at the
611 University of California, Irvine, with contributions from Brooks
612 Cutter.
613
614 "URI::URL" was developed by Gisle Aas, Tim Bunce, Roy Fielding and
615 Martijn Koster with input from other people on the libwww-perl mailing
616 list.
617
618 "URI" and related subclasses was developed by Gisle Aas.
619
620
621
622perl v5.10.1 2009-08-14 URI(3)