mod_perl logo perl icon
previous page: APR::ThreadRWLock - Perl API for APR thread read/write lockspage up: mod_perl 2.0 APInext page: APR::Util - Perl API for Various APR Utilities

APR::URI - Perl API for URI manipulations






Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
mod_perl2 User's Guide

mod_perl2 User's Guide

By Stas Bekman, Jim Brandt
Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet
The mod_perl Developer's Cookbook

The mod_perl Developer's Cookbook

By Geoffrey Young, Paul Lindner, Randy Kobes
mod_perl Pocket Reference

mod_perl Pocket Reference

By Andrew Ford
Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern


Table of Contents

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");


TOP

Description

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

All APR::URI object accessors accept a string or an undef value as an argument. Same goes for return value. It's important to distinguish between an empty string and undef. 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 undef, 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 the unparse() section as various optional flags affect how the deparsed URI is rendered.



TOP

API

APR::URI provides the following functions and/or methods:



TOP

fragment

Get/set trailing "#fragment" string

  $oldval = $parsed->fragment($newval);


TOP

hostinfo

Get/set combined [user[:password]@]host[:port]

  $oldval = $parsed->hostinfo($newval);

The hostinfo value is set automatically when parse() is called.

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

It's not used when unparse() is called.



TOP

hostname

Get/set hostname

  $oldval = $parsed->hostname($newval);


TOP

password

Get/set password (as in http://user:password@host:port/)

  $oldval = $parsed->password($newval);


TOP

parse

Parse the URI string into URI components

  $parsed = APR::URI->parse($pool, $uri);

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 undef.



TOP

path

Get/set the request path

  $oldval = $parsed->path($newval);


TOP

rpath

Gets the path minus the path_info

  $rpath =  $parsed->rpath();


TOP

port

Get/set port number

  $oldval = $parsed->port($newval);


TOP

port_of_scheme

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);


TOP

query

Get/set the query string (the part starting after '?' and all the way till the end or the '#fragment' part if the latter exists).

  $oldval = $parsed->query($newval);


TOP

scheme

Get/set the protocol scheme ("http", "ftp", ...)

  $oldval = $parsed->scheme($newval);


TOP

user

Get/set user name (as in http://user:password@host:port/)

  $oldval = $parsed->user($newval);


TOP

unparse

Unparse the URI components back into a URI string

  $new_uri = $parsed->unparse();
  $new_uri = $parsed->unparse($flags);

Valid flags 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 APR::URI_UNP_.

And the available constants are:

Notice that some flags overlap.

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

If the port number matches the port_of_scheme(), the unparsed URI won't include it and there is no flag to force that port to appear. If the port 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 flags argument:



TOP

See Also

Apache2::URI, mod_perl 2.0 documentation.



TOP

Copyright

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



TOP

Authors

The mod_perl development team and numerous contributors.






TOP
previous page: APR::ThreadRWLock - Perl API for APR thread read/write lockspage up: mod_perl 2.0 APInext page: APR::Util - Perl API for Various APR Utilities