<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">=encoding UTF8

=head1 NAME

URI::Fast - A fast(er) URI parser

=head1 SYNOPSIS

  use URI::Fast qw(uri);

  my $uri = uri 'http://www.example.com/some/path?fnord=slack&amp;foo=bar';

  if ($uri-&gt;scheme =~ /http(s)?/) {
    my @path  = $uri-&gt;path;
    my $fnord = $uri-&gt;param('fnord');
    my $foo   = $uri-&gt;param('foo');
  }

  if ($uri-&gt;path =~ /\/login/ &amp;&amp; $uri-&gt;scheme ne 'https') {
    $uri-&gt;scheme('https');
    $uri-&gt;param('upgraded', 1);
  }

=head1 DESCRIPTION

C&lt;URI::Fast&gt; is a faster alternative to L&lt;URI&gt;. It is written in C and provides
basic parsing and modification of a URI.

L&lt;URI&gt; is an excellent module; it is battle-tested, robust, and handles many
edge cases. As a result, it is rather slower than it would otherwise be for
more trivial cases, such as inspecting the path or updating a single query
parameter.

=head1 EXPORTED SUBROUTINES

Subroutines are exported on demand.

=head2 uri

Accepts a URI string, minimally parses it, and returns a C&lt;URI::Fast&gt; object.

Note: passing a C&lt;URI::Fast&gt; instance to this routine will cause the object to
be interpolated into a string (via L&lt;/to_string&gt;), effectively creating a clone
of the original C&lt;URI::Fast&gt; object.

=head2 iri

Similar to L&lt;/uri&gt;, but returns a C&lt;URI::Fast::IRI&gt; object. A C&lt;URI::Fast::IRI&gt;
differs from a C&lt;URI::Fast&gt; in that UTF-8 characters are permitted and will not
be percent-encoded when modified.

=head2 uri_split

Behaves (hopefully) identically to L&lt;URI::Split&gt;, but roughly twice as fast.

=head2 encode/decode/uri_encode/uri_decode

See L&lt;/ENCODING&gt;.

=head1 CONSTRUCTOR

If desired, both C&lt;URI::Fast&gt; and L&lt;URI::Fast::IRI&gt; may be instantiated using
the default constructor, C&lt;new&gt;.

  my $uri = URI::Fast-&gt;new('http://www.example.com');

=head1 ATTRIBUTES

All attributes serve as full accessors, allowing the URI segment to be both
retrieved and modified.

Each attribute defines a C&lt;raw_*&gt; method, which returns the raw, encoded string
value for that attribute.

Each attribute further has a matching clearer method (C&lt;clear_*&gt;) which unsets
its value.

In general, accessors accept an I&lt;unencoded&gt; string and set their slot value to
the I&lt;encoded&gt; value. They return the decoded value. See L&lt;/ENCODING&gt; for an in
depth description of their behavior as well as an explanation of the more
complex behavior of compound fields.

=head2 scheme

Gets or sets the scheme portion of the URI (e.g. C&lt;http&gt;), excluding C&lt;://&gt;.

=head2 auth

The authorization section is composed of the username, password, host name, and
port number:

  hostname.com
  someone@hostname.com
  someone:secret@hostname.com:1234

Setting this field may be done with a string (see the note below about
L&lt;/ENCODING&gt;) or a hash reference of individual field names (C&lt;usr&gt;, C&lt;pwd&gt;,
C&lt;host&gt;, and C&lt;port&gt;). In both cases, the existing values are completely
replaced by the new values and any values missing from the caller-supplied
input are deleted.

=head3 usr

The username segment of the authorization string. Updating this value alters
L&lt;/auth&gt;.

=head3 pwd

The password segment of the authorization string. Updating this value alters
L&lt;/auth&gt;.

=head3 host

The host name segment of the authorization string. May be a domain string or an
IP address. If the host is an IPV6 address, it must be surrounded by square
brackets (per spec), which are included in the host string. Updating this value
alters L&lt;/auth&gt;.

=head3 port

The port number segment of the authorization string. Updating this value alters
L&lt;/auth&gt;.

=head2 path

In scalar context, returns the entire path string. In list context, returns a
list of path segments, split by C&lt;/&gt;.

  my $uri = uri '/foo/bar';
  my $path = $uri-&gt;path;  # "/foo/bar"
  my @path = $uri-&gt;path;  # ("foo", "bar")

The path may also be updated using either a string or an array ref of segments:

  $uri-&gt;path('/foo/bar');
  $uri-&gt;path(['foo', 'bar']);

