Mojo/URL.pm

filter: Statementstmt 100% 110 / 110 Branchbran 97.6% 82 / 84 Conditioncond 81.4% 22 / 27 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 26 / 26 Podpod 100% 15 / 15 totaltotal 94.2% 262 / 278 CC 54 SCAR 16.5
CC
54
Cov
97%
_string
10.0
parse
7.0
query
7.0
CRAP 5.2
« Mojo/IOLoop/Stream.pm ↑ Summary Mojolicious/Types.pm »
1 package Mojo::URL;
2 31 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4  
5 31 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use Mojo::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use Mojo::Util qw(decode encode punycode_decode punycode_encode url_escape url_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 292 ▶has base => sub { Mojo::URL->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
10 31 has [qw(fragment host port scheme userinfo)];
11  
12 427 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
13 427 my $self = shift;
14 427 my $clone = $self->new;
15 427 @$clone{keys %$self} = values %$self;
16 427 ▶ $clone->{$_} && ($clone->{$_} = $clone->{$_}->clone) for qw(base path query);
Branch: 1 branch
BranchTrueFalseTotal
$clone->{$_} and $clone->{$_} = $clone->{$_}->clone 422 859 1281
17 427 return $clone;
18 }
19  
20 1556 ▶sub host_port {
Subroutine host_port: called CC=6 SCAR=17.9
Pod documented
21 1556 my ($self, $host_port) = @_;
22  
23 1556 ▶ if (defined $host_port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $host_port) 701 855 1556
24 701 ▶ $self->port($1) if $host_port =~ s/:(\d+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $host_port =~ s/:(\d+)$//u 382 319 701
25 701 my $host = url_unescape $host_port;
26 701 ▶ return $host =~ /[^\x00-\x7f]/ ? $self->ihost($host) : $self->host($host);
Branch: 1 branch
BranchTrueFalseTotal
$host =~ /[^\x00-\x7f]/u ? : 17 684 701
27 }
28  
29 855 ▶ return undef unless defined(my $host = $self->ihost);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $host = $self->ihost) 397 458 855
30 458 ▶ return $host unless defined(my $port = $self->port);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $port = $self->port) 228 230 458
31 230 return "$host:$port";
32 }
33  
34 1379 ▶sub ihost {
Subroutine ihost: called CC=6 SCAR=17.9
Pod documented
35 1379 my $self = shift;
36  
37 # Decode
38 1379 ▶ return $self->host(join '.', map { /^xn--(.+)$/ ? punycode_decode $1 : $_ } split(/\./, shift, -1)) if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 18 1361 1379
/^xn--(.+)$/u ? : 2 43 45
39  
40 # Check if host needs to be encoded
41 1361 ▶ return undef unless defined(my $host = $self->host);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $host = $self->host) 397 964 1361
42 964 ▶ return $host unless $host =~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $host =~ /[^\x00-\x7f]/u 932 32 964
43  
44 # Encode
45 32 ▶ return join '.', map { /[^\x00-\x7f]/ ? ('xn--' . punycode_encode $_) : $_ } split(/\./, $host, -1);
Branch: 1 branch
BranchTrueFalseTotal
/[^\x00-\x7f]/u ? : 43 39 82
46 }
47  
48 374 ▶sub is_abs { !!shift->scheme }
Subroutine is_abs: called CC=1 SCAR=0.0
Pod documented
49  
50 1854 ▶sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }
Subroutine new: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 369 1485 1854
51  
52 843 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.5
Pod documented
53 843 my ($self, $url) = @_;
54  
55 # Official regex from RFC 3986
56 843 $url =~ m!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!;
57 843 ▶ $self->scheme($2) if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 497 346 843
58 843 ▶ $self->path($5) if defined $5;
Branch: 1 branch
BranchTrueFalseTotal
if defined $5 843 0 843
59 843 ▶ $self->query($7) if defined $7;
Branch: 1 branch
BranchTrueFalseTotal
if defined $7 52 791 843
60 843 ▶ $self->fragment(_decode(url_unescape $9)) if defined $9;
Branch: 1 branch
BranchTrueFalseTotal
if defined $9 35 808 843
61 843 ▶ if (defined(my $auth = $4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $auth = $4)) 495 348 843
62 495 ▶ $self->userinfo(_decode(url_unescape $1)) if $auth =~ s/^([^\@]+)\@//;
Branch: 1 branch
BranchTrueFalseTotal
if $auth =~ s/^([^\@]+)\@//u 17 478 495
63 495 $self->host_port($auth);
64 }
65  
66 843 return $self;
67 }
68  
69 6 ▶sub password { (shift->userinfo // '') =~ /:(.*)$/ ? $1 : undef }
Subroutine password: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
((shift())->userinfo // '') =~ /:(.*)$/u ? : 4 2 6
Condition100%2/2
(shift())->userinfo // ""
l!l
51
MC/DC100%1/1
(shift())->userinfo // ""
(shift())->userinfo
Truth table100%2/2
A
A= (shift())->userinfo
Aresult
11
00
70  
71 2951 ▶sub path {
Subroutine path: called CC=4 SCAR=13.9
Pod documented
72 2951 my $self = shift;
73  
74 # Old path
75 2951 ▶ $self->{path} ||= Mojo::Path->new;
Condition66%2/3
$self->{"path"} ||= "Mojo::Path"->new
l!l&&r!l&&!r
163013210
MC/DC0%0/2
$self->{"path"} ||= "Mojo::Path"->new
$self->{"path"} "Mojo::Path"->new
Truth table66%2/3
A ||= B
A= $self->{"path"}B= "Mojo::Path"->new
ABresult
1X1
011
000
76 2951 ▶ return $self->{path} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1809 1142 2951
77  
78 # New path
79 1142 ▶ $self->{path} = ref $_[0] ? $_[0] : $self->{path}->merge($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 46 1096 1142
80  
81 1142 return $self;
82 }
83  
84 1100 ▶sub path_query {
Subroutine path_query: called CC=5 SCAR=16.1
Pod documented
85 1100 my ($self, $pq) = @_;
86  
87 1100 ▶ if (defined $pq) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pq) 242 858 1100
88 242 ▶ return $self unless $pq =~ /^([^?#]*)(?:\?([^#]*))?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pq =~ /^([^?#]*)(?:\?([^#]*))?/u 0 242 242
89 242 ▶ return defined $2 ? $self->path($1)->query($2) : $self->path($1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 29 213 242
90 }
91  
92 858 my $query = $self->query->to_string;
93 858 ▶ return $self->path->to_string . (length $query ? "?$query" : '');
Branch: 1 branch
BranchTrueFalseTotal
length $query ? : 109 749 858
94 }
95  
96 1415 ▶sub protocol { lc(shift->scheme // '') }
Subroutine protocol: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
(shift())->scheme // ""
l!l
1014401
MC/DC100%1/1
(shift())->scheme // ""
(shift())->scheme
Truth table100%2/2
A
A= (shift())->scheme
Aresult
11
00
97  
98 1140 ▶sub query {
Subroutine query: called CC=7 SCAR=19.5
Pod documented
99 1140 my $self = shift;
100  
101 # Old parameters
102 1140 ▶ my $q = $self->{query} ||= Mojo::Parameters->new;
Condition66%2/3
$self->{"query"} ||= "Mojo::Parameters"->new
l!l&&r!l&&!r
4327080
MC/DC0%0/2
$self->{"query"} ||= "Mojo::Parameters"->new
$self->{"query"} "Mojo::Parameters"->new
Truth table66%2/3
A ||= B
A= $self->{"query"}B= "Mojo::Parameters"->new
ABresult
1X1
011
000
103 1140 ▶ return $q unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1041 99 1140
104  
105 # Replace with list
106 99 ▶ if (@_ > 1) { $q->pairs([])->parse(@_) }
Branch: 3 branches
BranchTrueFalseTotal
if (@_ > 1) { } 2 97 99
elsif (ref $_[0] eq 'HASH') { } 3 94 97
elsif (ref $_[0] eq 'ARRAY') { } 2 92 94
107  
108 # Merge with hash
109 3 elsif (ref $_[0] eq 'HASH') { $q->merge(%{$_[0]}) }
110  
111 # Append array
112 2 elsif (ref $_[0] eq 'ARRAY') { $q->append(@{$_[0]}) }
113  
114 # New parameters
115 92 ▶ else { $self->{query} = ref $_[0] ? $_[0] : $q->parse($_[0]) }
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 9 83 92
116  
117 99 return $self;
118 }
119  
120 159 ▶sub to_abs {
Subroutine to_abs: called CC=7 SCAR=19.5
Pod documented
121 159 my $self = shift;
122  
123 159 my $abs = $self->clone;
124 159 ▶ return $abs if $abs->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->is_abs 71 88 159
125  
126 # Scheme
127 88 ▶ my $base = shift || $abs->base;
Condition66%2/3
shift() || $abs->base
l!l&&r!l&&!r
23650
MC/DC0%0/2
shift() || $abs->base
shift() $abs->base
Truth table66%2/3
A || B
A= shift()B= $abs->base
ABresult
1X1
011
000
128 88 $abs->base($base)->scheme($base->scheme);
129  
130 # Authority
131 88 ▶ return $abs if $abs->host;
Branch: 1 branch
BranchTrueFalseTotal
if $abs->host 6 82 88
132 82 $abs->userinfo($base->userinfo)->host($base->host)->port($base->port);
133  
134 # Absolute path
135 82 my $path = $abs->path;
136 82 ▶ return $abs if $path->leading_slash;
Branch: 1 branch
BranchTrueFalseTotal
if $path->leading_slash 36 46 82
137  
138 # Inherit path
139 46 ▶ if (!@{$path->parts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$path->parts;}) { } 9 37 46
140 9 $abs->path($base->path->clone->canonicalize);
141  
142 # Query
143 9 ▶ $abs->query($base->query->clone) unless length $abs->query->to_string;
Branch: 1 branch
BranchTrueFalseTotal
unless length $abs->query->to_string 7 2 9
144 }
145  
146 # Merge paths
147 37 else { $abs->path($base->path->clone->merge($path)->canonicalize) }
148  
149 46 return $abs;
150 }
151  
152 659 ▶sub to_string { shift->_string(0) }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
153 3 ▶sub to_unsafe_string { shift->_string(1) }
Subroutine to_unsafe_string: called CC=1 SCAR=0.0
Pod documented
154  
155 6 ▶sub username { (shift->userinfo // '') =~ /^([^:]+)/ ? $1 : undef }
Subroutine username: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
((shift())->userinfo // '') =~ /^([^:]+)/u ? : 5 1 6
Condition100%2/2
(shift())->userinfo // ""
l!l
51
MC/DC100%1/1
(shift())->userinfo // ""
(shift())->userinfo
Truth table100%2/2
A
A= (shift())->userinfo
Aresult
11
00
156  
157 52 ▶sub _decode { decode('UTF-8', $_[0]) // $_[0] }
Subroutine _decode: called CC=2 SCAR=7.2
Condition66%2/3
decode("UTF-8", $_[0]) // $_[0]
l!l&&r!l&&!r
5020
MC/DC0%0/2
decode("UTF-8", $_[0]) // $_[0]
decode("UTF-8", $_[0]) $_[0]
Truth table66%2/3
A // B
A= decode("UTF-8", $_[0])B= $_[0]
ABresult
1X1
011
000
158  
159 323 ▶sub _encode { url_escape encode('UTF-8', $_[0]), $_[1] }
Subroutine _encode: called CC=1 SCAR=0.0
160  
161 662 ▶sub _string {
Subroutine _string: called CC=10 SCAR=23.0
162 662 my ($self, $unsafe) = @_;
163  
164 # Scheme
165 662 my $url = '';
166 662 ▶ if (my $proto = $self->protocol) { $url .= "$proto:" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $proto = $self->protocol) 268 394 662
167  
168 # Authority
169 662 my $auth = $self->host_port;
170 662 ▶ $auth = _encode($auth, '^A-Za-z0-9\-._~!$&\'()*+,;=:\[\]') if defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $auth 266 396 662
171 662 ▶ if ($unsafe && defined(my $info = $self->userinfo)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsafe and defined(my $info = $self->userinfo)) 3 659 662
Condition66%2/3
$unsafe and defined(my $info = $self->userinfo)
!ll&&!rl&&r
65903
MC/DC50%1/2
$unsafe and defined(my $info = $self->userinfo)
$unsafe defined(my $info = $self->userinfo)
Truth table66%2/3
A and B
A= $unsafeB= defined(my $info = $self->userinfo)
ABresult
0X0
100
111
172 3 $auth = _encode($info, '^A-Za-z0-9\-._~!$&\'()*+,;=:') . '@' . $auth;
173 }
174 662 ▶ $url .= "//$auth" if defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $auth 266 396 662
175  
176 # Path and query
177 662 my $path = $self->path_query;
178 662 ▶ $url .= !$auth || !length $path || $path =~ m!^[/?]! ? $path : "/$path";
Branch: 1 branch
BranchTrueFalseTotal
!($auth && length $path) || $path =~ m[^[/?]]u ? : 659 3 662
Condition100%6/6
not $auth && length $path or $path =~ m[^[/?]]u
l!l&&r!l&&!r
4432163
$auth && length $path
!ll&&!rl&&r
40043219
MC/DC100%3/3
not $auth && length $path or $path =~ m[^[/?]]u
$auth length $path $path =~ m[^[/?]]u
Truth table100%4/4
not(A && B) or C
A= $authB= length $pathC= $path =~ m[^[/?]]u
ABCresult
0XX1
10X1
1111
1100
179  
180 # Fragment
181 662 ▶ return $url unless defined(my $fragment = $self->fragment);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $fragment = $self->fragment) 608 54 662
182 54 return $url . '#' . _encode($fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=:@/?');
183 }
184  
185 31 1;
186  
187 =encoding utf8
188  
189 =head1 NAME
190  
191 Mojo::URL - Uniform Resource Locator
192  
193 =head1 SYNOPSIS
194  
195 use Mojo::URL;
196  
197 # Parse
198 my $url = Mojo::URL->new('http://sri:foo@example.com:3000/foo?foo=bar#23');
199 say $url->scheme;
200 say $url->userinfo;
201 say $url->host;
202 say $url->port;
203 say $url->path;
204 say $url->query;
205 say $url->fragment;
206  
207 # Build
208 my $url = Mojo::URL->new;
209 $url->scheme('http');
210 $url->host('example.com');
211 $url->port(3000);
212 $url->path('/foo/bar');
213 $url->query(foo => 'bar');
214 $url->fragment(23);
215 say "$url";
216  
217 =head1 DESCRIPTION
218  
219 L<Mojo::URL> implements a subset of L<RFC 3986|https://tools.ietf.org/html/rfc3986>, L<RFC
220 3987|https://tools.ietf.org/html/rfc3987> and the L<URL Living Standard|https://url.spec.whatwg.org> for Uniform
221 Resource Locators with support for IDNA and IRIs.
222  
223 =head1 ATTRIBUTES
224  
225 L<Mojo::URL> implements the following attributes.
226  
227 =head2 base
228  
229 my $base = $url->base;
230 $url = $url->base(Mojo::URL->new);
231  
232 Base of this URL, defaults to a L<Mojo::URL> object.
233  
234 "http://example.com/a/b?c"
235 Mojo::URL->new("/a/b?c")->base(Mojo::URL->new("http://example.com"))->to_abs;
236  
237 =head2 fragment
238  
239 my $fragment = $url->fragment;
240 $url = $url->fragment('♥mojolicious♥');
241  
242 Fragment part of this URL.
243  
244 # "yada"
245 Mojo::URL->new('http://example.com/foo?bar=baz#yada')->fragment;
246  
247 =head2 host
248  
249 my $host = $url->host;
250 $url = $url->host('127.0.0.1');
251  
252 Host part of this URL.
253  
254 # "example.com"
255 Mojo::URL->new('http://sri:t3st@example.com:8080/foo')->host;
256  
257 =head2 port
258  
259 my $port = $url->port;
260 $url = $url->port(8080);
261  
262 Port part of this URL.
263  
264 # "8080"
265 Mojo::URL->new('http://sri:t3st@example.com:8080/foo')->port;
266  
267 =head2 scheme
268  
269 my $scheme = $url->scheme;
270 $url = $url->scheme('http');
271  
272 Scheme part of this URL.
273  
274 # "http"
275 Mojo::URL->new('http://example.com/foo')->scheme;
276  
277 =head2 userinfo
278  
279 my $info = $url->userinfo;
280 $url = $url->userinfo('root:♥');
281  
282 Userinfo part of this URL.
283  
284 # "sri:t3st"
285 Mojo::URL->new('https://sri:t3st@example.com/foo')->userinfo;
286  
287 =head1 METHODS
288  
289 L<Mojo::URL> inherits all methods from L<Mojo::Base> and implements the following new ones.
290  
291 =head2 clone
292  
293 my $url2 = $url->clone;
294  
295 Return a new L<Mojo::URL> object cloned from this URL.
296  
297 =head2 host_port
298  
299 my $host_port = $url->host_port;
300 $url = $url->host_port('example.com:8080');
301  
302 Normalized version of L</"host"> and L</"port">.
303  
304 # "xn--n3h.net:8080"
305 Mojo::URL->new('http://☃.net:8080/test')->host_port;
306  
307 # "example.com"
308 Mojo::URL->new('http://example.com/test')->host_port;
309  
310 =head2 ihost
311  
312 my $ihost = $url->ihost;
313 $url = $url->ihost('xn--bcher-kva.ch');
314  
315 Host part of this URL in punycode format.
316  
317 # "xn--n3h.net"
318 Mojo::URL->new('http://☃.net')->ihost;
319  
320 # "example.com"
321 Mojo::URL->new('http://example.com')->ihost;
322  
323 =head2 is_abs
324  
325 my $bool = $url->is_abs;
326  
327 Check if URL is absolute.
328  
329 # True
330 Mojo::URL->new('http://example.com')->is_abs;
331 Mojo::URL->new('http://example.com/test/index.html')->is_abs;
332  
333 # False
334 Mojo::URL->new('test/index.html')->is_abs;
335 Mojo::URL->new('/test/index.html')->is_abs;
336 Mojo::URL->new('//example.com/test/index.html')->is_abs;
337  
338 =head2 new
339  
340 my $url = Mojo::URL->new;
341 my $url = Mojo::URL->new('http://127.0.0.1:3000/foo?f=b&baz=2#foo');
342  
343 Construct a new L<Mojo::URL> object and L</"parse"> URL if necessary.
344  
345 =head2 parse
346  
347 $url = $url->parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo');
348  
349 Parse relative or absolute URL.
350  
351 # "/test/123"
352 $url->parse('/test/123?foo=bar')->path;
353  
354 # "example.com"
355 $url->parse('http://example.com/test/123?foo=bar')->host;
356  
357 # "sri@example.com"
358 $url->parse('mailto:sri@example.com')->path;
359  
360 =head2 password
361  
362 my $password = $url->password;
363  
364 Password part of L</"userinfo">.
365  
366 # "s3cret"
367 Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->password;
368  
369 # "s:3:c:r:e:t"
370 Mojo::URL->new('http://isabel:s:3:c:r:e:t@mojolicious.org')->password;
371  
372 =head2 path
373  
374 my $path = $url->path;
375 $url = $url->path('foo/bar');
376 $url = $url->path('/foo/bar');
377 $url = $url->path(Mojo::Path->new);
378  
379 Path part of this URL, relative paths will be merged with L<Mojo::Path/"merge">, defaults to a L<Mojo::Path> object.
380  
381 # "test"
382 Mojo::URL->new('http://example.com/test/Mojo')->path->parts->[0];
383  
384 # "/test/DOM/HTML"
385 Mojo::URL->new('http://example.com/test/Mojo')->path->merge('DOM/HTML');
386  
387 # "http://example.com/DOM/HTML"
388 Mojo::URL->new('http://example.com/test/Mojo')->path('/DOM/HTML');
389  
390 # "http://example.com/test/DOM/HTML"
391 Mojo::URL->new('http://example.com/test/Mojo')->path('DOM/HTML');
392  
393 # "http://example.com/test/Mojo/DOM/HTML"
394 Mojo::URL->new('http://example.com/test/Mojo/')->path('DOM/HTML');
395  
396 =head2 path_query
397  
398 my $path_query = $url->path_query;
399 $url = $url->path_query('/foo/bar?a=1&b=2');
400  
401 Normalized version of L</"path"> and L</"query">.
402  
403 # "/test?a=1&b=2"
404 Mojo::URL->new('http://example.com/test?a=1&b=2')->path_query;
405  
406 # "/"
407 Mojo::URL->new('http://example.com/')->path_query;
408  
409 =head2 protocol
410  
411 my $proto = $url->protocol;
412  
413 Normalized version of L</"scheme">.
414  
415 # "http"
416 Mojo::URL->new('HtTp://example.com')->protocol;
417  
418 =head2 query
419  
420 my $query = $url->query;
421 $url = $url->query({merge => 'to'});
422 $url = $url->query([append => 'with']);
423 $url = $url->query(replace => 'with');
424 $url = $url->query('a=1&b=2');
425 $url = $url->query(Mojo::Parameters->new);
426  
427 Query part of this URL, key/value pairs in an array reference will be appended with L<Mojo::Parameters/"append">, and
428 key/value pairs in a hash reference merged with L<Mojo::Parameters/"merge">, defaults to a L<Mojo::Parameters> object.
429  
430 # "2"
431 Mojo::URL->new('http://example.com?a=1&b=2')->query->param('b');
432  
433 # "a=2&b=2&c=3"
434 Mojo::URL->new('http://example.com?a=1&b=2')->query->merge(a => 2, c => 3);
435  
436 # "http://example.com?a=2&c=3"
437 Mojo::URL->new('http://example.com?a=1&b=2')->query(a => 2, c => 3);
438  
439 # "http://example.com?a=2&a=3"
440 Mojo::URL->new('http://example.com?a=1&b=2')->query(a => [2, 3]);
441  
442 # "http://example.com?a=2&b=2&c=3"
443 Mojo::URL->new('http://example.com?a=1&b=2')->query({a => 2, c => 3});
444  
445 # "http://example.com?b=2"
446 Mojo::URL->new('http://example.com?a=1&b=2')->query({a => undef});
447  
448 # "http://example.com?a=1&b=2&a=2&c=3"
449 Mojo::URL->new('http://example.com?a=1&b=2')->query([a => 2, c => 3]);
450  
451 =head2 to_abs
452  
453 my $abs = $url->to_abs;
454 my $abs = $url->to_abs(Mojo::URL->new('http://example.com/foo'));
455  
456 Return a new L<Mojo::URL> object cloned from this relative URL and turn it into an absolute one using L</"base"> or
457 provided base URL.
458  
459 # "http://example.com/foo/baz.xml?test=123"
460 Mojo::URL->new('baz.xml?test=123')
461 ->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));
462  
463 # "http://example.com/baz.xml?test=123"
464 Mojo::URL->new('/baz.xml?test=123')
465 ->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));
466  
467 # "http://example.com/foo/baz.xml?test=123"
468 Mojo::URL->new('//example.com/foo/baz.xml?test=123')
469 ->to_abs(Mojo::URL->new('http://example.com/foo/bar.html'));
470  
471 =head2 to_string
472  
473 my $str = $url->to_string;
474  
475 Turn URL into a string. Note that L</"userinfo"> will not be included for security reasons.
476  
477 # "http://mojolicious.org"
478 Mojo::URL->new->scheme('http')->host('mojolicious.org')->to_string;
479  
480 # "http://mojolicious.org"
481 Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_string;
482  
483 =head2 to_unsafe_string
484  
485 my $str = $url->to_unsafe_string;
486  
487 Same as L</"to_string">, but includes L</"userinfo">.
488  
489 # "http://daniel:s3cret@mojolicious.org"
490 Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_unsafe_string;
491  
492 =head2 username
493  
494 my $username = $url->username;
495  
496 Username part of L</"userinfo">.
497  
498 # "isabel"
499 Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->username;
500  
501 =head1 OPERATORS
502  
503 L<Mojo::URL> overloads the following operators.
504  
505 =head2 bool
506  
507 my $bool = !!$url;
508  
509 Always true.
510  
511 =head2 stringify
512  
513 my $str = "$url";
514  
515 Alias for L</"to_string">.
516  
517 =head1 SEE ALSO
518  
519 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
520  
521 =cut
« Mojo/IOLoop/Stream.pm ↑ Summary Mojolicious/Types.pm »