man APR::URI () - Perl API for URI manipulations

NAME

APR::URI - Perl API for URI manipulations

Synopsis

  use APR::URI ();

  my $url = 'http://user:pass@example.com:80/foo?bar#item5';

  # parse and break the url into components
  my $parsed = APR::URI->parse($r->pool, $url);
  print $parsed->scheme;
  print $parsed->user;
  print $parsed->password;
  print $parsed->hostname;
  print $parsed->port;
  print $parsed->path;
  print $parsed->rpath;
  print $parsed->query;
  print $parsed->fragment;

  # reconstruct the url, after changing some components and completely
  # removing other
  $parsed->scheme($new_scheme);
  $parsed->user(undef);
  $parsed->password(undef);
  $parsed->hostname($new_hostname);
  $parsed->port($new_port);
  $parsed->path($new_path);
  $parsed->query(undef);
  $parsed->fragment(undef);
  print $parsed->unparse;

  # get the password field too (by default it's not revealed)
  use APR::Const -compile => qw(URI_UNP_REVEALPASSWORD);
  print $parsed->unparse(APR::Const::URI_UNP_REVEALPASSWORD);

  # what the default port for the ftp protocol?
  my $ftp_port = APR::URI::port_of_scheme("ftp");

Description

CWAPR::URI allows you to parse URI strings, manipulate each of the URI elements and deparse them back into URIs.

All CWAPR::URI object accessors accept a string or an CWundef value as an argument. Same goes for return value. It's important to distinguish between an empty string and CWundef. For example let's say your code was:

  my $uri = 'http://example.com/foo?bar#item5';
  my $parsed = APR::URI->parse($r->pool, $uri);

Now you no longer want to the query and fragment components in the final url. If you do:

  $parsed->fragment('');
  $parsed->query('');

followed by:

  my $new_uri = parsed->unparse;

the resulting URI will be:

  http://example.com/foo?#

which is probably not something that you've expected. In order to get rid of the separators, you must completely unset the fields you don't want to see. So, if you do:

  $parsed->fragment(undef);
  $parsed->query(undef);

followed by:

  my $new_uri = parsed->unparse;

the resulting URI will be:

   http://example.com/foo

As mentioned earlier the same goes for return values, so continuing this example:

  my $new_fragment = $parsed->fragment();
  my $new_query    = $parsed->query();

Both values now contain CWundef, therefore you must be careful when using the return values, when you use them, as you may get warnings.

Also make sure you read through CWthe CIunparse()CW section as various optional flags affect how the deparsed URI is rendered.

API

CWAPR::URI provides the following functions and/or methods: Get/set trailing #fragment string

  $oldval = $parsed->fragment($newval);
since: 2.0.00
Get/set combined CW[user[:password]@]host[:port]

  $oldval = $parsed->hostinfo($newval);
since: 2.0.00

The CWhostinfo value is set automatically when CWCIparse()CW is called.

It's not updated if any of the individual fields is modified.

It's not used when CWCIunparse()CW is called. Get/set hostname

  $oldval = $parsed->hostname($newval);
since: 2.0.00
Get/set password (as in http://user:password@host:port/)

  $oldval = $parsed->password($newval);
since: 2.0.00
Parse the URI string into URI components

  $parsed = APR::URI->parse($pool, $uri);
The URI to parse The parsed URI object
since: 2.0.00

After parsing, if a component existed but was an empty string (e.g. empty query http://hostname/path?) the corresponding accessor will return an empty string. If a component didn't exist (e.g. no query part http://hostname/path) the corresponding accessor will return CWundef. Get/set the request path

  $oldval = $parsed->path($newval);
CW"/" if only CWscheme://host
since: 2.0.00
Gets the CWpath minus the CWpath_info

  $rpath =  $parsed->rpath();
The path minus the path_info
since: 2.0.00
Get/set port number

  $oldval = $parsed->port($newval);
If the port component didn't appear in the parsed URI, APR internally calls CWCIport_of_scheme()CW to find out the port number for the given CWCIscheme()CW.
since: 2.0.00
Return the default port for a given scheme. The recognized schemes are http, ftp, https, gopher, wais, nntp, snews and prospero.

  $port = APR::URI::port_of_scheme($scheme);
The scheme string The default port for this scheme
since: 2.0.00
Get/set the query string (the part starting after CW'?' and all the way till the end or the CW'#fragment' part if the latter exists).

  $oldval = $parsed->query($newval);
since: 2.0.00
Get/set the protocol scheme (http, ftp, ...)

  $oldval = $parsed->scheme($newval);
since: 2.0.00
Get/set user name (as in http://user:password@host:port/)

  $oldval = $parsed->user($newval);
since: 2.0.00
Unparse the URI components back into a URI string

  $new_uri = $parsed->unparse();
  $new_uri = $parsed->unparse($flags);
By default the constant CWAPR::Const::URI_UNP_OMITPASSWORD is passed. If you need to pass more than one flag use unary CW|, e.g.:
  $flags = APR::Const::URI_UNP_OMITUSER|APR::Const::URI_UNP_OMITPASSWORD;
The valid CWflags constants are listed next
since: 2.0.00

Valid CWflags constants:

To import all URI constants you could do:

  use APR::Const -compile => qw(:uri);

but there is a significant amount of them, most irrelevant to this method. Therefore you probably don't want to do that. Instead specify explicitly the ones that you need. All the relevant to this method constants start with CWAPR::URI_UNP_.

And the available constants are: Don't show CWscheme, CWuser, CWpassword, CWhostname and CWport components (i.e. if you want only the relative URI) Hide the CWuser component Hide the CWpassword component (the default) Reveal the CWpassword component Don't show CWpath, CWquery and CWfragment components Don't show CWquery and CWfragment components

Notice that some flags overlap.

If the optional CW$flags argument is passed and contains no CWAPR::Const::URI_UNP_OMITPASSWORD and no CWAPR::Const::URI_UNP_REVEALPASSWORD the CWpassword part will be rendered as a literal CW"XXXXXXXX" string.

If the CWport number matches the CWCIport_of_scheme()CW, the unparsed URI won't include it and there is no flag to force that CWport to appear. If the CWport number is non-standard it will show up in the unparsed string.

Examples:

Starting with the parsed URL:

  use APR::URI ();
  my $url = 'http://user:pass@example.com:80/foo?bar#item5';
  my $parsed = APR::URI->parse($r->pool, $url);

deparse it back including and excluding parts, using different values for the optional CWflags argument:

•
Show all but the CWpassword fields:
  print $parsed->unparse;
Prints:
  http://user@example.com/foo?bar#item5
Notice that the CWport field is gone too, since it was a default CWport for CWscheme CWhttp://.
•
Include the CWpassword field (by default it's not revealed)
  use APR::Const -compile => qw(URI_UNP_REVEALPASSWORD);
  print $parsed->unparse(APR::Const::URI_UNP_REVEALPASSWORD);
Prints:
  http://user:pass@example.com/foo?bar#item5
•
Show all fields but the last three, CWpath, CWquery and CWfragment:
  use APR::Const -compile => qw(URI_UNP_REVEALPASSWORD
                                APR::Const::URI_UNP_OMITPATHINFO);
  print $parsed->unparse(
      APR::Const::URI_UNP_REVEALPASSWORD|URI_UNP_OMITPATHINFO);
Prints:
  http://user:pass@example.com

See Also

CWApache2::URI, mod_perl 2.0 documentation.

Copyright

mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.

Authors

The mod_perl development team and numerous contributors.