This differs from the behavior of L&lt;URI/path_segments&gt;, which considers the
leading slash separating the path from the authority section to be an
individual segment. If this behavior is desired, the lower level
C&lt;split_path_compat&gt; is available. C&lt;split_path_compat&gt; (and its partner,
C&lt;split_path&gt;), always return an array reference.

  my $uri = uri '/foo/bar';
  $uri-&gt;split_path;         # ['foo', 'bar'];
  $uri-&gt;split_path_compat;  # ['', 'foo', 'bar'];

=head2 query

In scalar context, returns the complete query string, excluding the leading
C&lt;?&gt;. The query string may be set in several ways.

  $uri-&gt;query("foo=bar&amp;baz=bat"); # note: no percent-encoding performed
  $uri-&gt;query({foo =&gt; 'bar', baz =&gt; 'bat'}); # foo=bar&amp;baz=bat
  $uri-&gt;query({foo =&gt; 'bar', baz =&gt; 'bat'}, ';'); # foo=bar;baz=bat

In list context, returns a hash ref mapping query keys to array refs of their
values (see L&lt;/query_hash&gt;).

Both '&amp;' and ';' are treated as separators for key/value parameters.

=head2 frag

The fragment section of the URI, excluding the leading C&lt;#&gt;.

=head1 METHODS

=head2 query_keys

Does a fast scan of the query string and returns a list of unique parameter
names that appear in the query string.

Both '&amp;' and ';' are treated as separators for key/value parameters.

=head2 query_hash

Scans the query string and returns a hash ref of key/value pairs. Values are
returned as an array ref, as keys may appear multiple times. Both '&amp;' and ';'
are treated as separators for key/value parameters.

May optionally be called with a new hash of parameters to replace the query
string with, in which case keys may map to scalar values or arrays of scalar
values. As with all query setter methods, a third parameter may be used to
explicitly specify the separator to use when generating the new query string.

=head2 param

Gets or sets a parameter value. Setting a parameter value will replace existing
values completely; the L&lt;/query&gt; string will also be updated. Setting a
parameter to C&lt;undef&gt; deletes the parameter from the URI.

  $uri-&gt;param('foo', ['bar', 'baz']);
  $uri-&gt;param('fnord', 'slack');

  my $value_scalar = $uri-&gt;param('fnord'); # fnord appears once
  my @value_list   = $uri-&gt;param('foo');   # foo appears twice
  my $value_scalar = $uri-&gt;param('foo');   # croaks; expected single value but foo has multiple

  # Delete parameter
  $uri-&gt;param('foo', undef); # deletes foo

  # Ambiguous cases
  $uri-&gt;param('foo', '');  # foo=
  $uri-&gt;param('foo', '0'); # foo=0
  $uri-&gt;param('foo', ' '); # foo=%20

Both '&amp;' and ';' are treated as separators for key/value parameters when
parsing the query string. An optional third parameter explicitly selects the
character used to separate key/value pairs.

  $uri-&gt;param('foo', 'bar', ';'); # foo=bar
  $uri-&gt;param('baz', 'bat', ';'); # foo=bar;baz=bat

When unspecified, '&amp;' is chosen as the default. I&lt;In either case, all
separators in the query string will be normalized to the chosen separator&gt;.

  $uri-&gt;param('foo', 'bar', ';'); # foo=bar
  $uri-&gt;param('baz', 'bat', ';'); # foo=bar;baz=bat
  $uri-&gt;param('fnord', 'slack');  # foo=bar&amp;baz=bat&amp;fnord=slack

=head2 add_param

Updates the query string by adding a new value for the specified key. If the
key already exists in the query string, the new value is appended without
altering the original value.

  $uri-&gt;add_param('foo', 'bar'); # foo=bar
  $uri-&gt;add_param('foo', 'baz'); # foo=bar&amp;foo=baz

This method is simply sugar for calling:

  $uri-&gt;param('key', [$uri-&gt;param('key'), 'new value']);

As with L&lt;/param&gt;, the separator character may be specified as the final
parameter. The same caveats apply with regard to normalization of the query
string separator.

  $uri-&gt;add_param('foo', 'bar', ';'); # foo=bar
  $uri-&gt;add_param('foo', 'baz', ';'); # foo=bar;foo=baz

=head2 query_keyset

Allows modification of the query string in the manner of a set, using keys
without C&lt;=value&gt;, e.g. C&lt;foo&amp;bar&amp;baz&gt;. Accepts a hash ref of keys to update.
A truthy value adds the key, a falsey value removes it. Any keys not mentioned
in the update hash are left unchanged.

  my $uri = uri '&amp;baz&amp;bat';
  $uri-&gt;query_keyset({foo =&gt; 1, bar =&gt; 1}); # baz&amp;bat&amp;foo&amp;bar
  $uri-&gt;query_keyset({baz =&gt; 0, bat =&gt; 0}); # foo&amp;bar

