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