blib/lib/URI/Fast.pm

filter: Statementstmt 97.6% 166 / 170 Branchbran 86.0% 86 / 100 Conditioncond 69.7% 23 / 33 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 96.1% 25 / 26 Podpod 100% 12 / 12 totaltotal 89.0% 318 / 357 CC 73 SCAR 21.5
CC
73
Cov
91%
compare
15.3
_walk
14.4
relative
10.0
CRAP 8.6
↑ Summary blib/lib/URI/Fast/IRI.pm »
1 package URI::Fast;
2  
3 19 our $XS_VERSION = our $VERSION = '0.48_01';
4 19 $VERSION =~ tr/_//;
5  
6 19 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 19 require XSLoader;
16 19 XSLoader::load('URI::Fast', $XS_VERSION);
17  
18 19 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 19 our @EXPORT_OK = qw(
21 uri iri uri_split
22 encode uri_encode url_encode
23 decode uri_decode url_decode
24 );
25  
26 19 require URI::Fast::IRI;
27  
28 19 ▶use overload 'eq' => sub{ $_[0]->compare($_[1]) };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
29  
30 sub auth {
31 25 ▶ my ($self, $val) = @_;
Subroutine auth: called CC=8 SCAR=21.2
Pod documented
32  
33 25 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 10 15 25
34 10 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 1 9 10
35 1 ▶ $self->usr($val->{usr} || '');
Condition50%1/2
$val->{'usr'} || ''
l!l
10
MC/DC0%0/1
$val->{'usr'} || ''
$val->{'usr'}
Truth table50%1/2
A
A= $val->{'usr'}
Aresult
11
00
36 1 ▶ $self->pwd($val->{pwd} || '');
Condition50%1/2
$val->{'pwd'} || ''
l!l
10
MC/DC0%0/1
$val->{'pwd'} || ''
$val->{'pwd'}
Truth table50%1/2
A
A= $val->{'pwd'}
Aresult
11
00
37 1 ▶ $self->host($val->{host} || '');
Condition50%1/2
$val->{'host'} || ''
l!l
10
MC/DC0%0/1
$val->{'host'} || ''
$val->{'host'}
Truth table50%1/2
A
A= $val->{'host'}
Aresult
11
00
38 1 ▶ $self->port($val->{port} || '');
Condition50%1/2
$val->{'port'} || ''
l!l
10
MC/DC0%0/1
$val->{'port'} || ''
$val->{'port'}
Truth table50%1/2
A
A= $val->{'port'}
Aresult
11
00
39 }
40 else {
41 9 $self->set_auth($val);
42 }
43 }
44  
45 25 ▶ return $self->get_auth
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 18 7 25
46 if defined wantarray;
47 }
48  
49 # Path is slightly more complicated as it can parse the path
50 sub path {
51 138 ▶ my ($self, $val) = @_;
Subroutine path: called CC=5 SCAR=16.1
Pod documented
52  
53 138 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 27 111 138
54 27 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 9 18 27
55 9 $self->set_path_array($val);
56 } else {
57 18 $self->set_path($val);
58 }
59 }
60  
61 138 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 92 46 138
elsif (defined wantarray) { } 22 24 46
62 92 @{ $self->split_path };
63 } elsif (defined wantarray) {
64 22 $self->get_path;
65 }
66 }
67  
68 # Queries may be set with either a string or a hash ref
69 sub query {
70 62 ▶ my ($self, $val, $sep) = @_;
Subroutine query: called CC=6 SCAR=17.9
Pod documented
71  
72 62 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 10 52 62
73 10 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 4 6 10
74 4 $self->clear_query;
75 $self->param($_, $val->{$_}, $sep)
76 4 foreach keys %$val;
77 }
78 else {
79 6 $self->set_query($val);
80 }
81 }
82  
83 62 ▶ return unless defined(wantarray); # void context
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 7 55 62
84 55 ▶ return $self->get_query unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 48 7 55
85 7 return %{ $self->query_hash }; # list context
86 }
87  
88 sub query_hash {
89 62 ▶ my $self = shift;
Subroutine query_hash: called CC=2 SCAR=7.1
Pod documented
90 62 ▶ $self->query(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 62 62
91 62 $self->get_query_hash;
92 }
93  
94 sub query_keys {
95 9 ▶ keys %{ $_[0]->get_query_keys };
Subroutine query_keys: called CC=1 SCAR=0.0
Pod documented
96 }
97  
98 sub param {
99 112 ▶ my ($self, $key, $val, $sep) = @_;
Subroutine param: called CC=10 SCAR=23.0
Pod documented
100 112 ▶ $sep ||= '&';
Condition100%2/2
$sep ||= "&"
l!l
2290
MC/DC100%1/1
$sep ||= "&"
$sep
Truth table100%2/2
A
A= $sep
Aresult
11
00
101  
102 112 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 57 55 112
103 57 ▶ $val = ref $val ? $val
Branch: 2 branches
BranchTrueFalseTotal
ref $val ? : 20 37 57
defined $val ? : 29 8 37
104 : defined $val ? [$val]
105 : [];
106  
107 57 $self->set_param($key, $val, $sep);
108 }
109  
110 # No return value in void context
111 112 ▶ return unless defined(wantarray) && $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray and $key 42 70 112
Condition66%2/3
defined wantarray and $key
!ll&&!rl&&r
42070
MC/DC50%1/2
defined wantarray and $key
defined wantarray $key
Truth table66%2/3
A and B
A= defined wantarrayB= $key
ABresult
0X0
100
111
112  
113 70 my $params = $self->get_param($key);
114 70 ▶ return unless @$params;
Branch: 1 branch
BranchTrueFalseTotal
unless @$params 12 58 70
115  
116 58 ▶ return wantarray ? @$params
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 17 41 58
@$params == 1 ? : 39 2 41
117 : @$params == 1 ? $params->[0]
118 : croak("param: multiple values encountered for query parameter '$key' when called in SCALAR context");
119 }
120  
121 sub add_param {
122 9 ▶ my ($self, $key, $val, $sep) = @_;
Subroutine add_param: called CC=1 SCAR=0.0
Pod documented
123 9 $self->param($key, [$self->param($key), $val], $sep);
124 }
125  
126 sub append {
127 1 ▶ my $self = shift;
Subroutine append: called CC=6 SCAR=17.9
Pod documented
128  
129 1 foreach my $segment (@_) {
130 5 ▶ if ($segment =~ /^\?/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($segment =~ /^\?/) { } 1 4 5
elsif (my($frag) = $segment =~ /^#(.*)$/) { } 1 3 4
131 1 my $q = uri($segment)->query_hash;
132  
133 1 foreach my $key (keys %$q) {
134 $self->add_param($key => $_)
135 1 foreach @{ $q->{$key} };
136 }
137 }
138 elsif (my ($frag) = $segment =~ /^#(.*)$/) {
139 1 $self->frag($frag);
140 }
141 else {
142 3 my @segments = ($self->path, split('/', $segment));
143 3 $self->path(\@segments);
144 }
145 }
146  
147 1 return $self;
148 }
149  
150 sub _cmp ($$) {
151 274 ▶ if (defined $_[0]) {
Subroutine _cmp: called CC=5 SCAR=18.6
Branch: 2 branches
BranchTrueFalseTotal
if (defined $_[0]) { } 274 0 274
elsif (defined $_[1]) { } 0 0 0
152 274 ▶ return if !defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 274 274
153 274 ▶ return if $_[0] ne $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] ne $_[1] 26 248 274
154 }
155 elsif (defined $_[1]) {
156 0 return;
157 }
158  
159 248 return 1;
160 }
161  
162 sub compare {
163 52 ▶ my ($self, $other) = @_;
Subroutine compare: called CC=15 SCAR=27.3
Pod documented
164 52 $other = uri($other);
165  
166 52 ▶ return unless _cmp($self->scheme, $other->scheme)
Branch: 1 branch
BranchTrueFalseTotal
unless &_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr) and &_cmp(scalar $self->pwd, scalar $other->pwd) and &_cmp(scalar $self->host, scalar $other->host) and &_cmp(scalar $self->port, scalar $other->port) and &_cmp(scalar $self->frag, scalar $other->frag) 5 34 39
Condition73%11/15
&_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr) and &_cmp(scalar $self->pwd, scalar $other->pwd) and &_cmp(scalar $self->host, scalar $other->host) and &_cmp(scalar $self->port, scalar $other->port) and &_cmp(scalar $self->frag, scalar $other->frag)
!ll&&!rl&&r
13534
&_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr) and &_cmp(scalar $self->pwd, scalar $other->pwd) and &_cmp(scalar $self->host, scalar $other->host) and &_cmp(scalar $self->port, scalar $other->port)
!ll&&!rl&&r
13039
&_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr) and &_cmp(scalar $self->pwd, scalar $other->pwd) and &_cmp(scalar $self->host, scalar $other->host)
!ll&&!rl&&r
13039
&_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr) and &_cmp(scalar $self->pwd, scalar $other->pwd)
!ll&&!rl&&r
13039
&_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr)
!ll&&!rl&&r
13039
MC/DC33%2/6
&_cmp(scalar $self->scheme, scalar $other->scheme) and &_cmp(scalar $self->usr, scalar $other->usr) and &_cmp(scalar $self->pwd, scalar $other->pwd) and &_cmp(scalar $self->host, scalar $other->host) and &_cmp(scalar $self->port, scalar $other->port) and &_cmp(scalar $self->frag, scalar $other->frag)
&_cmp(scalar $self->scheme, scalar $other->scheme) &_cmp(scalar $self->usr, scalar $other->usr) &_cmp(scalar $self->pwd, scalar $other->pwd) &_cmp(scalar $self->host, scalar $other->host) &_cmp(scalar $self->port, scalar $other->port) &_cmp(scalar $self->frag, scalar $other->frag)
Truth table42%3/7
A and B and C and D and E and F
A= &_cmp(scalar $self->scheme, scalar $other->scheme)B= &_cmp(scalar $self->usr, scalar $other->usr)C= &_cmp(scalar $self->pwd, scalar $other->pwd)D= &_cmp(scalar $self->host, scalar $other->host)E= &_cmp(scalar $self->port, scalar $other->port)F= &_cmp(scalar $self->frag, scalar $other->frag)
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
167 && _cmp($self->usr, $other->usr)
168 && _cmp($self->pwd, $other->pwd)
169 && _cmp($self->host, $other->host)
170 && _cmp($self->port, $other->port)
171 && _cmp($self->frag, $other->frag);
172  
173 34 my @spath = $self->path;
174 34 my @opath = $other->path;
175 34 ▶ return unless @spath == @opath;
Branch: 1 branch
BranchTrueFalseTotal
unless @spath == @opath 12 22 34
176  
177 22 foreach (0 .. $#spath) {
178 17 ▶ return unless _cmp($spath[$_], $opath[$_]);
Branch: 1 branch
BranchTrueFalseTotal
unless &_cmp($spath[$_], $opath[$_]) 7 10 17
179 }
180  
181 15 my $sparam = $self->query_hash;
182 15 my $oparam = $other->query_hash;
183  
184 15 foreach my $k (keys %$sparam) {
185 13 ▶ return unless exists $oparam->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $oparam->{$k} 3 10 13
186 10 ▶ return unless @{$sparam->{$k}} == @{$oparam->{$k}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$sparam->{$k};} == @{$oparam->{$k};} 0 10 10
187  
188 10 foreach (0 .. $#{$sparam->{$k}}) {
189 10 ▶ return unless _cmp($sparam->{$k}[$_], $oparam->{$k}[$_]);
Branch: 1 branch
BranchTrueFalseTotal
unless &_cmp($sparam->{$k}[$_], $oparam->{$k}[$_]) 1 9 10
190 }
191 }
192  
193 11 return 1;
194 }
195  
196 #-------------------------------------------------------------------------------
197 # Relativism
198 #-------------------------------------------------------------------------------
199 sub relative {
200 7 ▶ my ($self, $base) = @_;
Subroutine relative: called CC=10 SCAR=23.1
Pod documented
201 7 my $rel = (ref $self)->new("$self");
202  
203 7 my $rpath = $self->path;
204 7 ($_, $_, my $bpath) = uri_split("$base");
205  
206 7 ▶ if ($bpath =~ m|/$| && $rpath eq $bpath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpath =~ m[/$] and $rpath eq $bpath) { } 3 4 7
Condition100%3/3
$bpath =~ m[/$] and $rpath eq $bpath
!ll&&!rl&&r
313
MC/DC100%2/2
$bpath =~ m[/$] and $rpath eq $bpath
$bpath =~ m[/$] $rpath eq $bpath
Truth table100%3/3
A and B
A= $bpath =~ m[/$]B= $rpath eq $bpath
ABresult
0X0
100
111
207 3 $rel->path('./');
208 }
209 else {
210 # Ensure both paths begin with '/'
211 4 ▶ $rpath = "/$rpath" unless $rpath =~ m|^/|;
Branch: 1 branch
BranchTrueFalseTotal
unless $rpath =~ m[^/] 0 4 4
212 4 ▶ $bpath = "/$bpath" unless $bpath =~ m|^/|;
Branch: 1 branch
BranchTrueFalseTotal
unless $bpath =~ m[^/] 0 4 4
213  
214 # Remove common leading path segments
215 4 my ($idx, $brk) = (1, 0);
216  
217 # Scan for matching segments
218 4 while (1) {
219 # Locate the next delimiter
220 5 $brk = index($rpath, '/', $idx);
221  
222 5 ▶ last if $brk < 0; # not found
Branch: 1 branch
BranchTrueFalseTotal
if $brk < 0 1 4 5
223 4 ▶ last if $brk != index($bpath, '/', $idx); # no match: different segment lengths
Branch: 1 branch
BranchTrueFalseTotal
if $brk != index($bpath, "/", $idx) 2 2 4
224 2 ▶ last if substr($rpath, $idx, $brk - $idx) # no match: segments have different string values
Branch: 1 branch
BranchTrueFalseTotal
if substr($rpath, $idx, $brk - $idx) ne substr($bpath, $idx, $brk - $idx) 1 1 2
225 ne substr($bpath, $idx, $brk - $idx);
226  
227 1 $idx = $brk + 1; # move past delimiter
228 }
229  
230 # $idx now matches the slash after the common prefix; remove path contents
231 # up to that point.
232 4 substr($rpath, 0, $idx) = '';
233 4 substr($bpath, 0, $idx) = '';
234  
235 # Add ../ for each remaining base path segment
236 4 ▶ while ($bpath =~ m|/|gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpath =~ m[/]cg) 2 4 6
237 2 $rpath = "../$rpath";
238 }
239  
240 # An empty path becomes ./
241 4 ▶ $rpath ||= './';
Condition50%1/2
$rpath ||= "./"
l!l
40
MC/DC0%0/1
$rpath ||= "./"
$rpath
Truth table50%1/2
A
A= $rpath
Aresult
11
00
242  
243 4 $rel->path($rpath);
244 }
245  
246 7 $rel->clear_scheme;
247 7 $rel->clear_auth;
248  
249 7 return $rel;
250 }
251  
252 sub _walk {
253 3 ▶ my ($ref, $sub) = @_;
Subroutine _walk: called CC=13 SCAR=26.6
254 3 my @stack = ($ref);
255 3 my %seen;
256  
257 3 ▶ while (my $elt = shift @stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $elt = shift @stack) 9 3 12
258 9 my $ref = ref $elt;
259  
260 9 ▶ if (defined $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) 9 0 9
261 9 my $addr = refaddr $elt;
262 9 ▶ next if exists $seen{ $addr };
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$addr} 1 8 9
263 8 $seen{ $addr } = 1;
264  
265 8 ▶ if ($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 2 6 8
elsif ($ref eq 'HASH') { } 6 0 6
elsif ($ref eq 'SCALAR') { } 0 0 0
266 2 for (0 .. @$elt - 1) {
267 4 ▶ if (ref $elt->[$_]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $elt->[$_]) { } 0 4 4
elsif (defined $elt->[$_]) { } 4 0 4
268 0 push @stack, $elt->[$_];
269 }
270 elsif (defined $elt->[$_]) {
271 4 $elt->[$_] = $sub->( $elt->[$_] );
272 }
273 }
274 }
275 elsif ($ref eq 'HASH') {
276 6 foreach (keys %$elt) {
277 11 ▶ if (ref $elt->{$_}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $elt->{$_}) { } 6 5 11
elsif (defined $elt->{$_}) { } 3 2 5
278 6 push @stack, $elt->{$_};
279 }
280 elsif (defined $elt->{$_}) {
281 3 $elt->{$_} = $sub->( $elt->{$_} );
282 }
283 }
284 }
285 elsif ($ref eq 'SCALAR') {
286 0 push @stack, $$elt;
287 }
288 }
289 }
290  
291 3 $ref;
292 }
293  
294 sub escape_tree {
295 3 ▶ my ($ref, @args) = @_;
Subroutine escape_tree: called CC=2 SCAR=6.9
Pod documented
296 3 ▶ ref $ref || croak "escape_tree: reference expected";
Branch: 1 branch
BranchTrueFalseTotal
ref $ref or croak("escape_tree: reference expected") 1 2 3
297  
298 my $sub = sub{
299 4 ▶ push @_, @args;
Subroutine __ANON__: called CC=1 SCAR=0.0
300 4 goto \&encode;
301 2 };
302  
303 2 _walk($ref, $sub);
304 }
305  
306 sub unescape_tree {
307 2 ▶ my ($ref, @args) = @_;
Subroutine unescape_tree: called CC=2 SCAR=6.9
Pod documented
308 2 ▶ ref $ref || croak "unescape_tree: reference expected";
Branch: 1 branch
BranchTrueFalseTotal
ref $ref or croak("unescape_tree: reference expected") 1 1 2
309  
310 my $sub = sub{
311 3 ▶ push @_, @args;
Subroutine __ANON__: called CC=1 SCAR=0.0
312 3 goto \&decode;
313 1 };
314  
315 1 _walk($ref, $sub);
316 }
317  
318 =encoding UTF8
319  
320 =head1 NAME
321  
322 URI::Fast - A fast(er) URI parser
323  
324 =head1 SYNOPSIS
325  
326 use URI::Fast qw(uri);
327  
328 my $uri = uri 'http://www.example.com/some/path?fnord=slack&foo=bar';
329  
330 if ($uri->scheme =~ /http(s)?/) {
331 my @path = $uri->path;
332 my $fnord = $uri->param('fnord');
333 my $foo = $uri->param('foo');
334 }
335  
336 if ($uri->path =~ /\/login/ && $uri->scheme ne 'https') {
337 $uri->scheme('https');
338 $uri->param('upgraded', 1);
339 }
340  
341 =head1 DESCRIPTION
342  
343 C<URI::Fast> is a faster alternative to L<URI>. It is written in C and provides
344 basic parsing and modification of a URI.
345  
346 L<URI> is an excellent module; it is battle-tested, robust, and handles many
347 edge cases. As a result, it is rather slower than it would otherwise be for
348 more trivial cases, such as inspecting the path or updating a single query
349 parameter.
350  
351 =head1 EXPORTED SUBROUTINES
352  
353 Subroutines are exported on demand.
354  
355 =head2 uri
356  
357 Accepts a URI string, minimally parses it, and returns a C<URI::Fast> object.
358  
359 Note: passing a C<URI::Fast> instance to this routine will cause the object to
360 be interpolated into a string (via L</to_string>), effectively creating a clone
361 of the original C<URI::Fast> object.
362  
363 =head2 iri
364  
365 Similar to L</uri>, but returns a C<URI::Fast::IRI> object. A C<URI::Fast::IRI>
366 differs from a C<URI::Fast> in that UTF-8 characters are permitted and will not
367 be percent-encoded when modified.
368  
369 =head2 uri_split
370  
371 Behaves (hopefully) identically to L<URI::Split>, but roughly twice as fast.
372  
373 =head2 encode/decode/uri_encode/uri_decode
374  
375 See L</ENCODING>.
376  
377 =head1 CONSTRUCTOR
378  
379 If desired, both C<URI::Fast> and L<URI::Fast::IRI> may be instantiated using
380 the default constructor, C<new>.
381  
382 my $uri = URI::Fast->new('http://www.example.com');
383  
384 =head1 ATTRIBUTES
385  
386 All attributes serve as full accessors, allowing the URI segment to be both
387 retrieved and modified.
388  
389 Each attribute defines a C<raw_*> method, which returns the raw, encoded string
390 value for that attribute.
391  
392 Each attribute further has a matching clearer method (C<clear_*>) which unsets
393 its value.
394  
395 In general, accessors accept an I<unencoded> string and set their slot value to
396 the I<encoded> value. They return the decoded value. See L</ENCODING> for an in
397 depth description of their behavior as well as an explanation of the more
398 complex behavior of compound fields.
399  
400 =head2 scheme
401  
402 Gets or sets the scheme portion of the URI (e.g. C<http>), excluding C<://>.
403  
404 =head2 auth
405  
406 The authorization section is composed of the username, password, host name, and
407 port number:
408  
409 hostname.com
410 someone@hostname.com
411 someone:secret@hostname.com:1234
412  
413 Setting this field may be done with a string (see the note below about
414 L</ENCODING>) or a hash reference of individual field names (C<usr>, C<pwd>,
415 C<host>, and C<port>). In both cases, the existing values are completely
416 replaced by the new values and any values missing from the caller-supplied
417 input are deleted.
418  
419 =head3 usr
420  
421 The username segment of the authorization string. Updating this value alters
422 L</auth>.
423  
424 =head3 pwd
425  
426 The password segment of the authorization string. Updating this value alters
427 L</auth>.
428  
429 =head3 host
430  
431 The host name segment of the authorization string. May be a domain string or an
432 IP address. If the host is an IPV6 address, it must be surrounded by square
433 brackets (per spec), which are included in the host string. Updating this value
434 alters L</auth>.
435  
436 =head3 port
437  
438 The port number segment of the authorization string. Updating this value alters
439 L</auth>.
440  
441 =head2 path
442  
443 In scalar context, returns the entire path string. In list context, returns a
444 list of path segments, split by C</>.
445  
446 my $uri = uri '/foo/bar';
447 my $path = $uri->path; # "/foo/bar"
448 my @path = $uri->path; # ("foo", "bar")
449  
450 The path may also be updated using either a string or an array ref of segments:
451  
452 $uri->path('/foo/bar');
453 $uri->path(['foo', 'bar']);
454  
455 This differs from the behavior of L<URI/path_segments>, which considers the
456 leading slash separating the path from the authority section to be an
457 individual segment. If this behavior is desired, the lower level
458 C<split_path_compat> is available. C<split_path_compat> (and its partner,
459 C<split_path>), always return an array reference.
460  
461 my $uri = uri '/foo/bar';
462 $uri->split_path; # ['foo', 'bar'];
463 $uri->split_path_compat; # ['', 'foo', 'bar'];
464  
465 =head2 query
466  
467 In scalar context, returns the complete query string, excluding the leading
468 C<?>. The query string may be set in several ways.
469  
470 $uri->query("foo=bar&baz=bat"); # note: no percent-encoding performed
471 $uri->query({foo => 'bar', baz => 'bat'}); # foo=bar&baz=bat
472 $uri->query({foo => 'bar', baz => 'bat'}, ';'); # foo=bar;baz=bat
473  
474 In list context, returns a hash ref mapping query keys to array refs of their
475 values (see L</query_hash>).
476  
477 Both '&' and ';' are treated as separators for key/value parameters.
478  
479 =head2 frag
480  
481 The fragment section of the URI, excluding the leading C<#>.
482  
483 =head1 METHODS
484  
485 =head2 query_keys
486  
487 Does a fast scan of the query string and returns a list of unique parameter
488 names that appear in the query string.
489  
490 Both '&' and ';' are treated as separators for key/value parameters.
491  
492 =head2 query_hash
493  
494 Scans the query string and returns a hash ref of key/value pairs. Values are
495 returned as an array ref, as keys may appear multiple times. Both '&' and ';'
496 are treated as separators for key/value parameters.
497  
498 May optionally be called with a new hash of parameters to replace the query
499 string with, in which case keys may map to scalar values or arrays of scalar
500 values. As with all query setter methods, a third parameter may be used to
501 explicitly specify the separator to use when generating the new query string.
502  
503 =head2 param
504  
505 Gets or sets a parameter value. Setting a parameter value will replace existing
506 values completely; the L</query> string will also be updated. Setting a
507 parameter to C<undef> deletes the parameter from the URI.
508  
509 $uri->param('foo', ['bar', 'baz']);
510 $uri->param('fnord', 'slack');
511  
512 my $value_scalar = $uri->param('fnord'); # fnord appears once
513 my @value_list = $uri->param('foo'); # foo appears twice
514 my $value_scalar = $uri->param('foo'); # croaks; expected single value but foo has multiple
515  
516 # Delete parameter
517 $uri->param('foo', undef); # deletes foo
518  
519 # Ambiguous cases
520 $uri->param('foo', ''); # foo=
521 $uri->param('foo', '0'); # foo=0
522 $uri->param('foo', ' '); # foo=%20
523  
524 Both '&' and ';' are treated as separators for key/value parameters when
525 parsing the query string. An optional third parameter explicitly selects the
526 character used to separate key/value pairs.
527  
528 $uri->param('foo', 'bar', ';'); # foo=bar
529 $uri->param('baz', 'bat', ';'); # foo=bar;baz=bat
530  
531 When unspecified, '&' is chosen as the default. I<In either case, all
532 separators in the query string will be normalized to the chosen separator>.
533  
534 $uri->param('foo', 'bar', ';'); # foo=bar
535 $uri->param('baz', 'bat', ';'); # foo=bar;baz=bat
536 $uri->param('fnord', 'slack'); # foo=bar&baz=bat&fnord=slack
537  
538 =head2 add_param
539  
540 Updates the query string by adding a new value for the specified key. If the
541 key already exists in the query string, the new value is appended without
542 altering the original value.
543  
544 $uri->add_param('foo', 'bar'); # foo=bar
545 $uri->add_param('foo', 'baz'); # foo=bar&foo=baz
546  
547 This method is simply sugar for calling:
548  
549 $uri->param('key', [$uri->param('key'), 'new value']);
550  
551 As with L</param>, the separator character may be specified as the final
552 parameter. The same caveats apply with regard to normalization of the query
553 string separator.
554  
555 $uri->add_param('foo', 'bar', ';'); # foo=bar
556 $uri->add_param('foo', 'baz', ';'); # foo=bar;foo=baz
557  
558 =head2 query_keyset
559  
560 Allows modification of the query string in the manner of a set, using keys
561 without C<=value>, e.g. C<foo&bar&baz>. Accepts a hash ref of keys to update.
562 A truthy value adds the key, a falsey value removes it. Any keys not mentioned
563 in the update hash are left unchanged.
564  
565 my $uri = uri '&baz&bat';
566 $uri->query_keyset({foo => 1, bar => 1}); # baz&bat&foo&bar
567 $uri->query_keyset({baz => 0, bat => 0}); # foo&bar
568  
569 If there are key-value pairs in the query string as well, the behavior of
570 this method becomes a little more complex. When a key is specified in the
571 hash update hash ref, a positive value will leave an existing key/value pair
572 untouched. A negative value will remove the key and value.
573  
574 my $uri = uri '&foo=bar&baz&bat';
575 $uri->query_keyset({foo => 1, baz => 0}); # foo=bar&bat
576  
577 An optional second parameter may be specified to control the separator
578 character used when updating the query string. The same caveats apply with
579 regard to normalization of the query string separator.
580  
581 =head2 append
582  
583 Serially appends path segments, query strings, and fragments, to the end of the
584 URI. Each argument is added in order. If the segment begins with C<?>, it is
585 assumed to be a query string and it is appended using L</add_param>. If the
586 segment begins with C<#>, it is treated as a fragment, replacing any existing
587 fragment. Otherwise, the segment is treated as a path fragment and appended to
588 the path.
589  
590 my $uri = uri 'http://www.example.com/foo?k=v';
591 $uri->append('bar', 'baz/bat', '?k=v1&k=v2', '#fnord', 'slack');
592 # 'http://www.example.com/foo/bar/baz/bat/slack?k=v&k=v1&k=v2#fnord'
593  
594  
595 =head2 to_string
596  
597 =head2 as_string
598  
599 =head2 "$uri"
600  
601 Stringifies the URI, encoding output as necessary. String interpolation is
602 overloaded.
603  
604 =head2 compare
605  
606 =head2 $uri eq $other
607  
608 Compares the URI to another, returning true if the URIs are equivalent.
609 Overloads the C<eq> operator.
610  
611 =head2 clone
612  
613 Sugar for:
614  
615 my $uri = uri '...';
616 my $clone = uri $uri;
617  
618 =head2 absolute
619  
620 Builds an absolute URI from a relative URI and a base URI string.
621 Adheres as strictly as possible to the rules for resolving a target URI in
622 L<RFC3986 section 5.2|https://www.rfc-editor.org/rfc/rfc3986.txt>. Returns a new
623 L<URI::Fast> object representing the absolute, merged URI.
624  
625 my $uri = uri('some/path')->absolute('http://www.example.com/fnord');
626 $uri->to_string; # "http://www.example.com/fnord/some/path"
627  
628 =head2 relative
629  
630 Builds a relative URI using a second URI (either a C<URI::Fast> object or a
631 string) as a base. Unlike L<URI/rel>, ignores differences in domain and scheme
632 assumes the caller wishes to adopt the base URL's instead. Aside from that difference,
633 it's behavior should mimic L<URI/rel>'s.
634  
635 my $uri = uri('http://example.com/foo/bar')->relative('http://example.com/foo');
636 $uri->to_string; # "foo/bar"
637  
638 my $uri = uri('http://example.com/foo/bar/')->relative('http://example.com/foo');
639 $uri->to_string; # "foo/bar/"
640  
641 =head2 normalize
642  
643 Similar to L<URI/canonical>, performs a minimal normalization on the URI. Only
644 generic normalization described in the rfc is performed; no scheme-specific
645 normalization is done. Specifically, the scheme and host members are converted
646 to lower case, dot segments are collapsed in the path, and any percent-encoded
647 characters in the URI are converted to upper case.
648  
649 =head1 ENCODING
650  
651 C<URI::Fast> tries to do the right thing in most cases with regard to reserved
652 and non-ASCII characters. C<URI::Fast> will fully encode reserved and non-ASCII
653 characters when setting I<individual> values and return their fully decoded
654 values. However, the "right thing" is somewhat ambiguous when it comes to
655 setting compound fields like L</auth>, L</path>, and L</query>.
656  
657 When setting compound fields with a string value, reserved characters are
658 expected to be present, and are therefore accepted as-is. Any non-ASCII
659 characters will be percent-encoded (since they are unambiguous and there is no
660 risk of double-encoding them). Thus,
661  
662 $uri->auth('someone:secret@Ῥόδος.com:1234');
663 print $uri->auth; # "someone:secret@%E1%BF%AC%CF%8C%CE%B4%CE%BF%CF%82.com:1234"
664  
665 On the other hand, when setting these fields with a I<reference> value (assumed
666 to be a hash ref for L</auth> and L</query> or an array ref for L</path>; see
667 individual methods' docs for details), each field is fully percent-encoded,
668 just as if each individual simple slot's setter had been called:
669  
670 $uri->auth({usr => 'some one', host => 'somewhere.com'});
671 print $uri->auth; # "some%20one@somewhere.com"
672 print $uri->usr;; # "some one"
673  
674 The same goes for return values. For compound fields returning a string,
675 non-ASCII characters are decoded but reserved characters are not. When
676 returning a list or reference of the deconstructed field, individual values are
677 decoded of both reserved and non-ASCII characters.
678  
679 =head2 '+' vs '%20'
680  
681 Although no longer part of the standard, C<+> is commonly used as the encoded
682 space character (rather than C<%20>); it I<is> still official to the
683 C<application/x-www-form-urlencoded> type, and is treated as a space by
684 L</decode>.
685  
686 =head2 encode
687  
688 Percent-encodes a string for use in a URI. By default, both reserved and UTF-8
689 chars (C<! * ' ( ) ; : @ & = + $ , / ? # [ ] %>) are encoded.
690  
691 A second (optional) parameter provides a string containing any characters the
692 caller does not wish to be encoded. An empty string will result in the default
693 behavior described above.
694  
695 For example, to encode all characters in a query-like string I<except> for
696 those used by the query:
697  
698 my $encoded = URI::Fast::encode($some_string, '?&=');
699  
700 =head2 decode
701  
702 Decodes a percent-encoded string.
703  
704 my $decoded = URI::Fast::decode($some_string);
705  
706 =head2 uri_encode
707  
708 =head2 uri_decode
709  
710 These are aliases of L</encode> and L</decode>, respectively. They were added
711 to make L<BLUEFEET|https://metacpan.org/author/BLUEFEET> happy after he made
712 fun of me for naming L</encode> and L</decode> too generically.
713  
714 In fact, these were originally aliased as C<url_encode> and C<url_decode>, but
715 due to some pedantic whining on the part of
716 L<BGRIMM|https://metacpan.org/author/BGRIMM>, they have been renamed to
717 C<uri_encode> and C<uri_decode>.
718  
719 =head2 escape_tree
720  
721 =head2 unescape_tree
722  
723 Traverses a data structure, escaping or unescaping I<defined> scalar values in
724 place. Accepts a reference to be traversed. Any further parameters are passed
725 unchanged to L</encode> or L</decode>. Croaks if the input to escape/unescape
726 is a non-reference value.
727  
728 my $obj = {
729 foo => ['bar baz', 'bat%fnord'],
730 bar => {baz => 'bat%bat'},
731 baz => undef,
732 bat => '',
733 };
734  
735 URI::Fast::escape_tree($obj);
736  
737 # $obj is now:
738 {
739 foo => ['bar%20baz', 'bat%25fnord'],
740 bar => {baz => 'bat%25bat'},
741 baz => undef,
742 bat => '',
743 }
744  
745 URI::Fast::unescape_tree($obj); # $obj returned to original form
746  
747 URI::Fast::escape_tree($obj, '%'); # escape but allow "%"
748  
749 # $obj is now:
750 {
751 foo => ['bar%20baz', 'bat%fnord'],
752 bar => {baz => 'bat%bat'},
753 baz => undef,
754 bat => '',
755 }
756  
757 =head1 SPEED
758  
759 See L<URI::Fast::Benchmarks>.
760  
761 =head1 SEE ALSO
762  
763 =over
764  
765 =item L<URI>
766  
767 The de facto standard.
768  
769 =item L<RFC 3986|https://www.rfc-editor.org/rfc/rfc3986.txt>
770  
771 The official standard.
772  
773 =back
774  
775 =head1 ACKNOWLEDGEMENTS
776  
777 Thanks to L<ZipRecruiter|https://www.ziprecruiter.com> for encouraging their
778 employees to contribute back to the open source ecosystem. Without their
779 dedication to quality software development this distribution would not exist.
780  
781 =head1 CONTRIBUTORS
782  
783 The following people have contributed to this module with patches, bug reports,
784 API advice, identifying areas where the documentation is unclear, or by making
785 fun of me for naming certain methods too generically.
786  
787 =over
788  
789 =item Andy Ruder
790  
791 =item Aran Deltac (BLUEFEET)
792  
793 =item Ben Grimm (BGRIMM)
794  
795 =item Dave Hubbard (DAVEH)
796  
797 =item James Messrie
798  
799 =item Martin Locklear
800  
801 =item Randal Schwartz (MERLYN)
802  
803 =item Sara Siegal (SSIEGAL)
804  
805 =item Tim Vroom (VROOM)
806  
807 =item Des Daignault (NAWGLAN)
808  
809 =item Josh Rosenbaum
810  
811 =back
812  
813 =head1 AUTHOR
814  
815 Jeff Ober <sysread@fastmail.fm>
816  
817 =head1 COPYRIGHT AND LICENSE
818  
819 This software is copyright (c) 2018 by Jeff Ober. This is free software; you
820 can redistribute it and/or modify it under the same terms as the Perl 5
821 programming language system itself.
822  
823 =cut
824  
825 19 1;
↑ Summary blib/lib/URI/Fast/IRI.pm »