If there are key-value pairs in the query string as well, the behavior of
this method becomes a little more complex. When a key is specified in the
hash update hash ref, a positive value will leave an existing key/value pair
untouched. A negative value will remove the key and value.

  my $uri = uri '&amp;foo=bar&amp;baz&amp;bat';
  $uri-&gt;query_keyset({foo =&gt; 1, baz =&gt; 0}); # foo=bar&amp;bat

An optional second parameter may be specified to control the separator
character used when updating the query string. The same caveats apply with
regard to normalization of the query string separator.

=head2 append

Serially appends path segments, query strings, and fragments, to the end of the
URI. Each argument is added in order. If the segment begins with C&lt;?&gt;, it is
assumed to be a query string and it is appended using L&lt;/add_param&gt;. If the
segment begins with C&lt;#&gt;, it is treated as a fragment, replacing any existing
fragment. Otherwise, the segment is treated as a path fragment and appended to
the path.

  my $uri = uri 'http://www.example.com/foo?k=v';
  $uri-&gt;append('bar', 'baz/bat', '?k=v1&amp;k=v2', '#fnord', 'slack');
  # 'http://www.example.com/foo/bar/baz/bat/slack?k=v&amp;k=v1&amp;k=v2#fnord'


=head2 to_string

=head2 as_string

=head2 "$uri"

Stringifies the URI, encoding output as necessary. String interpolation is
overloaded.

=head2 compare

=head2 $uri eq $other

Compares the URI to another, returning true if the URIs are equivalent.
Overloads the C&lt;eq&gt; operator.

=head2 clone

Sugar for:

  my $uri = uri '...';
  my $clone = uri $uri;

=head2 absolute

Builds an absolute URI from a relative URI and a base URI string.
Adheres as strictly as possible to the rules for resolving a target URI in
L&lt;RFC3986 section 5.2|https://www.rfc-editor.org/rfc/rfc3986.txt&gt;. Returns a new
L&lt;URI::Fast&gt; object representing the absolute, merged URI.

  my $uri = uri('some/path')-&gt;absolute('http://www.example.com/fnord');
  $uri-&gt;to_string; # "http://www.example.com/fnord/some/path"

=head2 relative

Builds a relative URI using a second URI (either a C&lt;URI::Fast&gt; object or a
string) as a base. Unlike L&lt;URI/rel&gt;, ignores differences in domain and scheme
assumes the caller wishes to adopt the base URL's instead. Aside from that difference,
it's behavior should mimic L&lt;URI/rel&gt;'s.

  my $uri = uri('http://example.com/foo/bar')-&gt;relative('http://example.com/foo');
  $uri-&gt;to_string; # "foo/bar"

  my $uri = uri('http://example.com/foo/bar/')-&gt;relative('http://example.com/foo');
  $uri-&gt;to_string; # "foo/bar/"

=head2 normalize

Similar to L&lt;URI/canonical&gt;, performs a minimal normalization on the URI. Only
generic normalization described in the rfc is performed; no scheme-specific
normalization is done. Specifically, the scheme and host members are converted
to lower case, dot segments are collapsed in the path, and any percent-encoded
characters in the URI are converted to upper case.

=head1 ENCODING

C&lt;URI::Fast&gt; tries to do the right thing in most cases with regard to reserved
and non-ASCII characters. C&lt;URI::Fast&gt; will fully encode reserved and non-ASCII
characters when setting I&lt;individual&gt; values and return their fully decoded
values. However, the "right thing" is somewhat ambiguous when it comes to
setting compound fields like L&lt;/auth&gt;, L&lt;/path&gt;, and L&lt;/query&gt;.

When setting compound fields with a string value, reserved characters are
expected to be present, and are therefore accepted as-is. Any non-ASCII
characters will be percent-encoded (since they are unambiguous and there is no
risk of double-encoding them). Thus,

  $uri-&gt;auth('someone:secret@ل؟¬دŒخ´خ؟د‚.com:1234');
  print $uri-&gt;auth; # "someone:secret@%E1%BF%AC%CF%8C%CE%B4%CE%BF%CF%82.com:1234"

