home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / URI / URL.pm < prev   
Encoding:
Perl POD Document  |  1997-08-10  |  27.9 KB  |  1,004 lines

  1. package URI::URL;
  2.  
  3. $VERSION = "4.11";   # $Date: 1997/06/20 09:25:49 $
  4. sub Version { $VERSION; }
  5.  
  6. require 5.002;
  7.  
  8. require Exporter;
  9. @ISA = qw(Exporter);
  10. @EXPORT = qw(url);
  11.  
  12. require AutoLoader;
  13. *AUTOLOAD = \&AutoLoader::AUTOLOAD;
  14.  
  15. use Carp ();
  16.  
  17. # Basic lexical elements, taken from RFC 1738:
  18. #
  19. #  safe         = "$" | "-" | "_" | "." | "+"
  20. #  extra        = "!" | "*" | "'" | "(" | ")" | ","
  21. #  national     = "{" | "}" | "|" | "\" | "^" | "~" | "[" | "]" | "`"
  22. #  punctuation  = "<" | ">" | "#" | "%" | <">
  23. #  reserved     = ";" | "/" | "?" | ":" | "@" | "&" | "="
  24. #  escape       = "%" hex hex
  25. #  unreserved   = alpha | digit | safe | extra
  26. #  uchar        = unreserved | escape
  27. #  xchar        = unreserved | reserved | escape
  28.  
  29. # draft-fielding-url-syntax-05.txt adds '+' to the reserved chars and
  30. # takes '~' out
  31.  
  32. use strict;
  33. use vars qw($reserved $reserved_no_slash $reserved_no_form $unsafe
  34.         $COMPAT_VER_3
  35.         $Debug $Strict_URL
  36.        );
  37.  
  38. $reserved          = ";\\/?:\\@&=+#%"; # RFC 1738 reserved pluss '#' and '%'
  39. $reserved_no_slash = ";?:\\@&=+#%";    # used when escaping path
  40. $reserved_no_form  = ";\\/?:\\@#%";    # used when escaping params and query
  41.  
  42. # This is the unsafe characters (excluding those reserved)
  43. $unsafe   = "\x00-\x20{}|\\\\^\\[\\]`<>\"\x7F-\xFF";
  44. #$unsafe .= "~";  # according to RFC1738 but not to common practice
  45.  
  46. $Debug         = 0;     # set to 1 to print URLs on creation
  47. $COMPAT_VER_3  = 0;     # should we try to behave in the old way
  48. $Strict_URL    = 0;     # see new()
  49.  
  50. use overload ( '""' => 'as_string', 'fallback' => 1 );
  51.  
  52. my %Implementor = (); # mapping from scheme to implementation class
  53.  
  54.  
  55. # Easy to use constructor
  56. sub url ($;$)
  57. {
  58.     URI::URL->new(@_);
  59. }
  60.  
  61.  
  62. # URI::URL objects are implemented as blessed hashes:
  63. #
  64. # Each of the URL components (scheme, netloc, user, password, host,
  65. # port, path, params, query, fragment) are stored under their
  66. # name. The netloc, path, params and query is stored in quoted
  67. # (escaped) form.  The others is stored unquoted (unescaped).
  68. #
  69. # Netloc is special since it is rendundant (same as
  70. # "user:password@host:port") and must be kept in sync with those.
  71. #
  72. # The '_str' key stores a cached stringified version of the URL
  73. # (by definition in quoted form).
  74. # The '_base' key stores the optional base of a relative URL.
  75. #
  76. # The '_orig_url' is used while debugging is on.
  77. #
  78. # Subclasses may add their own keys but must take great care to
  79. # avoid names which might be used in later verions of this module.
  80.  
  81. sub new
  82. {
  83.     my($class, $init, $base) = @_;
  84.  
  85.     my $self;
  86.     if (ref $init) {
  87.     $self = $init->clone;
  88.     $self->base($base) if $base;
  89.     } else {
  90.     $init = "" unless defined $init;
  91.     # RFC 1738 appendix suggest that we just ignore extra whitespace
  92.     $init =~ s/\s+//g;
  93.     # Also get rid of any <URL: > wrapper
  94.     $init =~ s/^<(?:URL:)?(.*)>$/$1/;
  95.  
  96.     # We need a scheme to determine which class to use
  97.     my($scheme) = $init =~ m/^([.+\-\w]+):/;
  98.     if (!$scheme and $base){ # get scheme from base
  99.         if (ref $base){ # may be object or just a string
  100.         $scheme = $base->scheme;
  101.         } else {
  102.         $scheme = $1 if $base =~ m/^([.+\-\w]+):/;
  103.         }
  104.     }
  105.     unless($scheme){
  106.         Carp::croak("Unable to determine scheme for '$init'")
  107.         if $Strict_URL;
  108.         $scheme = 'http';
  109.     }
  110.     my $impclass = URI::URL::implementor($scheme);
  111.     unless ($impclass) {
  112.         Carp::croak("URI::URL scheme '$scheme' is not supported")
  113.         if $Strict_URL;
  114.         # use generic as fallback
  115.         require URI::URL::_generic;
  116.         URI::URL::implementor($scheme, 'URI::URL::_generic');
  117.         $impclass = 'URI::URL::_generic';
  118.     }
  119.  
  120.     # hand-off to scheme specific implementation sub-class
  121.     $self->{'_orig_url'} = $init if $Debug;
  122.     $self = $impclass->new($init, $base);
  123.     }
  124.     $self->print_on('STDERR') if $Debug;
  125.     return $self;
  126. }
  127.  
  128.  
  129. sub clone
  130. {
  131.     my $self = shift;
  132.     # this work as long as none of the components are references themselves
  133.     bless { %$self }, ref $self;
  134. }
  135.  
  136.  
  137. sub implementor
  138. {
  139.     my($scheme, $impclass) = @_;
  140.     unless (defined $scheme) {
  141.     require URI::URL::_generic;
  142.     return 'URI::URL::_generic';
  143.     }
  144.  
  145.     $scheme = lc($scheme);
  146.     if ($impclass) {
  147.     $impclass->_init_implementor($scheme);
  148.     my $old = $Implementor{$scheme};
  149.     $Implementor{$scheme} = $impclass;
  150.     return $old;
  151.     }
  152.  
  153.     my $ic = $Implementor{$scheme};
  154.     return $ic if $ic;
  155.  
  156.     # scheme not yet known, look for internal or
  157.     # preloaded (with 'use') implementation
  158.     $ic = "URI::URL::$scheme";  # default location
  159.     no strict 'refs';
  160.     # check we actually have one for the scheme:
  161.     unless (defined @{"${ic}::ISA"}) {
  162.     # Try to load it
  163.     eval { require "URI/URL/$scheme.pm"; };
  164.     die $@ if $@ && $@ !~ /Can\'t locate/;
  165.     $ic = '' unless defined @{"${ic}::ISA"};
  166.     }
  167.     if ($ic) {
  168.     $ic->_init_implementor($scheme);
  169.     $Implementor{$scheme} = $ic;
  170.     }
  171.     $ic;
  172. }
  173.  
  174.  
  175. sub _init_implementor
  176. {
  177.     my($class, $scheme) = @_;
  178.     # Remember that one implementor class may actually
  179.     # serve to implement several URL schemes.
  180.  
  181.     if ($] < 5.003_17) {
  182.     no strict qw(refs);
  183.     # Setup overloading inheritace - experimental
  184.     %{"${class}::OVERLOAD"} = %URI::URL::OVERLOAD
  185.         unless defined %{"${class}::OVERLOAD"};
  186.     }
  187. }
  188.  
  189.  
  190. # This private method help us implement access to the elements in the
  191. # URI::URL object hash (%$self).  You can set up access to an element
  192. # with a routine similar to this one:
  193. #
  194. #  sub component { shift->_elem('component', @_); }
  195.  
  196. sub _elem
  197. {
  198.     my($self, $element, @val) = @_;
  199.     my $old = $self->{$element};
  200.     return $old unless @val;
  201.     $self->{$element} = $val[0]; # general case
  202.     $self->{'_str'} = '';        # void cached string
  203.     $old;
  204. }
  205.  
  206.  
  207. # Make all standard methods available to all kinds of URLs.  This allow
  208. # us to call these without to much worry when URI::URL::strict(0);
  209.  
  210. sub bad_method;
  211.  
  212. *netloc       = \&bad_method;
  213. *user         = \&bad_method;
  214. *password     = \&bad_method;
  215. *host         = \&bad_method;
  216. *port         = \&bad_method;
  217. *default_port = \&bad_method;
  218.  
  219. *full_path    = \&bad_method;
  220. *epath        = \&bad_method;
  221. *eparams      = \&bad_method;
  222. *equery       = \&bad_method;
  223.  
  224. *path         = \&bad_method;
  225. *path_components = \&bad_method;
  226. *params       = \&bad_method;
  227. *query        = \&bad_method;
  228. *frag         = \&bad_method;
  229.  
  230.  
  231. #
  232. # A U T O  L O A D I N G
  233. #
  234. # The rest of the methods are autoloaded because they should be less
  235. # frequently used.  We define stubs here so that inheritance works as
  236. # it should.
  237. sub newlocal;
  238. sub strict;
  239. sub base;
  240. sub scheme;
  241. sub crack;
  242. sub abs;
  243. sub rel;
  244. sub as_string;
  245. sub eq;
  246. sub print_on;
  247. sub unsafe;
  248. sub escape;
  249. sub unescape;
  250.  
  251. # Don't need DESTROY but avoid trying to AUTOLOAD it.
  252. sub DESTROY { }
  253.  
  254. 1;
  255. __END__
  256.  
  257. sub newlocal
  258. {
  259.     require URI::URL::file;
  260.     my($class, $path) = @_;
  261.     newlocal URI::URL::file $path;  # pass it on the the file class
  262. }
  263.  
  264. sub strict
  265. {
  266.     return $Strict_URL unless @_;
  267.     my $old = $Strict_URL;
  268.     $Strict_URL = $_[0];
  269.     $old;
  270. }
  271.  
  272. # Access some attributes of a URL object:
  273. sub base {
  274.     my $self = shift;
  275.     return $self->_elem('_base', @_) if @_;      # set
  276.  
  277.     # The base attribute supports 'lazy' conversion from URL strings
  278.     # to URL objects. Strings may be stored but when a string is
  279.     # fetched it will automatically be converted to a URL object.
  280.     # The main benefit is to make it much cheaper to say:
  281.     #   new URI::URL $random_url_string, 'http:'
  282.     my $base = $self->_elem('_base');            # get
  283.     return undef unless defined $base;
  284.     unless (ref $base){
  285.     $base = new URI::URL $base;
  286.     $self->_elem('_base', $base); # set new object
  287.     }
  288.     $base;
  289. }
  290.  
  291. sub scheme {
  292.     my $self = shift;
  293.     my $old = $self->{'scheme'};
  294.     return $old unless @_;
  295.  
  296.     my $newscheme = shift;
  297.     if (defined($newscheme) && length($newscheme)) {
  298.     # reparse URL with new scheme
  299.     my $str = $self->as_string;
  300.     $str =~ s/^[\w+\-.]+://;
  301.     my $newself = new URI::URL "$newscheme:$str";
  302.     %$self = %$newself;
  303.     bless $self, ref($newself);
  304.     } else {
  305.     $self->{'scheme'} = undef;
  306.     }
  307.     $old;
  308. }
  309.  
  310. sub crack
  311. {
  312.     # should be overridden by subclasses
  313.     my $self = shift;
  314.     ($self->scheme,  # 0: scheme
  315.      undef,          # 1: user
  316.      undef,          # 2: passwd
  317.      undef,          # 3: host
  318.      undef,          # 4: port
  319.      undef,          # 5: path
  320.      undef,          # 6: params
  321.      undef,          # 7: query
  322.      undef           # 8: fragment
  323.     )
  324. }
  325.  
  326. # These are overridden by _generic (this is just a noop for those schemes that
  327. # do not wish to be a subclass of URI::URL::_generic)
  328. sub abs { shift->clone; }
  329. sub rel { shift->clone; }
  330.  
  331. # This method should always be overridden in subclasses
  332. sub as_string {
  333.     "";
  334. }
  335.  
  336. # Compare two URLs, subclasses will provide a more correct implementation
  337. sub eq {
  338.     my($self, $other) = @_;
  339.     $other = URI::URL->new($other, $self) unless ref $other;
  340.     ref($self) eq ref($other) &&
  341.       $self->scheme eq $other->scheme &&
  342.       $self->as_string eq $other->as_string;  # Case-sensitive
  343. }
  344.  
  345. # This is set up as an alias for various methods
  346. sub bad_method {
  347.     my $self = shift;
  348.     my $scheme = $self->scheme;
  349.     Carp::croak("Illegal method called for $scheme: URL")
  350.     if $Strict_URL;
  351.     # Carp::carp("Illegal method called for $scheme: URL")
  352.     #     if $^W;
  353.     undef;
  354. }
  355.  
  356. sub print_on
  357. {
  358.     no strict qw(refs);  # because we use strings as filehandles
  359.     my $self = shift;
  360.     my $fh = shift || 'STDOUT';
  361.     my($k, $v);
  362.     print $fh "Dump of URL $self...\n";
  363.     foreach $k (sort keys %$self){
  364.     $v = $self->{$k};
  365.     $v = 'UNDEF' unless defined $v;
  366.     print $fh "  $k\t'$v'\n";
  367.     }
  368. }
  369.  
  370. # These are just supported for some (bad) kind of backwards portability.
  371.  
  372. sub unsafe {
  373.     Carp::croak("The unsafe() method not supported by URI::URL any more!
  374. If you need this feature badly, then you should make a subclass of
  375. the URL-schemes you need to modify the behavior for.  The method
  376. was called");
  377. }
  378.  
  379. sub escape {
  380.     Carp::croak("The escape() method not supported by URI::URL any more!
  381. Use the URI::Escape module instead.  The method was called");
  382. }
  383.  
  384. sub unescape {
  385.     Carp::croak("unescape() method not supported by URI::URL any more!
  386. Use the URI::Escape module instead.  The method was called");
  387. }
  388.  
  389. 1;
  390.  
  391.  
  392. #########################################################################
  393. #### D O C U M E N T A T I O N
  394. #########################################################################
  395.  
  396. =head1 NAME
  397.  
  398. URI::URL - Uniform Resource Locators (absolute and relative)
  399.  
  400. =head1 SYNOPSIS
  401.  
  402.  use URI::URL;
  403.  
  404.  # Constructors
  405.  $url1 = new URI::URL 'http://www.perl.com/%7Euser/gisle.gif';
  406.  $url2 = new URI::URL 'gisle.gif', 'http://www.com/%7Euser';
  407.  $url3 = url 'http://www.sn.no/'; # handy constructor
  408.  $url4 = $url2->abs;       # get absolute url using base
  409.  $url5 = $url2->abs('http:/other/path');
  410.  $url6 = newlocal URI::URL 'test';
  411.  
  412.  # Stringify URL
  413.  $str1 = $url->as_string;  # complete escaped URL string
  414.  $str2 = $url->full_path;  # escaped path+params+query
  415.  $str3 = "$url";           # use operator overloading
  416.  
  417.  # Retrieving Generic-RL components:
  418.  $scheme   = $url->scheme;
  419.  $netloc   = $url->netloc; # see user,password,host,port below
  420.  $path     = $url->path;
  421.  $params   = $url->params;
  422.  $query    = $url->query;
  423.  $frag     = $url->frag;
  424.  
  425.  # Accessing elements in their escaped form
  426.  $path     = $url->epath;
  427.  $params   = $url->eparams;
  428.  $query    = $url->equery;
  429.  
  430.  # Retrieving Network location (netloc) components:
  431.  $user     = $url->user;
  432.  $password = $url->password;
  433.  $host     = $url->host;
  434.  $port     = $url->port;   # returns default if not defined
  435.  
  436.  # Retrieve escaped path components as an array
  437.  @path     = $url->path_components;
  438.  
  439.  # HTTP query-string access methods
  440.  @keywords = $url->keywords;
  441.  @form     = $url->query_form;
  442.  
  443.  # All methods above can set the field values, e.g:
  444.  $url->scheme('http');
  445.  $url->host('www.w3.org');
  446.  $url->port($url->default_port);
  447.  $url->base($url5);                      # use string or object
  448.  $url->keywords(qw(dog bones));
  449.  
  450.  # File methods
  451.  $url = new URI::URL "file:/foo/bar";
  452.  open(F, $url->local_path) or die;
  453.  
  454.  # Compare URLs
  455.  if ($url->eq("http://www.sn.no")) or die;
  456.  
  457. =head1 DESCRIPTION
  458.  
  459. This module implements the URI::URL class representing Uniform
  460. Resource Locators (URL). URLs provide a compact string representation
  461. for resources available via the Internet. Both absolute (RFC 1738) and
  462. relative (RFC 1808) URLs are supported.
  463.  
  464. URI::URL objects are created by calling new(), which takes as argument
  465. a string representation of the URL or an existing URL object reference
  466. to be cloned. Specific individual elements can then be accessed via
  467. the scheme(), user(), password(), host(), port(), path(), params(),
  468. query() and frag() methods.  In addition escaped versions of the path,
  469. params and query can be accessed with the epath(), eparams() and
  470. equery() methods.  Note that some URL schemes will support all these
  471. methods.
  472.  
  473. The object constructor new() must be able to determine the scheme for
  474. the URL.  If a scheme is not specified in the URL itself, it will use
  475. the scheme specified by the base URL. If no base URL scheme is defined
  476. then new() will croak if URI::URL::strict(1) has been invoked,
  477. otherwise I<http> is silently assumed.  Once the scheme has been
  478. determined new() then uses the implementor() function to determine
  479. which class implements that scheme.  If no implementor class is
  480. defined for the scheme then new() will croak if URI::URL::strict(1)
  481. has been invoked, otherwise the internal generic URL class is assumed.
  482.  
  483. Internally defined schemes are implemented by the
  484. URI::URL::I<scheme_name> module.  The URI::URL::implementor() function
  485. can be used to explicitly set the class used to implement a scheme if
  486. you want to override this.
  487.  
  488.  
  489. =head1 HOW AND WHEN TO ESCAPE
  490.  
  491.  
  492. =over 3
  493.  
  494. =item This is an edited extract from a URI specification:
  495.  
  496. The printability requirement has been met by specifying a safe set of
  497. characters, and a general escaping scheme for encoding "unsafe"
  498. characters. This "safe" set is suitable, for example, for use in
  499. electronic mail.  This is the canonical form of a URI.
  500.  
  501. There is a conflict between the need to be able to represent many
  502. characters including spaces within a URI directly, and the need to be
  503. able to use a URI in environments which have limited character sets
  504. or in which certain characters are prone to corruption. This conflict
  505. has been resolved by use of an hexadecimal escaping method which may
  506. be applied to any characters forbidden in a given context. When URLs
  507. are moved between contexts, the set of characters escaped may be
  508. enlarged or reduced unambiguously.  The canonical form for URIs has
  509. all white spaces encoded.
  510.  
  511. =item Notes:
  512.  
  513. A URL string I<must>, by definition, consist of escaped
  514. components. Complete URLs are always escaped.
  515.  
  516. The components of a URL string must be I<individually> escaped.  Each
  517. component of a URL may have a separate requirements regarding what
  518. must be escaped, and those requirements are also dependent on the URL
  519. scheme.
  520.  
  521. Never escape an already escaped component string.
  522.  
  523. =back
  524.  
  525. This implementation expects an escaped URL string to be passed to
  526. new() and will return a fully escaped URL string from as_string()
  527. and full_path().
  528.  
  529. Individual components can be manipulated in unescaped or escaped
  530. form. The following methods return/accept unescaped strings:
  531.  
  532.     scheme                  path
  533.     user                    params
  534.     password                query
  535.     host                    frag
  536.     port
  537.  
  538. The following methods return/accept partial I<escaped> strings:
  539.  
  540.     netloc                  eparams
  541.     epath                   equery
  542.  
  543. I<Partial escaped> means that only reserved characters
  544. (i.e. ':', '@', '/', ';', '?', '=', '&' in addition to '%', '.' and '#')
  545. needs to be escaped when they are to be treated as normal characters.
  546. I<Fully escaped> means that all unsafe characters are escaped. Unsafe
  547. characters are all all control characters (%00-%1F and %7F), all 8-bit
  548. characters (%80-%FF) as well
  549. as '{', '}', '|', '\', '^', '[', ']' '`', '"', '<' and '>'.
  550. Note that the character '~' is B<not> considered
  551. unsafe by this library as it is common practice to use it to reference
  552. personal home pages, but it is still unsafe according to RFC 1738.
  553.  
  554. =head1 ADDING NEW URL SCHEMES
  555.  
  556. New URL schemes or alternative implementations for existing schemes
  557. can be added to your own code. To create a new scheme class use code
  558. like:
  559.  
  560.    package MYURL::foo;
  561.    @ISA = (URI::URL::implementor());   # inherit from generic scheme
  562.  
  563. The 'URI::URL::implementor()' function call with no parameters returns
  564. the name of the class which implements the generic URL scheme
  565. behaviour (typically C<URI::URL::_generic>). All hierarchical schemes
  566. should be derived from this class.
  567.  
  568. Your class can then define overriding methods (e.g., new(), _parse()
  569. as required).
  570.  
  571. To register your new class as the implementor for a specific scheme
  572. use code like:
  573.  
  574.    URI::URL::implementor('x-foo', 'MYURL::foo');
  575.  
  576. Any new URL created for scheme 'x-foo' will be implemented by your
  577. C<MYURL::foo> class. Existing URLs will not be affected.
  578.  
  579.  
  580. =head1 FUNCTIONS
  581.  
  582. =over 3
  583.  
  584. =item new URI::URL $url_string [, $base_url]
  585.  
  586. This is the object constructor.  It will create a new URI::URL object,
  587. initialized from the URL string.  To trap bad or unknown URL schemes
  588. use:
  589.  
  590.  $obj = eval { new URI::URL "snews:comp.lang.perl.misc" };
  591.  
  592. or set URI::URL::strict(0) if you do not care about bad or unknown
  593. schemes.
  594.  
  595. =item newlocal URI::URL $path;
  596.  
  597. Returns an URL object that denotes a path within the local filesystem.
  598. Paths not starting with '/' are interpreted relative to the current
  599. working directory.  This constructor always return an absolute 'file'
  600. URL.
  601.  
  602. =item url($url_string, [, $base_url])
  603.  
  604. Alternative constructor function.  The url() function is exported by
  605. the URI::URL module and is easier both to type and read than calling
  606. URI::URL->new directly.  Useful for constructs like this:
  607.  
  608.    $h = url($str)->host;
  609.  
  610. This function is just a wrapper for URI::URL->new.
  611.  
  612. =item URI::URL::strict($bool)
  613.  
  614. If strict is true then we croak on errors.  The function returns the
  615. previous value.
  616.  
  617. =item URI::URL::implementor([$scheme, [$class]])
  618.  
  619. Use this function to get or set implementor class for a scheme.
  620. Returns '' if specified scheme is not supported.  Returns generic URL
  621. class if no scheme specified.
  622.  
  623. =back
  624.  
  625. =head1 METHODS
  626.  
  627. This section describes the methods available for an URI::URL object.
  628. Note that some URL schemes will disallow some of these methods and
  629. will croak if they are used.  Some URL schemes add additional methods
  630. that are described in the sections to follow.
  631.  
  632. Attribute access methods marked with (*) can take an optional argument
  633. to set the value of the attribute, and they always return the old
  634. value.
  635.  
  636. =over 3
  637.  
  638. =item $url->abs([$base, [$allow_scheme_in_relative_urls]])
  639.  
  640. The abs() method attempts to return a new absolute URI::URL object
  641. for a given URL.  In order to convert a relative URL into an absolute
  642. one, a I<base> URL is required. You can associate a default base with a
  643. URL either by passing a I<base> to the new() constructor when a
  644. URI::URL is created or using the base() method on the object later.
  645. Alternatively you can specify a one-off base as a parameter to the
  646. abs() method.
  647.  
  648. Some older parsers used to allow the scheme name to be present in the
  649. relative URL if it was the same as the base URL scheme.  RFC1808 says
  650. that this should be avoided, but you can enable this old behaviour by
  651. passing a TRUE value as the second argument to the abs() method.  The
  652. difference is demonstrated by the following examples:
  653.  
  654.   url("http:foo")->abs("http://host/a/b")     ==>  "http:foo"
  655.   url("http:foo")->abs("http://host/a/b", 1)  ==>  "http:/host/a/foo"
  656.  
  657. The rel() method will do the opposite transformation.
  658.  
  659. =item $url->as_string
  660.  
  661. Returns a string representing the URL in its canonical form.  All
  662. unsafe characters will be escaped.  This method is overloaded as the
  663. perl "stringify" operator, which means that URLs can be used as
  664. strings in many contexts.
  665.  
  666. =item $url->base (*)
  667.  
  668. Get/set the base URL associated with the current URI::URL object.  The
  669. base URL matters when you call the abs() method.
  670.  
  671. =item $url->clone
  672.  
  673. Returns a copy of the current URI::URL object.
  674.  
  675. =item $url->crack
  676.  
  677. Return a 9 element array with the following content:
  678.  
  679.    0: $url->scheme *)
  680.    1: $url->user
  681.    2: $url->password
  682.    3: $url->host
  683.    4: $url->port
  684.    5: $url->epath
  685.    6: $url->eparams
  686.    7: $url->equery
  687.    8: $url->frag
  688.  
  689. All elements except I<scheme> will be undefined if the corresponding
  690. URL part is not available.
  691.  
  692. B<Note:> The scheme (first element) returned by crack will aways be
  693. defined.  This is different from what the $url->scheme returns, since
  694. it will return I<undef> for relative URLs.
  695.  
  696. =item $url->default_port
  697.  
  698. Returns the default port number for the URL scheme that the URI::URL
  699. belongs too.
  700.  
  701. =item $url->eparams (*)
  702.  
  703. Get/set the URL parameters in escaped form.
  704.  
  705. =item $url->epath (*)
  706.  
  707. Get/set the URL path in escaped form.
  708.  
  709. =item $url->eq($other_url)
  710.  
  711. Compare two URLs to decide if they match or not.  The rules for how
  712. comparison is made varies for different parts of the URLs; scheme and
  713. netloc comparison is case-insensitive, and escaped chars match their
  714. %XX encoding unless they are "reserved" or "unsafe".
  715.  
  716. =item $url->equery (*)
  717.  
  718. Get/set the URL query string in escaped form.
  719.  
  720. =item $url->full_path
  721.  
  722. Returns the string "/path;params?query".  This is the string that is
  723. passed to a remote server in order to access the document.
  724.  
  725. =item $url->frag (*)
  726.  
  727. Get/set the fragment (unescaped)
  728.  
  729. =item $url->host (*)
  730.  
  731. Get/set the host (unescaped)
  732.  
  733. =item $url->netloc (*)
  734.  
  735. Get/set the network location in escaped form.  Setting the network
  736. location will affect 'user', 'password', 'host' and 'port'.
  737.  
  738. =item $url->params (*)
  739.  
  740. Get/set the URL parameters (unescaped)
  741.  
  742. =item $url->password (*)
  743.  
  744. Get/set the password (unescaped)
  745.  
  746. =item $url->path (*)
  747.  
  748. Get/set the path (unescaped).  This method will croak if any of the
  749. path components in the return value contain the "/" character.  You
  750. should use the epath() method to be safe.
  751.  
  752. =item $url->path_components (*)
  753.  
  754. Get/set the path using a list of unescaped path components.  The
  755. return value will loose the distinction beween '.' and '%2E'.  When
  756. setting a value, a '.' is converted to be a literal '.' and is
  757. therefore encoded as '%2E'.
  758.  
  759. =item $url->port (*)
  760.  
  761. Get/set the network port (unescaped)
  762.  
  763. =item $url->rel([$base])
  764.  
  765. Return a relative URL if possible.  This is the opposite of what the
  766. abs() method does.  For instance:
  767.  
  768.    url("http://www.math.uio.no/doc/mail/top.html",
  769.        "http://www.math.uio.no/doc/linux/")->rel
  770.  
  771. will return a relative URL with path set to "../mail/top.html" and
  772. with the same base as the original URL.
  773.  
  774. If the original URL already is relative or the scheme or netloc does
  775. not match the base, then a copy of the original URL is returned.
  776.  
  777.  
  778. =item $url->print_on(*FILEHANDLE);
  779.  
  780. Prints a verbose presentation of the contents of the URL object to
  781. the specified file handle (default STDOUT).  Mainly useful for
  782. debugging.
  783.  
  784. =item $url->scheme (*)
  785.  
  786. Get/set the scheme for the URL.
  787.  
  788. =item $url->query (*)
  789.  
  790. Get/set the query string (unescaped).  This method will croak if the
  791. string returned contains both '+' and '%2B' or '=' together with '%3D'
  792. or '%26'.  You should use the equery() method to be safe.
  793.  
  794. =item $url->user (*)
  795.  
  796. Get/set the URL user name (unescaped)
  797.  
  798. =back
  799.  
  800. =head1 HTTP METHODS
  801.  
  802. For I<http> URLs you may also access the query string using the
  803. keywords() and the query_form() methods.  Both will croak if the query
  804. is not of the correct format.  The encodings look like this:
  805.  
  806.   word1+word2+word3..        # keywords
  807.   key1=val1&key2=val2...     # query_form
  808.  
  809. Note: These functions does not return the old value when they are used
  810. to set a value of the query string.
  811.  
  812. =over 3
  813.  
  814. =item $url->keywords (*)
  815.  
  816. The keywords() method returns a list of unescaped strings.  The method
  817. can also be used to set the query string by passing in the keywords as
  818. individual arguments to the method.
  819.  
  820. =item $url->query_form (*)
  821.  
  822. The query_form() method return a list of unescaped key/value pairs.
  823. If you assign the return value to a hash you might loose some values
  824. if the key is repeated (which it is allowed to do).
  825.  
  826. This method can also be used to set the query sting of the URL like this:
  827.  
  828.   $url->query_form(foo => 'bar', foo => 'baz', equal => '=');
  829.  
  830. If the value part of a key/value pair is a reference to an array, then
  831. it will be converted to separate key/value pairs for each value.  This
  832. means that these two calls are equal:
  833.  
  834.   $url->query_form(foo => 'bar', foo => 'baz');
  835.   $url->query_form(foo => ['bar', 'baz']);
  836.  
  837. =back
  838.  
  839. =head1 FILE METHODS
  840.  
  841. The I<file> URLs implement the local_path() method that returns a path
  842. suitable for access to files within the current filesystem.  These
  843. methods can B<not> be used to set the path of the URL.
  844.  
  845. =over 3
  846.  
  847. =item $url->local_path
  848.  
  849. This method is really just an alias for one of the methods below
  850. depending on what system you run on.
  851.  
  852. =item $url->unix_path
  853.  
  854. Returns a path suitable for use on a Unix system.  This method will
  855. croak if any of the path segments contains a "/" or a NULL character.
  856.  
  857. =item $url->dos_path
  858.  
  859. Returns a path suitable for use on a MS-DOS or MS-Windows system.
  860.  
  861. =item $url->mac_path
  862.  
  863. Returns a path suitable for use on a Macintosh system.
  864.  
  865. =item $url->vms_path
  866.  
  867. Returns a path suitable for use on a VMS system.  VMS is a trademark
  868. of Digital.
  869.  
  870. =back
  871.  
  872. =head1 GOPHER METHODS
  873.  
  874. The methods access the parts that are specific for the gopher URLs.
  875. These methods access different parts of the $url->path.
  876.  
  877. =over 3
  878.  
  879. =item $url->gtype (*)
  880.  
  881. =item $url->selector (*)
  882.  
  883. =item $url->search (*)
  884.  
  885. =item $url->string (*)
  886.  
  887. =back
  888.  
  889. =head1 NEWS METHODS
  890.  
  891. =over 3
  892.  
  893. =item $url->group (*)
  894.  
  895. =item $url->article (*)
  896.  
  897. =back
  898.  
  899. =head1 WAIS METHODS
  900.  
  901. The methods access the parts that are specific for the wais URLs.
  902. These methods access different parts of the $url->path.
  903.  
  904. =over 3
  905.  
  906. =item $url->database (*)
  907.  
  908. =item $url->wtype (*)
  909.  
  910. =item $url->wpath (*)
  911.  
  912. =back
  913.  
  914. =head1 MAILTO METHODS
  915.  
  916. =over 3
  917.  
  918. =item $url->address (*)
  919.  
  920. The mail address can also be accessed with the netloc() method.
  921.  
  922. =back
  923.  
  924.  
  925. =head1 WHAT A URL IS NOT
  926.  
  927. URL objects do not, and should not, know how to 'get' or 'put' the
  928. resources they specify locations for, anymore than a postal address
  929. 'knows' anything about the postal system. The actual access/transfer
  930. should be achieved by some form of transport agent class (see
  931. L<LWP::UserAgent>). The agent class can use the URL class, but should
  932. not be a subclass of it.
  933.  
  934. =head1 COMPATIBILITY
  935.  
  936. This is a listing incompatibilities with URI::URL version 3.x:
  937.  
  938. =over 3
  939.  
  940. =item unsafe(), escape() and unescape()
  941.  
  942. These methods not supported any more.
  943.  
  944. =item full_path() and as_string()
  945.  
  946. These methods does no longer take a second argument which specify the
  947. set of characters to consider as unsafe.
  948.  
  949. =item '+' in the query-string
  950.  
  951. The '+' character in the query part of the URL was earlier considered
  952. to be an encoding of a space. This was just bad influence from Mosaic.
  953. Space is now encoded as '%20'.
  954.  
  955. =item path() and query()
  956.  
  957. This methods will croak if they loose information.  Use epath() or
  958. equery() instead.  The path() method will for instance loose
  959. information if any path segment contain an (encoded) '/' character.
  960.  
  961. The path() now consider a leading '/' to be part of the path.  If the
  962. path is empty it will default to '/'.  You can get the old behaviour
  963. by setting $URI::URL::COMPAT_VER_3 to TRUE before accessing the path()
  964. method.
  965.  
  966. =item netloc()
  967.  
  968. The string passed to netloc is now assumed to be escaped.  The string
  969. returned will also be (partially) escaped.
  970.  
  971. =item sub-classing
  972.  
  973. The path, params and query is now stored internally in unescaped form.
  974. This might affect sub-classes of the URL scheme classes.
  975.  
  976. =back
  977.  
  978. =head1 AUTHORS / ACKNOWLEDGMENTS
  979.  
  980. This module is (distantly) based on the C<wwwurl.pl> code in the
  981. libwww-perl distribution developed by Roy Fielding
  982. <fielding@ics.uci.edu>, as part of the Arcadia project at the
  983. University of California, Irvine, with contributions from Brooks
  984. Cutter.
  985.  
  986. Gisle Aas <aas@sn.no>, Tim Bunce <Tim.Bunce@ig.co.uk>, Roy Fielding
  987. <fielding@ics.uci.edu> and Martijn Koster <m.koster@webcrawler.com>
  988. (in English alphabetical order) have collaborated on the complete
  989. rewrite for Perl 5, with input from other people on the libwww-perl
  990. mailing list.
  991.  
  992. If you have any suggestions, bug reports, fixes, or enhancements, send
  993. them to the libwww-perl mailing list at <libwww-perl@ics.uci.edu>.
  994.  
  995. =head1 COPYRIGHT
  996.  
  997. Copyright 1995-1996 Gisle Aas.
  998. Copyright 1995 Martijn Koster.
  999.  
  1000. This program is free software; you can redistribute it and/or modify
  1001. it under the same terms as Perl itself.
  1002.  
  1003. =cut
  1004.