home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _a75112b572e591fc9841db1d6f5ff0b8 < prev    next >
Encoding:
Text File  |  2004-06-01  |  30.3 KB  |  1,010 lines

  1. package URI;
  2.  
  3. use strict;
  4. use vars qw($VERSION);
  5. $VERSION = "1.30"; # $Date: 2004/01/14 13:59:48 $
  6.  
  7. use vars qw($ABS_REMOTE_LEADING_DOTS $ABS_ALLOW_RELATIVE_SCHEME);
  8.  
  9. my %implements;  # mapping from scheme to implementor class
  10.  
  11. # Some "official" character classes
  12.  
  13. use vars qw($reserved $mark $unreserved $uric $scheme_re);
  14. $reserved   = q(;/?:@&=+$,[]);
  15. $mark       = q(-_.!~*'());                                    #'; emacs
  16. $unreserved = "A-Za-z0-9\Q$mark\E";
  17. $uric       = quotemeta($reserved) . $unreserved . "%";
  18.  
  19. $scheme_re  = '[a-zA-Z][a-zA-Z0-9.+\-]*';
  20.  
  21. use Carp ();
  22. use URI::Escape ();
  23.  
  24. use overload ('""'     => sub { ${$_[0]} },
  25.           '=='     => sub { overload::StrVal($_[0]) eq
  26.                                 overload::StrVal($_[1])
  27.                               },
  28.               fallback => 1,
  29.              );
  30.  
  31. sub new
  32. {
  33.     my($class, $uri, $scheme) = @_;
  34.  
  35.     $uri = defined ($uri) ? "$uri" : "";   # stringify
  36.     # Get rid of potential wrapping
  37.     $uri =~ s/^<(?:URL:)?(.*)>$/$1/;  # 
  38.     $uri =~ s/^"(.*)"$/$1/;
  39.     $uri =~ s/^\s+//;
  40.     $uri =~ s/\s+$//;
  41.  
  42.     my $impclass;
  43.     if ($uri =~ m/^($scheme_re):/so) {
  44.     $scheme = $1;
  45.     }
  46.     else {
  47.     if (($impclass = ref($scheme))) {
  48.         $scheme = $scheme->scheme;
  49.     }
  50.     elsif ($scheme && $scheme =~ m/^($scheme_re)(?::|$)/o) {
  51.         $scheme = $1;
  52.         }
  53.     }
  54.     $impclass ||= implementor($scheme) ||
  55.     do {
  56.         require URI::_foreign;
  57.         $impclass = 'URI::_foreign';
  58.     };
  59.  
  60.     return $impclass->_init($uri, $scheme);
  61. }
  62.  
  63.  
  64. sub new_abs
  65. {
  66.     my($class, $uri, $base) = @_;
  67.     $uri = $class->new($uri, $base);
  68.     $uri->abs($base);
  69. }
  70.  
  71.  
  72. sub _init
  73. {
  74.     my $class = shift;
  75.     my($str, $scheme) = @_;
  76.     $str =~ s/([^$uric\#])/$URI::Escape::escapes{$1}/go;
  77.     $str = "$scheme:$str" unless $str =~ /^$scheme_re:/o ||
  78.                                  $class->_no_scheme_ok;
  79.     my $self = bless \$str, $class;
  80.     $self;
  81. }
  82.  
  83.  
  84. sub implementor
  85. {
  86.     my($scheme, $impclass) = @_;
  87.     if (!$scheme || $scheme !~ /\A$scheme_re\z/o) {
  88.     require URI::_generic;
  89.     return "URI::_generic";
  90.     }
  91.  
  92.     $scheme = lc($scheme);
  93.  
  94.     if ($impclass) {
  95.     # Set the implementor class for a given scheme
  96.         my $old = $implements{$scheme};
  97.         $impclass->_init_implementor($scheme);
  98.         $implements{$scheme} = $impclass;
  99.         return $old;
  100.     }
  101.  
  102.     my $ic = $implements{$scheme};
  103.     return $ic if $ic;
  104.  
  105.     # scheme not yet known, look for internal or
  106.     # preloaded (with 'use') implementation
  107.     $ic = "URI::$scheme";  # default location
  108.  
  109.     # turn scheme into a valid perl identifier by a simple tranformation...
  110.     $ic =~ s/\+/_P/g;
  111.     $ic =~ s/\./_O/g;
  112.     $ic =~ s/\-/_/g;
  113.  
  114.     no strict 'refs';
  115.     # check we actually have one for the scheme:
  116.     unless (@{"${ic}::ISA"}) {
  117.         # Try to load it
  118.         eval "require $ic";
  119.         die $@ if $@ && $@ !~ /Can\'t locate.*in \@INC/;
  120.         return unless @{"${ic}::ISA"};
  121.     }
  122.  
  123.     $ic->_init_implementor($scheme);
  124.     $implements{$scheme} = $ic;
  125.     $ic;
  126. }
  127.  
  128.  
  129. sub _init_implementor
  130. {
  131.     my($class, $scheme) = @_;
  132.     # Remember that one implementor class may actually
  133.     # serve to implement several URI schemes.
  134. }
  135.  
  136.  
  137. sub clone
  138. {
  139.     my $self = shift;
  140.     my $other = $$self;
  141.     bless \$other, ref $self;
  142. }
  143.  
  144.  
  145. sub _no_scheme_ok { 0 }
  146.  
  147. sub _scheme
  148. {
  149.     my $self = shift;
  150.  
  151.     unless (@_) {
  152.     return unless $$self =~ /^($scheme_re):/o;
  153.     return $1;
  154.     }
  155.  
  156.     my $old;
  157.     my $new = shift;
  158.     if (defined($new) && length($new)) {
  159.     Carp::croak("Bad scheme '$new'") unless $new =~ /^$scheme_re$/o;
  160.     $old = $1 if $$self =~ s/^($scheme_re)://o;
  161.     my $newself = URI->new("$new:$$self");
  162.     $$self = $$newself; 
  163.     bless $self, ref($newself);
  164.     }
  165.     else {
  166.     if ($self->_no_scheme_ok) {
  167.         $old = $1 if $$self =~ s/^($scheme_re)://o;
  168.         Carp::carp("Oops, opaque part now look like scheme")
  169.         if $^W && $$self =~ m/^$scheme_re:/o
  170.     }
  171.     else {
  172.         $old = $1 if $$self =~ m/^($scheme_re):/o;
  173.     }
  174.     }
  175.  
  176.     return $old;
  177. }
  178.  
  179. sub scheme
  180. {
  181.     my $scheme = shift->_scheme(@_);
  182.     return unless defined $scheme;
  183.     lc($scheme);
  184. }
  185.  
  186.  
  187. sub opaque
  188. {
  189.     my $self = shift;
  190.  
  191.     unless (@_) {
  192.     $$self =~ /^(?:$scheme_re:)?([^\#]*)/o or die;
  193.     return $1;
  194.     }
  195.  
  196.     $$self =~ /^($scheme_re:)?    # optional scheme
  197.             ([^\#]*)          # opaque
  198.                 (\#.*)?           # optional fragment
  199.               $/sx or die;
  200.  
  201.     my $old_scheme = $1;
  202.     my $old_opaque = $2;
  203.     my $old_frag   = $3;
  204.  
  205.     my $new_opaque = shift;
  206.     $new_opaque = "" unless defined $new_opaque;
  207.     $new_opaque =~ s/([^$uric])/$URI::Escape::escapes{$1}/go;
  208.  
  209.     $$self = defined($old_scheme) ? $old_scheme : "";
  210.     $$self .= $new_opaque;
  211.     $$self .= $old_frag if defined $old_frag;
  212.  
  213.     $old_opaque;
  214. }
  215.  
  216. *path = \&opaque;  # alias
  217.  
  218.  
  219. sub fragment
  220. {
  221.     my $self = shift;
  222.     unless (@_) {
  223.     return unless $$self =~ /\#(.*)/s;
  224.     return $1;
  225.     }
  226.  
  227.     my $old;
  228.     $old = $1 if $$self =~ s/\#(.*)//s;
  229.  
  230.     my $new_frag = shift;
  231.     if (defined $new_frag) {
  232.     $new_frag =~ s/([^$uric])/$URI::Escape::escapes{$1}/go;
  233.     $$self .= "#$new_frag";
  234.     }
  235.     $old;
  236. }
  237.  
  238.  
  239. sub as_string
  240. {
  241.     my $self = shift;
  242.     $$self;
  243. }
  244.  
  245.  
  246. sub canonical
  247. {
  248.     my $self = shift;
  249.  
  250.     # Make sure scheme is lowercased
  251.     my $scheme = $self->_scheme || "";
  252.     my $uc_scheme = $scheme =~ /[A-Z]/;
  253.     my $lc_esc    = $$self =~ /%(?:[a-f][a-fA-F0-9]|[A-F0-9][a-f])/;
  254.     if ($uc_scheme || $lc_esc) {
  255.     my $other = $self->clone;
  256.     $other->_scheme(lc $scheme) if $uc_scheme;
  257.     $$other =~ s/(%(?:[a-f][a-fA-F0-9]|[A-F0-9][a-f]))/uc($1)/ge
  258.         if $lc_esc;
  259.     return $other;
  260.     }
  261.     $self;
  262. }
  263.  
  264. # Compare two URIs, subclasses will provide a more correct implementation
  265. sub eq {
  266.     my($self, $other) = @_;
  267.     $self  = URI->new($self, $other) unless ref $self;
  268.     $other = URI->new($other, $self) unless ref $other;
  269.     ref($self) eq ref($other) &&                # same class
  270.     $self->canonical->as_string eq $other->canonical->as_string;
  271. }
  272.  
  273. # generic-URI transformation methods
  274. sub abs { $_[0]; }
  275. sub rel { $_[0]; }
  276.  
  277. # help out Storable
  278. sub STORABLE_freeze {
  279.        my($self, $cloning) = @_;
  280.        return $$self;
  281. }
  282.  
  283. sub STORABLE_thaw {
  284.        my($self, $cloning, $str) = @_;
  285.        $$self = $str;
  286. }
  287.  
  288. 1;
  289.  
  290. __END__
  291.  
  292. =head1 NAME
  293.  
  294. URI - Uniform Resource Identifiers (absolute and relative)
  295.  
  296. =head1 SYNOPSIS
  297.  
  298.  $u1 = URI->new("http://www.perl.com");
  299.  $u2 = URI->new("foo", "http");
  300.  $u3 = $u2->abs($u1);
  301.  $u4 = $u3->clone;
  302.  $u5 = URI->new("HTTP://WWW.perl.com:80")->canonical;
  303.  
  304.  $str = $u->as_string;
  305.  $str = "$u";
  306.  
  307.  $scheme = $u->scheme;
  308.  $opaque = $u->opaque;
  309.  $path   = $u->path;
  310.  $frag   = $u->fragment;
  311.  
  312.  $u->scheme("ftp");
  313.  $u->host("ftp.perl.com");
  314.  $u->path("cpan/");
  315.  
  316. =head1 DESCRIPTION
  317.  
  318. This module implements the C<URI> class.  Objects of this class
  319. represent "Uniform Resource Identifier references" as specified in RFC
  320. 2396 (and updated by RFC 2732).
  321.  
  322. A Uniform Resource Identifier is a compact string of characters that
  323. identifies an abstract or physical resource.  A Uniform Resource
  324. Identifier can be further classified as either a Uniform Resource Locator
  325. (URL) or a Uniform Resource Name (URN).  The distinction between URL
  326. and URN does not matter to the C<URI> class interface. A
  327. "URI-reference" is a URI that may have additional information attached
  328. in the form of a fragment identifier.
  329.  
  330. An absolute URI reference consists of three parts:  a I<scheme>, a
  331. I<scheme-specific part> and a I<fragment> identifier.  A subset of URI
  332. references share a common syntax for hierarchical namespaces.  For
  333. these, the scheme-specific part is further broken down into
  334. I<authority>, I<path> and I<query> components.  These URIs can also
  335. take the form of relative URI references, where the scheme (and
  336. usually also the authority) component is missing, but implied by the
  337. context of the URI reference.  The three forms of URI reference
  338. syntax are summarized as follows:
  339.  
  340.   <scheme>:<scheme-specific-part>#<fragment>
  341.   <scheme>://<authority><path>?<query>#<fragment>
  342.   <path>?<query>#<fragment>
  343.  
  344. The components into which a URI reference can be divided depend on the
  345. I<scheme>.  The C<URI> class provides methods to get and set the
  346. individual components.  The methods available for a specific
  347. C<URI> object depend on the scheme.
  348.  
  349. =head1 CONSTRUCTORS
  350.  
  351. The following methods construct new C<URI> objects:
  352.  
  353. =over 4
  354.  
  355. =item $uri = URI->new( $str )
  356.  
  357. =item $uri = URI->new( $str, $scheme )
  358.  
  359. Constructs a new URI object.  The string
  360. representation of a URI is given as argument, together with an optional
  361. scheme specification.  Common URI wrappers like "" and <>, as well as
  362. leading and trailing white space, are automatically removed from
  363. the $str argument before it is processed further.
  364.  
  365. The constructor determines the scheme, maps this to an appropriate
  366. URI subclass, constructs a new object of that class and returns it.
  367.  
  368. The $scheme argument is only used when $str is a
  369. relative URI.  It can be either a simple string that
  370. denotes the scheme, a string containing an absolute URI reference, or
  371. an absolute C<URI> object.  If no $scheme is specified for a relative
  372. URI $str, then $str is simply treated as a generic URI (no scheme-specific
  373. methods available).
  374.  
  375. The set of characters available for building URI references is
  376. restricted (see L<URI::Escape>).  Characters outside this set are
  377. automatically escaped by the URI constructor.
  378.  
  379. =item $uri = URI->new_abs( $str, $base_uri )
  380.  
  381. Constructs a new absolute URI object.  The $str argument can
  382. denote a relative or absolute URI.  If relative, then it is
  383. absolutized using $base_uri as base. The $base_uri must be an absolute
  384. URI.
  385.  
  386. =item $uri = URI::file->new( $filename )
  387.  
  388. =item $uri = URI::file->new( $filename, $os )
  389.  
  390. Constructs a new I<file> URI from a file name.  See L<URI::file>.
  391.  
  392. =item $uri = URI::file->new_abs( $filename )
  393.  
  394. =item $uri = URI::file->new_abs( $filename, $os )
  395.  
  396. Constructs a new absolute I<file> URI from a file name.  See
  397. L<URI::file>.
  398.  
  399. =item $uri = URI::file->cwd
  400.  
  401. Returns the current working directory as a I<file> URI.  See
  402. L<URI::file>.
  403.  
  404. =item $uri->clone
  405.  
  406. Returns a copy of the $uri.
  407.  
  408. =back
  409.  
  410. =head1 COMMON METHODS
  411.  
  412. The methods described in this section are available for all C<URI>
  413. objects.
  414.  
  415. Methods that give access to components of a URI always return the
  416. old value of the component.  The value returned is C<undef> if the
  417. component was not present.  There is generally a difference between a
  418. component that is empty (represented as C<"">) and a component that is
  419. missing (represented as C<undef>).  If an accessor method is given an
  420. argument, it updates the corresponding component in addition to
  421. returning the old value of the component.  Passing an undefined
  422. argument removes the component (if possible).  The description of
  423. each accessor method indicates whether the component is passed as
  424. an escaped or an unescaped string.  A component that can be further
  425. divided into sub-parts are usually passed escaped, as unescaping might
  426. change its semantics.
  427.  
  428. The common methods available for all URI are:
  429.  
  430. =over 4
  431.  
  432. =item $uri->scheme
  433.  
  434. =item $uri->scheme( $new_scheme )
  435.  
  436. Sets and returns the scheme part of the $uri.  If the $uri is
  437. relative, then $uri->scheme returns C<undef>.  If called with an
  438. argument, it updates the scheme of $uri, possibly changing the
  439. class of $uri, and returns the old scheme value.  The method croaks
  440. if the new scheme name is illegal; a scheme name must begin with a
  441. letter and must consist of only US-ASCII letters, numbers, and a few
  442. special marks: ".", "+", "-".  This restriction effectively means
  443. that the scheme must be passed unescaped.  Passing an undefined
  444. argument to the scheme method makes the URI relative (if possible).
  445.  
  446. Letter case does not matter for scheme names.  The string
  447. returned by $uri->scheme is always lowercase.  If you want the scheme
  448. just as it was written in the URI in its original case,
  449. you can use the $uri->_scheme method instead.
  450.  
  451. =item $uri->opaque
  452.  
  453. =item $uri->opaque( $new_opaque )
  454.  
  455. Sets and returns the scheme-specific part of the $uri
  456. (everything between the scheme and the fragment)
  457. as an escaped string.
  458.  
  459. =item $uri->path
  460.  
  461. =item $uri->path( $new_path )
  462.  
  463. Sets and returns the same value as $uri->opaque unless the URI
  464. supports the generic syntax for hierarchical namespaces.
  465. In that case the generic method is overridden to set and return
  466. the part of the URI between the I<host name> and the I<fragment>.
  467.  
  468. =item $uri->fragment
  469.  
  470. =item $uri->fragment( $new_frag )
  471.  
  472. Returns the fragment identifier of a URI reference
  473. as an escaped string.
  474.  
  475. =item $uri->as_string
  476.  
  477. Returns a URI object to a plain string.  URI objects are
  478. also converted to plain strings automatically by overloading.  This
  479. means that $uri objects can be used as plain strings in most Perl
  480. constructs.
  481.  
  482. =item $uri->canonical
  483.  
  484. Returns a normalized version of the URI.  The rules
  485. for normalization are scheme-dependent.  They usually involve
  486. lowercasing the scheme and Internet host name components,
  487. removing the explicit port specification if it matches the default port,
  488. uppercasing all escape sequences, and unescaping octets that can be
  489. better represented as plain characters.
  490.  
  491. For efficiency reasons, if the $uri is already in normalized form,
  492. then a reference to it is returned instead of a copy.
  493.  
  494. =item $uri->eq( $other_uri )
  495.  
  496. =item URI::eq( $first_uri, $other_uri )
  497.  
  498. Tests whether two URI references are equal.  URI references
  499. that normalize to the same string are considered equal.  The method
  500. can also be used as a plain function which can also test two string
  501. arguments.
  502.  
  503. If you need to test whether two C<URI> object references denote the
  504. same object, use the '==' operator.
  505.  
  506. =item $uri->abs( $base_uri )
  507.  
  508. Returns an absolute URI reference.  If $uri is already
  509. absolute, then a reference to it is simply returned.  If the $uri
  510. is relative, then a new absolute URI is constructed by combining the
  511. $uri and the $base_uri, and returned.
  512.  
  513. =item $uri->rel( $base_uri )
  514.  
  515. Returns a relative URI reference if it is possible to
  516. make one that denotes the same resource relative to $base_uri.
  517. If not, then $uri is simply returned.
  518.  
  519. =back
  520.  
  521. =head1 GENERIC METHODS
  522.  
  523. The following methods are available to schemes that use the
  524. common/generic syntax for hierarchical namespaces.  The descriptions of
  525. schemes below indicate which these are.  Unknown schemes are
  526. assumed to support the generic syntax, and therefore the following
  527. methods:
  528.  
  529. =over 4
  530.  
  531. =item $uri->authority
  532.  
  533. =item $uri->authority( $new_authority )
  534.  
  535. Sets and returns the escaped authority component
  536. of the $uri.
  537.  
  538. =item $uri->path
  539.  
  540. =item $uri->path( $new_path )
  541.  
  542. Sets and returns the escaped path component of
  543. the $uri (the part between the host name and the query or fragment).
  544. The path can never be undefined, but it can be the empty string.
  545.  
  546. =item $uri->path_query
  547.  
  548. =item $uri->path_query( $new_path_query )
  549.  
  550. Sets and returns the escaped path and query
  551. components as a single entity.  The path and the query are
  552. separated by a "?" character, but the query can itself contain "?".
  553.  
  554. =item $uri->path_segments
  555.  
  556. =item $uri->path_segments( $segment, ... )
  557.  
  558. Sets and returns the path.  In a scalar context, it returns
  559. the same value as $uri->path.  In a list context, it returns the
  560. unescaped path segments that make up the path.  Path segments that
  561. have parameters are returned as an anonymous array.  The first element
  562. is the unescaped path segment proper;  subsequent elements are escaped
  563. parameter strings.  Such an anonymous array uses overloading so it can
  564. be treated as a string too, but this string does not include the
  565. parameters.
  566.  
  567. =item $uri->query
  568.  
  569. =item $uri->query( $new_query )
  570.  
  571. Sets and returns the escaped query component of
  572. the $uri.
  573.  
  574. =item $uri->query_form
  575.  
  576. =item $uri->query_form( $key1 => $val1, $key2 => $val2, ... )
  577.  
  578. =item $uri->query_form( \@key_value_pairs )
  579.  
  580. =item $uri->query_form( \%hash )
  581.  
  582. Sets and returns query components that use the
  583. I<application/x-www-form-urlencoded> format.  Key/value pairs are
  584. separated by "&", and the key is separated from the value by a "="
  585. character.
  586.  
  587. The form can be set either by passing separate key/value pairs, or via
  588. an array or hash reference.  Passing an empty array or an empty hash
  589. removes the query component, whereas passing no arguments at all leaves
  590. the component unchanged.  The order of keys is undefined if a hash
  591. reference is passed.  The old value is always returned as a list of
  592. separate key/value pairs.  Assigning this list to a hash is unwise as
  593. the keys returned might repeat.
  594.  
  595. The values passed when setting the form can be plain strings or
  596. references to arrays of strings.  Passing an array of values has the
  597. same effect as passing the key repeatedly with one value at a time.
  598. All the following statements have the same effect:
  599.  
  600.     $uri->query_form(foo => 1, foo => 2);
  601.     $uri->query_form(foo => [1, 2]);
  602.     $uri->query_form([ foo => 1, foo => 2 ]);
  603.     $uri->query_form([ foo => [1, 2] ]);
  604.     $uri->query_form({ foo => [1, 2] });
  605.  
  606. The C<URI::QueryParam> module can be loaded to add further methods to
  607. manipulate the form of a URI.  See L<URI::QueryParam> for details.
  608.  
  609. =item $uri->query_keywords
  610.  
  611. =item $uri->query_keywords( $keywords, ... )
  612.  
  613. =item $uri->query_keywords( \@keywords )
  614.  
  615. Sets and returns query components that use the
  616. keywords separated by "+" format.
  617.  
  618. The keywords can be set either by passing separate keywords directly
  619. or by passing a reference to an array of keywords.  Passing an empty
  620. array removes the query component, whereas passing no arguments at
  621. all leaves the component unchanged.  The old value is always returned
  622. as a list of separate words.
  623.  
  624. =back
  625.  
  626. =head1 SERVER METHODS
  627.  
  628. For schemes where the I<authority> component denotes an Internet host,
  629. the following methods are available in addition to the generic
  630. methods.
  631.  
  632. =over 4
  633.  
  634. =item $uri->userinfo
  635.  
  636. =item $uri->userinfo( $new_userinfo )
  637.  
  638. Sets and returns the escaped userinfo part of the
  639. authority component.
  640.  
  641. For some schemes this is a user name and a password separated by
  642. a colon.  This practice is not recommended. Embedding passwords in
  643. clear text (such as URI) has proven to be a security risk in almost
  644. every case where it has been used.
  645.  
  646. =item $uri->host
  647.  
  648. =item $uri->host( $new_host )
  649.  
  650. Sets and returns the unescaped hostname.
  651.  
  652. If the $new_host string ends with a colon and a number, then this
  653. number also sets the port.
  654.  
  655. =item $uri->port
  656.  
  657. =item $uri->port( $new_port )
  658.  
  659. Sets and returns the port.  The port is a simple integer
  660. that should be greater than 0.
  661.  
  662. If a port is not specified explicitly in the URI, then the URI scheme's default port
  663. is returned. If you don't want the default port
  664. substituted, then you can use the $uri->_port method instead.
  665.  
  666. =item $uri->host_port
  667.  
  668. =item $uri->host_port( $new_host_port )
  669.  
  670. Sets and returns the host and port as a single
  671. unit.  The returned value includes a port, even if it matches the
  672. default port.  The host part and the port part are separated by a
  673. colon: ":".
  674.  
  675. =item $uri->default_port
  676.  
  677. Returns the default port of the URI scheme to which $uri
  678. belongs.  For I<http> this is the number 80, for I<ftp> this
  679. is the number 21, etc.  The default port for a scheme can not be
  680. changed.
  681.  
  682. =back
  683.  
  684. =head1 SCHEME-SPECIFIC SUPPORT
  685.  
  686. Scheme-specific support is provided for the following URI schemes.  For C<URI>
  687. objects that do not belong to one of these, you can only use the common and
  688. generic methods.
  689.  
  690. =over 4
  691.  
  692. =item B<data>:
  693.  
  694. The I<data> URI scheme is specified in RFC 2397.  It allows inclusion
  695. of small data items as "immediate" data, as if it had been included
  696. externally.
  697.  
  698. C<URI> objects belonging to the data scheme support the common methods
  699. and two new methods to access their scheme-specific components:
  700. $uri->media_type and $uri->data.  See L<URI::data> for details.
  701.  
  702. =item B<file>:
  703.  
  704. An old specification of the I<file> URI scheme is found in RFC 1738.
  705. A new RFC 2396 based specification in not available yet, but file URI
  706. references are in common use.
  707.  
  708. C<URI> objects belonging to the file scheme support the common and
  709. generic methods.  In addition, they provide two methods for mapping file URIs
  710. back to local file names; $uri->file and $uri->dir.  See L<URI::file>
  711. for details.
  712.  
  713. =item B<ftp>:
  714.  
  715. An old specification of the I<ftp> URI scheme is found in RFC 1738.  A
  716. new RFC 2396 based specification in not available yet, but ftp URI
  717. references are in common use.
  718.  
  719. C<URI> objects belonging to the ftp scheme support the common,
  720. generic and server methods.  In addition, they provide two methods for
  721. accessing the userinfo sub-components: $uri->user and $uri->password.
  722.  
  723. =item B<gopher>:
  724.  
  725. The I<gopher> URI scheme is specified in
  726. <draft-murali-url-gopher-1996-12-04> and will hopefully be available
  727. as a RFC 2396 based specification.
  728.  
  729. C<URI> objects belonging to the gopher scheme support the common,
  730. generic and server methods. In addition, they support some methods for
  731. accessing gopher-specific path components: $uri->gopher_type,
  732. $uri->selector, $uri->search, $uri->string.
  733.  
  734. =item B<http>:
  735.  
  736. The I<http> URI scheme is specified in RFC 2616.
  737. The scheme is used to reference resources hosted by HTTP servers.
  738.  
  739. C<URI> objects belonging to the http scheme support the common,
  740. generic and server methods.
  741.  
  742. =item B<https>:
  743.  
  744. The I<https> URI scheme is a Netscape invention which is commonly
  745. implemented.  The scheme is used to reference HTTP servers through SSL
  746. connections.  Its syntax is the same as http, but the default
  747. port is different.
  748.  
  749. =item B<ldap>:
  750.  
  751. The I<ldap> URI scheme is specified in RFC 2255.  LDAP is the
  752. Lightweight Directory Access Protocol.  An ldap URI describes an LDAP
  753. search operation to perform to retrieve information from an LDAP
  754. directory.
  755.  
  756. C<URI> objects belonging to the ldap scheme support the common,
  757. generic and server methods as well as ldap-specific methods: $uri->dn,
  758. $uri->attributes, $uri->scope, $uri->filter, $uri->extensions.  See
  759. L<URI::ldap> for details.
  760.  
  761. =item B<ldapi>:
  762.  
  763. Like the I<ldap> URI scheme, but uses a UNIX domain socket.  The
  764. server methods are not supported, and the local socket path is
  765. available as $uri->un_path.  The I<ldapi> scheme is used by the
  766. OpenLDAP package.  There is no real specification for it, but it is
  767. mentioned in various OpenLDAP manual pages.
  768.  
  769. =item B<ldaps>:
  770.  
  771. Like the I<ldap> URI scheme, but uses an SSL connection.  This
  772. scheme is deprecated, as the preferred way is to use the I<start_tls>
  773. mechanism.
  774.  
  775. =item B<mailto>:
  776.  
  777. The I<mailto> URI scheme is specified in RFC 2368.  The scheme was
  778. originally used to designate the Internet mailing address of an
  779. individual or service.  It has (in RFC 2368) been extended to allow
  780. setting of other mail header fields and the message body.
  781.  
  782. C<URI> objects belonging to the mailto scheme support the common
  783. methods and the generic query methods.  In addition, they support the
  784. following mailto-specific methods: $uri->to, $uri->headers.
  785.  
  786. =item B<mms>:
  787.  
  788. The I<mms> URL specification can be found at L<http://sdp.ppona.com/>
  789. C<URI> objects belonging to the mms scheme support the common,
  790. generic, and server methods, with the exception of userinfo and
  791. query-related sub-components.
  792.  
  793. =item B<news>:
  794.  
  795. The I<news>, I<nntp> and I<snews> URI schemes are specified in
  796. <draft-gilman-news-url-01> and will hopefully be available as an RFC
  797. 2396 based specification soon.
  798.  
  799. C<URI> objects belonging to the news scheme support the common,
  800. generic and server methods.  In addition, they provide some methods to
  801. access the path: $uri->group and $uri->message.
  802.  
  803. =item B<nntp>:
  804.  
  805. See I<news> scheme.
  806.  
  807. =item B<pop>:
  808.  
  809. The I<pop> URI scheme is specified in RFC 2384. The scheme is used to
  810. reference a POP3 mailbox.
  811.  
  812. C<URI> objects belonging to the pop scheme support the common, generic
  813. and server methods.  In addition, they provide two methods to access the
  814. userinfo components: $uri->user and $uri->auth
  815.  
  816. =item B<rlogin>:
  817.  
  818. An old specification of the I<rlogin> URI scheme is found in RFC
  819. 1738. C<URI> objects belonging to the rlogin scheme support the
  820. common, generic and server methods.
  821.  
  822. =item B<rtsp>:
  823.  
  824. The I<rtsp> URL specification can be found in section 3.2 of RFC 2326.
  825. C<URI> objects belonging to the rtsp scheme support the common,
  826. generic, and server methods, with the exception of userinfo and
  827. query-related sub-components.
  828.  
  829. =item B<rtspu>:
  830.  
  831. The I<rtspu> URI scheme is used to talk to RTSP servers over UDP
  832. instead of TCP.  The syntax is the same as rtsp.
  833.  
  834. =item B<rsync>:
  835.  
  836. Information about rsync is available from http://rsync.samba.org.
  837. C<URI> objects belonging to the rsync scheme support the common,
  838. generic and server methods.  In addition, they provide methods to
  839. access the userinfo sub-components: $uri->user and $uri->password.
  840.  
  841. =item B<sip>:
  842.  
  843. The I<sip> URI specification is described in sections 19.1 and 25
  844. of RFC 3261.  C<URI> objects belonging to the sip scheme support the
  845. common, generic, and server methods with the exception of path related
  846. sub-components.  In addition, they provide two methods to get and set
  847. I<sip> parameters: $uri->params_form and $uri->params.
  848.  
  849. =item B<sips>:
  850.  
  851. See I<sip> scheme.  Its syntax is the same as sip, but the default
  852. port is different.
  853.  
  854. =item B<snews>:
  855.  
  856. See I<news> scheme.  Its syntax is the same as news, but the default
  857. port is different.
  858.  
  859. =item B<telnet>:
  860.  
  861. An old specification of the I<telnet> URI scheme is found in RFC
  862. 1738. C<URI> objects belonging to the telnet scheme support the
  863. common, generic and server methods.
  864.  
  865. =item B<tn3270>:
  866.  
  867. These URIs are used like I<telnet> URIs but for connections to IBM
  868. mainframes.  C<URI> objects belonging to the tn3270 scheme support the
  869. common, generic and server methods.
  870.  
  871. =item B<ssh>:
  872.  
  873. Information about ssh is available at http://www.openssh.com/.
  874. C<URI> objects belonging to the ssh scheme support the common,
  875. generic and server methods. In addition, they provide methods to
  876. access the userinfo sub-components: $uri->user and $uri->password.
  877.  
  878. =item B<urn>:
  879.  
  880. The syntax of Uniform Resource Names is specified in RFC 2141.  C<URI>
  881. objects belonging to the urn scheme provide the common methods, and also the
  882. methods $uri->nid and $uri->nss, which return the Namespace Identifier
  883. and the Namespace-Specific String respectively.
  884.  
  885. The Namespace Identifier basically works like the Scheme identifier of
  886. URIs, and further divides the URN namespace.  Namespace Identifier
  887. assignments are maintained at
  888. <http://www.iana.org/assignments/urn-namespaces>.
  889.  
  890. Letter case is not significant for the Namespace Identifier.  It is
  891. always returned in lower case by the $uri->nid method.  The $uri->_nid
  892. method can be used if you want it in its original case.
  893.  
  894. =item B<urn>:B<isbn>:
  895.  
  896. The C<urn:isbn:> namespace contains International Standard Book
  897. Numbers (ISBNs) and is described in RFC 3187.  A C<URI> object belonging
  898. to this namespace has the following extra methods (if the
  899. Business::ISBN module is available): $uri->isbn,
  900. $uri->isbn_publisher_code, $uri->isbn_country_code, $uri->isbn_as_ean.
  901.  
  902. =item B<urn>:B<oid>:
  903.  
  904. The C<urn:oid:> namespace contains Object Identifiers (OIDs) and is
  905. described in RFC 3061.  An object identifier consists of sequences of digits
  906. separated by dots.  A C<URI> object belonging to this namespace has an
  907. additional method called $uri->oid that can be used to get/set the oid
  908. value.  In a list context, oid numbers are returned as separate elements.
  909.  
  910. =back
  911.  
  912. =head1 CONFIGURATION VARIABLES
  913.  
  914. The following configuration variables influence how the class and its
  915. methods behave:
  916.  
  917. =over 4
  918.  
  919. =item $URI::ABS_ALLOW_RELATIVE_SCHEME
  920.  
  921. Some older parsers used to allow the scheme name to be present in the
  922. relative URL if it was the same as the base URL scheme.  RFC 2396 says
  923. that this should be avoided, but you can enable this old behaviour by
  924. setting the $URI::ABS_ALLOW_RELATIVE_SCHEME variable to a TRUE value.
  925. The difference is demonstrated by the following examples:
  926.  
  927.   URI->new("http:foo")->abs("http://host/a/b")
  928.       ==>  "http:foo"
  929.  
  930.   local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
  931.   URI->new("http:foo")->abs("http://host/a/b")
  932.       ==>  "http:/host/a/foo"
  933.  
  934.  
  935. =item $URI::ABS_REMOTE_LEADING_DOTS
  936.  
  937. You can also have the abs() method ignore excess ".."
  938. segments in the relative URI by setting $URI::ABS_REMOTE_LEADING_DOTS
  939. to a TRUE value.  The difference is demonstrated by the following
  940. examples:
  941.  
  942.   URI->new("../../../foo")->abs("http://host/a/b")
  943.       ==> "http://host/../../foo"
  944.  
  945.   local $URI::ABS_REMOTE_LEADING_DOTS = 1;
  946.   URI->new("../../../foo")->abs("http://host/a/b")
  947.       ==> "http://host/foo"
  948.  
  949. =back
  950.  
  951. =head1 BUGS
  952.  
  953. Using regexp variables like $1 directly as arguments to the URI methods
  954. does not work too well with current perl implementations.  I would argue
  955. that this is actually a bug in perl.  The workaround is to quote
  956. them. Example:
  957.  
  958.    /(...)/ || die;
  959.    $u->query("$1");
  960.  
  961. =head1 PARSING URIs WITH REGEXP
  962.  
  963. As an alternative to this module, the following (official) regular
  964. expression can be used to decode a URI:
  965.  
  966.   my($scheme, $authority, $path, $query, $fragment) =
  967.   $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
  968.  
  969. The C<URI::Split> module provides the function uri_split() as a
  970. readable alternative.
  971.  
  972. =head1 SEE ALSO
  973.  
  974. L<URI::file>, L<URI::WithBase>, L<URI::QueryParam>, L<URI::Escape>,
  975. L<URI::Split>, L<URI::Heuristic>
  976.  
  977. RFC 2396: "Uniform Resource Identifiers (URI): Generic Syntax",
  978. Berners-Lee, Fielding, Masinter, August 1998.
  979.  
  980. http://www.iana.org/assignments/uri-schemes
  981.  
  982. http://www.iana.org/assignments/urn-namespaces
  983.  
  984. http://www.w3.org/Addressing/
  985.  
  986. =head1 COPYRIGHT
  987.  
  988. Copyright 1995-2003 Gisle Aas.
  989.  
  990. Copyright 1995 Martijn Koster.
  991.  
  992. This program is free software; you can redistribute it and/or modify
  993. it under the same terms as Perl itself.
  994.  
  995. =head1 AUTHORS / ACKNOWLEDGMENTS
  996.  
  997. This module is based on the C<URI::URL> module, which in turn was
  998. (distantly) based on the C<wwwurl.pl> code in the libwww-perl for
  999. perl4 developed by Roy Fielding, as part of the Arcadia project at the
  1000. University of California, Irvine, with contributions from Brooks
  1001. Cutter.
  1002.  
  1003. C<URI::URL> was developed by Gisle Aas, Tim Bunce, Roy Fielding and
  1004. Martijn Koster with input from other people on the libwww-perl mailing
  1005. list.
  1006.  
  1007. C<URI> and related subclasses was developed by Gisle Aas.
  1008.  
  1009. =cut
  1010.