On the other hand, when setting these fields with a I&lt;reference&gt; value (assumed
to be a hash ref for L&lt;/auth&gt; and L&lt;/query&gt; or an array ref for L&lt;/path&gt;; see
individual methods' docs for details), each field is fully percent-encoded,
just as if each individual simple slot's setter had been called:

  $uri-&gt;auth({usr =&gt; 'some one', host =&gt; 'somewhere.com'});
  print $uri-&gt;auth; # "some%20one@somewhere.com"
  print $uri-&gt;usr;; # "some one"

The same goes for return values. For compound fields returning a string,
non-ASCII characters are decoded but reserved characters are not. When
returning a list or reference of the deconstructed field, individual values are
decoded of both reserved and non-ASCII characters.

=head2 '+' vs '%20'

Although no longer part of the standard, C&lt;+&gt; is commonly used as the encoded
space character (rather than C&lt;%20&gt;); it I&lt;is&gt; still official to the
C&lt;application/x-www-form-urlencoded&gt; type, and is treated as a space by
L&lt;/decode&gt;.

=head2 encode

Percent-encodes a string for use in a URI. By default, both reserved and UTF-8
chars (C&lt;! * ' ( ) ; : @ &amp; = + $ , / ? # [ ] %&gt;) are encoded.

A second (optional) parameter provides a string containing any characters the
caller does not wish to be encoded. An empty string will result in the default
behavior described above.

For example, to encode all characters in a query-like string I&lt;except&gt; for
those used by the query:

  my $encoded = URI::Fast::encode($some_string, '?&amp;=');

=head2 decode

Decodes a percent-encoded string.

  my $decoded = URI::Fast::decode($some_string);

=head2 uri_encode

=head2 uri_decode

These are aliases of L&lt;/encode&gt; and L&lt;/decode&gt;, respectively. They were added
to make L&lt;BLUEFEET|https://metacpan.org/author/BLUEFEET&gt; happy after he made
fun of me for naming L&lt;/encode&gt; and L&lt;/decode&gt; too generically.

In fact, these were originally aliased as C&lt;url_encode&gt; and C&lt;url_decode&gt;, but
due to some pedantic whining on the part of
L&lt;BGRIMM|https://metacpan.org/author/BGRIMM&gt;, they have been renamed to
C&lt;uri_encode&gt; and C&lt;uri_decode&gt;.

=head2 escape_tree

=head2 unescape_tree

Traverses a data structure, escaping or unescaping I&lt;defined&gt; scalar values in
place. Accepts a reference to be traversed. Any further parameters are passed
unchanged to L&lt;/encode&gt; or L&lt;/decode&gt;. Croaks if the input to escape/unescape
is a non-reference value.

  my $obj = {
    foo =&gt; ['bar baz', 'bat%fnord'],
    bar =&gt; {baz =&gt; 'bat%bat'},
    baz =&gt; undef,
    bat =&gt; '',
  };

  URI::Fast::escape_tree($obj);

  # $obj is now:
  {
    foo =&gt; ['bar%20baz', 'bat%25fnord'],
    bar =&gt; {baz =&gt; 'bat%25bat'},
    baz =&gt; undef,
    bat =&gt; '',
  }

  URI::Fast::unescape_tree($obj); # $obj returned to original form

  URI::Fast::escape_tree($obj, '%'); # escape but allow "%"

  # $obj is now:
  {
    foo =&gt; ['bar%20baz', 'bat%fnord'],
    bar =&gt; {baz =&gt; 'bat%bat'},
    baz =&gt; undef,
    bat =&gt; '',
  }

=head1 SPEED

See L&lt;URI::Fast::Benchmarks&gt;.

=head1 FUTURE PLANS

Some thoughts on future development for this module.

=over

=item Support for arbitrary binary data in query string

Currently, queries are restricted to keys or key/value pairs and decoded into
utf8 strings.

=item Compat module

Add a compatibility layer with URI that implements what functionality is
possible and croaks loudly otherwise.

=back

=head1 SEE ALSO

=over

=item L&lt;URI&gt;

The de facto standard.

=item L&lt;RFC 3986|https://www.rfc-editor.org/rfc/rfc3986.txt&gt;

The official standard.

=back

=head1 ACKNOWLEDGEMENTS

Thanks to L&lt;ZipRecruiter|https://www.ziprecruiter.com&gt; for encouraging their
employees to contribute back to the open source ecosystem. Without their
dedication to quality software development this distribution would not exist.

=head1 CONTRIBUTORS

The following people have contributed to this module with patches, bug reports,
API advice, identifying areas where the documentation is unclear, or by making
fun of me for naming certain methods too generically.

=over

=item Andy Ruder

=item Aran Deltac (BLUEFEET)

=item Ben Grimm (BGRIMM)

=item Dave Hubbard (DAVEH)

=item James Messrie

=item Martin Locklear

=item Randal Schwartz (MERLYN)

=item Sara Siegal (SSIEGAL)

=item Tim Vroom (VROOM)

=item Des Daignault (NAWGLAN)

=item Josh Rosenbaum

=back

=head1 AUTHOR

Jeff Ober &lt;sysread@fastmail.fm&gt;

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Jeff Ober. This is free software; you
can redistribute it and/or modify it under the same terms as the Perl 5
programming language system itself.

</pre></body></html>