home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / URI / URL.pm < prev   
Text File  |  1997-11-18  |  29KB  |  1,008 lines

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