| 1 | package URI::Fast; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A fast(er) URI parser | ||||||||||||||||||||||||||||
| 3 | 4 | $URI::Fast::VERSION = '0.22'; | |||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use Inline | ||||||||||||||||||||||||||||
| 12 | 4 | C => 'DATA', | |||||||||||||||||||||||||||
| 13 | name => 'URI::Fast', | ||||||||||||||||||||||||||||
| 14 | version => eval '$URI::Fast::VERSION', | ||||||||||||||||||||||||||||
| 15 | 4 | ▶ | optimize => '-O2'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | Inline->init; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | our @EXPORT_OK = qw(uri uri_split); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use overload '""' => sub{ $_[0]->to_string }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub uri ($) { | ||||||||||||||||||||||||||||
| 25 | 76 | ▶ | my $self = URI::Fast->new($_[0]); | ||||||||||||||||||||||||||
|
Subroutine
uri: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 76 | ▶ | $self->set_scheme('file') unless $self->get_scheme; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 76 | $self; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Build a simple accessor for basic attributes | ||||||||||||||||||||||||||||
| 31 | 4 | foreach my $attr (qw(scheme usr pwd host port frag)) { | |||||||||||||||||||||||||||
| 32 | 24 | my $s = "set_$attr"; | |||||||||||||||||||||||||||
| 33 | 24 | my $g = "get_$attr"; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 24 | *{__PACKAGE__ . "::$attr"} = sub { | |||||||||||||||||||||||||||
| 36 | 95 | ▶ | if (@_ == 2) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 29 | $_[0]->$s( $_[1] ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 95 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | # It turns out that it is faster to call decode here than directly in | ||||||||||||||||||||||||||||
| 42 | # url_fast.c due to the overhead of decoding utf8 and flipping the | ||||||||||||||||||||||||||||
| 43 | # internal utf8 switch. | ||||||||||||||||||||||||||||
| 44 | 66 | return decode( $_[0]->$g() ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 24 | }; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub auth { | ||||||||||||||||||||||||||||
| 50 | 22 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
auth: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 22 | ▶ | if (@_ == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 10 | ▶ | if (ref $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $self->set_usr($val->{usr} // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"usr"} // ""
MC/DC0%0/1
$val->{"usr"} // ""
$val->{"usr"}
Truth table50%1/2
A
A= $val->{"usr"}
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | $self->set_pwd($val->{pwd} // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"pwd"} // ""
MC/DC0%0/1
$val->{"pwd"} // ""
$val->{"pwd"}
Truth table50%1/2
A
A= $val->{"pwd"}
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | $self->set_host($val->{host} // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"host"} // ""
MC/DC0%0/1
$val->{"host"} // ""
$val->{"host"}
Truth table50%1/2
A
A= $val->{"host"}
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | $self->set_port($val->{port} // ''); | ||||||||||||||||||||||||||
|
Condition50%1/2
$val->{"port"} // ""
MC/DC0%0/1
$val->{"port"} // ""
$val->{"port"}
Truth table50%1/2
A
A= $val->{"port"}
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||
| 60 | 9 | $self->set_auth($val); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 22 | ▶ | return $self->get_auth | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | if defined wantarray; | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Path is slightly more complicated as it can parse the path | ||||||||||||||||||||||||||||
| 69 | sub path { | ||||||||||||||||||||||||||||
| 70 | 31 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
path: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 31 | ▶ | if (@_ == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 13 | ▶ | $val = '/' . join '/', @$val if ref $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 13 | $self->set_path($val); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 31 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 78 | 11 | return @{ $self->split_path }; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | elsif (defined wantarray) { | ||||||||||||||||||||||||||||
| 81 | 10 | return decode($self->get_path); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Queries may be set with either a string or a hash ref | ||||||||||||||||||||||||||||
| 86 | sub query { | ||||||||||||||||||||||||||||
| 87 | 41 | ▶ | my ($self, $val, $sep) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 41 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 10 | ▶ | if (ref $val) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 3 | $self->clear_query; | |||||||||||||||||||||||||||
| 92 | $self->param($_, $val->{$_}, $sep) | ||||||||||||||||||||||||||||
| 93 | 3 | foreach keys %$val; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | 7 | $self->set_query($val); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 41 | ▶ | return unless defined(wantarray); # void context | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 35 | ▶ | return $self->get_query unless wantarray; # scalar context | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 7 | return %{ $self->query_hash }; # list context | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub query_keys { | ||||||||||||||||||||||||||||
| 106 | 4 | ▶ | keys %{ $_[0]->get_query_keys }; | ||||||||||||||||||||||||||
|
Subroutine
query_keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub param { | ||||||||||||||||||||||||||||
| 110 | 53 | ▶ | my ($self, $key, $val, $sep) = @_; | ||||||||||||||||||||||||||
|
Subroutine
param: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 53 | ▶ | $sep ||= '&'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$sep ||= "&"
MC/DC100%1/1
$sep ||= "&"
$sep
Truth table100%2/2
A
A= $sep
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 53 | ▶ | if (@_ > 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 25 | ▶ | $val = ref $val ? $val | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 115 | : defined $val ? [$val] | ||||||||||||||||||||||||||||
| 116 | : []; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 25 | $self->set_param($key, $val, $sep); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # No return value in void context | ||||||||||||||||||||||||||||
| 122 | 53 | ▶ | return unless defined(wantarray) && $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined wantarray and $key
MC/DC50%1/2
defined wantarray and $key
defined wantarray
$key
Truth table66%2/3
A and B
A= defined wantarrayB= $key
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 32 | my $params = $self->get_param($key); | |||||||||||||||||||||||||||
| 125 | 32 | ▶ | return unless @$params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 22 | ▶ | return wantarray ? @$params | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 128 | : @$params == 1 ? $params->[0] | ||||||||||||||||||||||||||||
| 129 | : $params; | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =pod | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 NAME | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | URI::Fast - A fast(er) URI parser | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 VERSION | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | version 0.22 | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | use URI::Fast qw(uri); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | my $uri = uri 'http://www.example.com/some/path?a=b&c=d'; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | if ($uri->scheme =~ /http(s)?/) { | ||||||||||||||||||||||||||||
| 154 | my @path = $uri->path; | ||||||||||||||||||||||||||||
| 155 | my $a = $uri->param('a'); | ||||||||||||||||||||||||||||
| 156 | my $b = $uri->param('b'); | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | if ($uri->path =~ /\/login/ && $uri->scheme ne 'https') { | ||||||||||||||||||||||||||||
| 160 | $uri->scheme('https'); | ||||||||||||||||||||||||||||
| 161 | $uri->param('upgraded', 1); | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | C<URI::Fast> is a faster alternative to L<URI>. It is written in C and provides | ||||||||||||||||||||||||||||
| 167 | basic parsing and modification of a URI. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | L<URI> is an excellent module; it is battle-tested, robust, and handles many | ||||||||||||||||||||||||||||
| 170 | edge cases. As a result, it is rather slower than it would otherwise be for | ||||||||||||||||||||||||||||
| 171 | more trivial cases, such as inspecting the path or updating a single query | ||||||||||||||||||||||||||||
| 172 | parameter. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 EXPORTED SUBROUTINES | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 uri | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Accepts a URI string, minimally parses it, and returns a L<URI::Fast> object. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Unless otherwise specified, all attributes serve as full accessors, allowing | ||||||||||||||||||||||||||||
| 183 | the URI segment to be both retrieved and modified. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head2 scheme | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Defaults to C<file> if not present in the URI string. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 auth | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | The authorization section is composed of the username, password, host name, and | ||||||||||||||||||||||||||||
| 192 | port number: | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | hostname.com | ||||||||||||||||||||||||||||
| 195 | someone@hostname.com | ||||||||||||||||||||||||||||
| 196 | someone:secret@hostname.com:1234 | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Setting this field may be done with a string (see the note below about | ||||||||||||||||||||||||||||
| 199 | L</ENCODING>) or a hash reference of individual field names (C<usr>, C<pwd>, | ||||||||||||||||||||||||||||
| 200 | C<host>, and C<sport>). In both cases, the existing values are completely | ||||||||||||||||||||||||||||
| 201 | replaced by the new values and any values not present are deleted. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head3 usr | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | The username segment of the authorization string. Updating this value alters | ||||||||||||||||||||||||||||
| 206 | L</auth>. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head3 pwd | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | The password segment of the authorization string. Updating this value alters | ||||||||||||||||||||||||||||
| 211 | L</auth>. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head3 host | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | The host name segment of the authorization string. May be a domain string or an | ||||||||||||||||||||||||||||
| 216 | IP address. Updating this value alters L</auth>. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head3 port | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | The port number segment of the authorization string. Updating this value alters | ||||||||||||||||||||||||||||
| 221 | L</auth>. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head2 path | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | In scalar context, returns the entire path string. In list context, returns a | ||||||||||||||||||||||||||||
| 226 | list of path segments, split by C</>. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | The path may also be updated using either a string or an array ref of segments: | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | $uri->path('/foo/bar'); | ||||||||||||||||||||||||||||
| 231 | $uri->path(['foo', 'bar']); | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head2 query | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Returns the complete query string. Does not include the leading C<?>. The query | ||||||||||||||||||||||||||||
| 236 | string may be set in several ways. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | $uri->query("foo=bar&baz=bat"); # note: no percent-encoding performed | ||||||||||||||||||||||||||||
| 239 | $uri->query({foo => 'bar', baz => 'bat'}); # foo=bar&baz=bat | ||||||||||||||||||||||||||||
| 240 | $uri->query({foo => 'bar', baz => 'bat'}, ';'); # foo=bar;baz=bat | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head3 query_keys | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Does a fast scan of the query string and returns a list of unique parameter | ||||||||||||||||||||||||||||
| 245 | names that appear in the query string. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head3 query_hash | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | Scans the query string and returns a hash ref of key/value pairs. Values are | ||||||||||||||||||||||||||||
| 250 | returned as an array ref, as keys may appear multiple times. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head3 param | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Gets or sets a parameter value. Setting a parameter value will replace existing | ||||||||||||||||||||||||||||
| 255 | values completely; the L</query> string will also be updated. Setting a | ||||||||||||||||||||||||||||
| 256 | parameter to C<undef> deletes the parameter from the URI. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | $uri->param('foo', ['bar', 'baz']); | ||||||||||||||||||||||||||||
| 259 | $uri->param('fnord', 'slack'); | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | my $value_scalar = $uri->param('fnord'); # fnord appears once | ||||||||||||||||||||||||||||
| 262 | my $value_array_ref = $uri->param('foo'); # foo appears twice | ||||||||||||||||||||||||||||
| 263 | my @value_list = $uri->param('foo'); # list context always yields a list | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # Delete 'foo' | ||||||||||||||||||||||||||||
| 266 | $uri->param('foo', undef); | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | An optional third parameter may be specified to control the character used to | ||||||||||||||||||||||||||||
| 269 | separate key/value pairs. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | $uri->param('foo', 'bar', ';'); # foo=bar | ||||||||||||||||||||||||||||
| 272 | $uri->param('baz', 'bat', ';'); # foo=bar;baz=bat | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head2 frag | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | The fragment section of the URI, excluding the leading C<#>. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head1 ENCODING | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | C<URI::Fast> tries to do the right thing in most cases with regard to reserved | ||||||||||||||||||||||||||||
| 281 | and non-ASCII characters. C<URI::Fast> will fully encode reserved and non-ASCII | ||||||||||||||||||||||||||||
| 282 | characters when setting C<individual> values. However, the "right thing" is a | ||||||||||||||||||||||||||||
| 283 | bit ambiguous when it comes to setting compound fields like L</auth>, L</path>, | ||||||||||||||||||||||||||||
| 284 | and L</query>. | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | When setting these fields with a string value, reserved characters are expected | ||||||||||||||||||||||||||||
| 287 | to be present, and are therefore accepted as-is. However, any non-ASCII | ||||||||||||||||||||||||||||
| 288 | characters will be percent-encoded (since they are unambiguous and there is no | ||||||||||||||||||||||||||||
| 289 | risk of double-encoding them). | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | $uri->auth('someone:secret@Ῥόδος.com:1234'); | ||||||||||||||||||||||||||||
| 292 | print $uri->auth; # "someone:secret@%E1%BF%AC%CF%8C%CE%B4%CE%BF%CF%82.com:1234" | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | On the other hand, when setting these fields with a I<reference> value, each | ||||||||||||||||||||||||||||
| 295 | field is fully percent-encoded: | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | $uri->auth({usr => 'some one', host => 'somewhere.com'}); | ||||||||||||||||||||||||||||
| 298 | print $uri->auth; # "some%20one@somewhere.com" | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | The same goes for return values. For compound fields returning a string, | ||||||||||||||||||||||||||||
| 301 | non-ASCII characters are decoded but reserved characters are not. When | ||||||||||||||||||||||||||||
| 302 | returning a list or reference of the deconstructed field, individual values are | ||||||||||||||||||||||||||||
| 303 | decoded of both reserved and non-ASCII characters. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head2 encode | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | Percent-encodes a string for use in a URI. By default, both reserved and UTF-8 | ||||||||||||||||||||||||||||
| 308 | chars (C<! * ' ( ) ; : @ & = + $ , / ? # [ ] %>) are encoded. | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | A second (optional) parameter provides a string containing any characters the | ||||||||||||||||||||||||||||
| 311 | caller does not wish to be decoded. An empty string will result in the default | ||||||||||||||||||||||||||||
| 312 | behavior described above. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | For example, to encode all characters in a query-like string I<except> for | ||||||||||||||||||||||||||||
| 315 | those used by the query: | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | my $encoded = URI::Fast::encode($some_string, '?&='); | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head2 decode | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Decodes a percent-encoded string. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | my $decoded = URI::Fast::decode($some_string); | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =head1 SPEED | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | See L<URI::Fast::Benchmarks>. | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =over | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =item L<URI> | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | The de facto standard. | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =item L<Panda::URI> | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | Written in C++ and purportedly very fast, but appears to only support Linux. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =back | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Thanks to L<ZipRecruiter|https://www.ziprecruiter.com> for encouraging their | ||||||||||||||||||||||||||||
| 346 | employees to contribute back to the open source ecosystem. Without their | ||||||||||||||||||||||||||||
| 347 | dedication to quality software development this distribution would not exist. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Jeff Ober <sysread@fastmail.fm> | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | This software is copyright (c) 2018 by Jeff Ober. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 358 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =cut | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | __DATA__ | ||||||||||||||||||||||||||||