copy gitweblite soruce code
|
1 |
package Mojo::URL; |
2 |
use Mojo::Base -base; |
|
update Mojolicious to 4.57
|
3 |
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1; |
copy gitweblite soruce code
|
4 | |
5 |
use Mojo::Parameters; |
|
6 |
use Mojo::Path; |
|
upgraded Mojolicious to v3.7...
|
7 |
use Mojo::Util qw(punycode_decode punycode_encode url_escape url_unescape); |
copy gitweblite soruce code
|
8 | |
9 |
has base => sub { Mojo::URL->new }; |
|
upgraded Mojolicious to v3.7...
|
10 |
has [qw(fragment host port scheme userinfo)]; |
copy gitweblite soruce code
|
11 | |
12 |
sub new { shift->SUPER::new->parse(@_) } |
|
13 | ||
14 |
sub authority { |
|
update Mojolicious 4.07
|
15 |
my $self = shift; |
copy gitweblite soruce code
|
16 | |
17 |
# New authority |
|
update Mojolicious 4.07
|
18 |
if (@_) { |
19 |
return $self unless defined(my $authority = shift); |
|
copy gitweblite soruce code
|
20 | |
21 |
# Userinfo |
|
upgraded Mojolicious to v3.7...
|
22 |
$authority =~ s/^([^\@]+)\@// and $self->userinfo(url_unescape $1); |
copy gitweblite soruce code
|
23 | |
24 |
# Port |
|
upgraded Mojolicious to v3.7...
|
25 |
$authority =~ s/:(\d+)$// and $self->port($1); |
copy gitweblite soruce code
|
26 | |
27 |
# Host |
|
upgraded Mojolicious to v3.7...
|
28 |
my $host = url_unescape $authority; |
copy gitweblite soruce code
|
29 |
return $host =~ /[^\x00-\x7f]/ ? $self->ihost($host) : $self->host($host); |
30 |
} |
|
31 | ||
update Mojolicious and added...
|
32 |
# Build authority |
update Mojolicious 4.07
|
33 |
return undef unless defined(my $authority = $self->ihost); |
34 |
if (my $userinfo = $self->userinfo) { |
|
35 |
$userinfo = url_escape $userinfo, '^A-Za-z0-9\-._~!$&\'()*+,;=:'; |
|
36 |
$authority = $userinfo . '@' . $authority; |
|
37 |
} |
|
upgraded Mojolicious to v3.7...
|
38 |
if (my $port = $self->port) { $authority .= ":$port" } |
copy gitweblite soruce code
|
39 | |
40 |
return $authority; |
|
41 |
} |
|
42 | ||
43 |
sub clone { |
|
44 |
my $self = shift; |
|
45 | ||
update Mojolicious 4.07
|
46 |
my $clone = $self->new; |
47 |
$clone->$_($self->$_) for qw(scheme userinfo host port fragment); |
|
copy gitweblite soruce code
|
48 |
$clone->path($self->path->clone); |
49 |
$clone->query($self->query->clone); |
|
50 |
$clone->base($self->base->clone) if $self->{base}; |
|
51 | ||
52 |
return $clone; |
|
53 |
} |
|
54 | ||
55 |
sub ihost { |
|
56 |
my $self = shift; |
|
57 | ||
58 |
# Decode |
|
59 |
return $self->host(join '.', |
|
60 |
map { /^xn--(.+)$/ ? punycode_decode($_) : $_ } split /\./, shift) |
|
61 |
if @_; |
|
62 | ||
63 |
# Check if host needs to be encoded |
|
update Mojolicious 4.07
|
64 |
return undef unless defined(my $host = $self->host); |
upgraded Mojolicious to v3.7...
|
65 |
return lc $host unless $host =~ /[^\x00-\x7f]/; |
copy gitweblite soruce code
|
66 | |
67 |
# Encode |
|
update Mojolicious 4.07
|
68 |
return lc join '.', |
copy gitweblite soruce code
|
69 |
map { /[^\x00-\x7f]/ ? ('xn--' . punycode_encode $_) : $_ } split /\./, |
70 |
$host; |
|
71 |
} |
|
72 | ||
upgraded Mojolicious to v3.7...
|
73 |
sub is_abs { !!shift->scheme } |
copy gitweblite soruce code
|
74 | |
75 |
sub parse { |
|
76 |
my ($self, $url) = @_; |
|
77 |
return $self unless $url; |
|
78 | ||
update Mojolicious 4.07
|
79 |
# Official regex from RFC 3986 |
80 |
$url =~ m!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!; |
|
81 |
return $self->scheme($2)->authority($4)->path($5)->query($7)->fragment($9); |
|
copy gitweblite soruce code
|
82 |
} |
83 | ||
84 |
sub path { |
|
update Mojolicious 4.07
|
85 |
my $self = shift; |
copy gitweblite soruce code
|
86 | |
87 |
# Old path |
|
upgraded Mojolicious to v3.7...
|
88 |
$self->{path} ||= Mojo::Path->new; |
update Mojolicious 4.07
|
89 |
return $self->{path} unless @_; |
copy gitweblite soruce code
|
90 | |
91 |
# New path |
|
update Mojolicious 4.07
|
92 |
my $path = shift; |
upgraded Mojolicious to v3.7...
|
93 |
$self->{path} = ref $path ? $path : $self->{path}->merge($path); |
copy gitweblite soruce code
|
94 | |
95 |
return $self; |
|
96 |
} |
|
97 | ||
upgraded Mojolicious to v3.7...
|
98 |
sub protocol { lc(do { my $tmp = shift->scheme; defined $tmp ? $tmp : ''} ) } |
99 | ||
copy gitweblite soruce code
|
100 |
sub query { |
101 |
my $self = shift; |
|
102 | ||
103 |
# Old parameters |
|
upgraded Mojolicious to v3.7...
|
104 |
my $q = $self->{query} ||= Mojo::Parameters->new; |
105 |
return $q unless @_; |
|
copy gitweblite soruce code
|
106 | |
107 |
# Replace with list |
|
upgraded Mojolicious to v3.7...
|
108 |
if (@_ > 1) { $q->params([])->parse(@_) } |
copy gitweblite soruce code
|
109 | |
110 |
# Merge with array |
|
111 |
elsif (ref $_[0] eq 'ARRAY') { |
|
112 |
while (my $name = shift @{$_[0]}) { |
|
113 |
my $value = shift @{$_[0]}; |
|
114 |
defined $value ? $q->param($name => $value) : $q->remove($name); |
|
115 |
} |
|
116 |
} |
|
117 | ||
118 |
# Append hash |
|
upgraded Mojolicious to v3.7...
|
119 |
elsif (ref $_[0] eq 'HASH') { $q->append(%{$_[0]}) } |
copy gitweblite soruce code
|
120 | |
121 |
# Replace with string |
|
upgraded Mojolicious to v3.7...
|
122 |
else { $q->parse($_[0]) } |
copy gitweblite soruce code
|
123 | |
124 |
return $self; |
|
125 |
} |
|
126 | ||
127 |
sub to_abs { |
|
128 |
my $self = shift; |
|
129 | ||
130 |
my $abs = $self->clone; |
|
131 |
return $abs if $abs->is_abs; |
|
upgraded Mojolicious to v3.7...
|
132 | |
133 |
# Scheme |
|
134 |
my $base = shift || $abs->base; |
|
135 |
$abs->base($base)->scheme($base->scheme); |
|
copy gitweblite soruce code
|
136 | |
137 |
# Authority |
|
138 |
return $abs if $abs->authority; |
|
139 |
$abs->authority($base->authority); |
|
140 | ||
141 |
# Absolute path |
|
142 |
my $path = $abs->path; |
|
143 |
return $abs if $path->leading_slash; |
|
144 | ||
145 |
# Inherit path |
|
146 |
my $base_path = $base->path; |
|
147 |
if (!@{$path->parts}) { |
|
148 |
$path |
|
149 |
= $abs->path($base_path->clone)->path->trailing_slash(0)->canonicalize; |
|
150 | ||
151 |
# Query |
|
152 |
return $abs if length $abs->query->to_string; |
|
153 |
$abs->query($base->query->clone); |
|
154 |
} |
|
155 | ||
156 |
# Merge paths |
|
upgraded Mojolicious to v3.7...
|
157 |
else { $abs->path($base_path->clone->merge($path)->canonicalize) } |
copy gitweblite soruce code
|
158 | |
159 |
return $abs; |
|
160 |
} |
|
161 | ||
162 |
sub to_rel { |
|
163 |
my $self = shift; |
|
upgraded Mojolicious to v3.7...
|
164 | |
165 |
my $rel = $self->clone; |
|
166 |
return $rel unless $rel->is_abs; |
|
copy gitweblite soruce code
|
167 | |
168 |
# Scheme and authority |
|
upgraded Mojolicious to v3.7...
|
169 |
my $base = shift || $rel->base; |
170 |
$rel->base($base)->scheme(undef); |
|
copy gitweblite soruce code
|
171 |
$rel->userinfo(undef)->host(undef)->port(undef) if $base->authority; |
172 | ||
173 |
# Path |
|
174 |
my @parts = @{$rel->path->parts}; |
|
175 |
my $base_path = $base->path; |
|
176 |
my @base_parts = @{$base_path->parts}; |
|
177 |
pop @base_parts unless $base_path->trailing_slash; |
|
178 |
while (@parts && @base_parts && $parts[0] eq $base_parts[0]) { |
|
update Mojolicious to 4.57
|
179 |
shift @$_ for \@parts, \@base_parts; |
copy gitweblite soruce code
|
180 |
} |
181 |
my $path = $rel->path(Mojo::Path->new)->path; |
|
182 |
$path->leading_slash(1) if $rel->authority; |
|
183 |
$path->parts([('..') x @base_parts, @parts]); |
|
184 |
$path->trailing_slash(1) if $self->path->trailing_slash; |
|
185 | ||
186 |
return $rel; |
|
187 |
} |
|
188 | ||
189 |
sub to_string { |
|
190 |
my $self = shift; |
|
191 | ||
update Mojolicious 4.07
|
192 |
# Scheme |
copy gitweblite soruce code
|
193 |
my $url = ''; |
update Mojolicious 4.07
|
194 |
if (my $proto = $self->protocol) { $url .= "$proto:" } |
copy gitweblite soruce code
|
195 | |
196 |
# Authority |
|
197 |
my $authority = $self->authority; |
|
update Mojolicious 4.07
|
198 |
$url .= "//$authority" if defined $authority; |
upgraded Mojolicious to v3.7...
|
199 | |
update Mojolicious 4.07
|
200 |
# Path |
201 |
my $path = $self->path->to_string; |
|
202 |
$url .= !$authority || $path eq '' || $path =~ m!^/! ? $path : "/$path"; |
|
copy gitweblite soruce code
|
203 | |
204 |
# Query |
|
update Mojolicious 4.07
|
205 |
if (length(my $query = $self->query->to_string)) { $url .= "?$query" } |
copy gitweblite soruce code
|
206 | |
207 |
# Fragment |
|
update Mojolicious 4.07
|
208 |
return $url unless defined(my $fragment = $self->fragment); |
209 |
return $url . '#' . url_escape $fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?'; |
|
copy gitweblite soruce code
|
210 |
} |
211 | ||
212 |
1; |
|
213 | ||
214 |
=encoding utf8 |
|
215 | ||
216 |
=head1 NAME |
|
217 | ||
218 |
Mojo::URL - Uniform Resource Locator |
|
219 | ||
220 |
=head1 SYNOPSIS |
|
221 | ||
222 |
use Mojo::URL; |
|
223 | ||
224 |
# Parse |
|
upgraded Mojolicious to v3.7...
|
225 |
my $url |
update Mojolicious 4.07
|
226 |
= Mojo::URL->new('http://sri:foobar@example.com:3000/foo/bar?foo=bar#23'); |
copy gitweblite soruce code
|
227 |
say $url->scheme; |
228 |
say $url->userinfo; |
|
229 |
say $url->host; |
|
230 |
say $url->port; |
|
231 |
say $url->path; |
|
232 |
say $url->query; |
|
233 |
say $url->fragment; |
|
234 | ||
235 |
# Build |
|
236 |
my $url = Mojo::URL->new; |
|
237 |
$url->scheme('http'); |
|
238 |
$url->userinfo('sri:foobar'); |
|
update Mojolicious 4.07
|
239 |
$url->host('example.com'); |
copy gitweblite soruce code
|
240 |
$url->port(3000); |
241 |
$url->path('/foo/bar'); |
|
242 |
$url->query->param(foo => 'bar'); |
|
243 |
$url->fragment(23); |
|
upgraded Mojolicious to v3.7...
|
244 |
say "$url"; |
copy gitweblite soruce code
|
245 | |
246 |
=head1 DESCRIPTION |
|
247 | ||
248 |
L<Mojo::URL> implements a subset of RFC 3986 and RFC 3987 for Uniform |
|
249 |
Resource Locators with support for IDNA and IRIs. |
|
250 | ||
251 |
=head1 ATTRIBUTES |
|
252 | ||
253 |
L<Mojo::URL> implements the following attributes. |
|
254 | ||
update Mojolicious and added...
|
255 |
=head2 base |
copy gitweblite soruce code
|
256 | |
257 |
my $base = $url->base; |
|
258 |
$url = $url->base(Mojo::URL->new); |
|
259 | ||
260 |
Base of this URL. |
|
261 | ||
update Mojolicious and added...
|
262 |
=head2 fragment |
copy gitweblite soruce code
|
263 | |
264 |
my $fragment = $url->fragment; |
|
265 |
$url = $url->fragment('foo'); |
|
266 | ||
267 |
Fragment part of this URL. |
|
268 | ||
update Mojolicious and added...
|
269 |
=head2 host |
copy gitweblite soruce code
|
270 | |
271 |
my $host = $url->host; |
|
272 |
$url = $url->host('127.0.0.1'); |
|
273 | ||
274 |
Host part of this URL. |
|
275 | ||
update Mojolicious and added...
|
276 |
=head2 port |
copy gitweblite soruce code
|
277 | |
278 |
my $port = $url->port; |
|
279 |
$url = $url->port(8080); |
|
280 | ||
281 |
Port part of this URL. |
|
282 | ||
update Mojolicious and added...
|
283 |
=head2 scheme |
copy gitweblite soruce code
|
284 | |
285 |
my $scheme = $url->scheme; |
|
286 |
$url = $url->scheme('http'); |
|
287 | ||
288 |
Scheme part of this URL. |
|
289 | ||
update Mojolicious and added...
|
290 |
=head2 userinfo |
copy gitweblite soruce code
|
291 | |
292 |
my $userinfo = $url->userinfo; |
|
293 |
$url = $url->userinfo('root:pass%3Bw0rd'); |
|
294 | ||
295 |
Userinfo part of this URL. |
|
296 | ||
297 |
=head1 METHODS |
|
298 | ||
299 |
L<Mojo::URL> inherits all methods from L<Mojo::Base> and implements the |
|
300 |
following new ones. |
|
301 | ||
update Mojolicious and added...
|
302 |
=head2 new |
copy gitweblite soruce code
|
303 | |
304 |
my $url = Mojo::URL->new; |
|
305 |
my $url = Mojo::URL->new('http://127.0.0.1:3000/foo?f=b&baz=2#foo'); |
|
306 | ||
update Mojolicious to 4.57
|
307 |
Construct a new L<Mojo::URL> object and L</"parse"> URL if necessary. |
copy gitweblite soruce code
|
308 | |
update Mojolicious and added...
|
309 |
=head2 authority |
upgraded Mojolicious to v3.7...
|
310 | |
311 |
my $authority = $url->authority; |
|
312 |
$url = $url->authority('root:pass%3Bw0rd@localhost:8080'); |
|
313 | ||
314 |
Authority part of this URL. |
|
315 | ||
update Mojolicious and added...
|
316 |
=head2 clone |
copy gitweblite soruce code
|
317 | |
318 |
my $url2 = $url->clone; |
|
319 | ||
320 |
Clone this URL. |
|
321 | ||
update Mojolicious and added...
|
322 |
=head2 ihost |
copy gitweblite soruce code
|
323 | |
324 |
my $ihost = $url->ihost; |
|
325 |
$url = $url->ihost('xn--bcher-kva.ch'); |
|
326 | ||
327 |
Host part of this URL in punycode format. |
|
328 | ||
329 |
# "xn--da5b0n.net" |
|
330 |
Mojo::URL->new('http://☃.net')->ihost; |
|
331 | ||
update Mojolicious and added...
|
332 |
=head2 is_abs |
copy gitweblite soruce code
|
333 | |
update Mojolicious to 4.57
|
334 |
my $bool = $url->is_abs; |
copy gitweblite soruce code
|
335 | |
336 |
Check if URL is absolute. |
|
337 | ||
update Mojolicious and added...
|
338 |
=head2 parse |
copy gitweblite soruce code
|
339 | |
340 |
$url = $url->parse('http://127.0.0.1:3000/foo/bar?fo=o&baz=23#foo'); |
|
341 | ||
update Mojolicious 4.07
|
342 |
Parse relative or absolute URL. |
343 | ||
344 |
# "/test/123" |
|
345 |
$url->parse('/test/123?foo=bar')->path; |
|
346 | ||
347 |
# "example.com" |
|
348 |
$url->parse('http://example.com/test/123?foo=bar')->host; |
|
349 | ||
350 |
# "sri@example.com" |
|
351 |
$url->parse('mailto:sri@example.com')->path; |
|
copy gitweblite soruce code
|
352 | |
update Mojolicious and added...
|
353 |
=head2 path |
copy gitweblite soruce code
|
354 | |
355 |
my $path = $url->path; |
|
356 |
$url = $url->path('/foo/bar'); |
|
357 |
$url = $url->path('foo/bar'); |
|
358 |
$url = $url->path(Mojo::Path->new); |
|
359 | ||
update Mojolicious 4.07
|
360 |
Path part of this URL, relative paths will be merged with the existing path, |
copy gitweblite soruce code
|
361 |
defaults to a L<Mojo::Path> object. |
362 | ||
update Mojolicious 4.07
|
363 |
# "http://example.com/DOM/HTML" |
364 |
Mojo::URL->new('http://example.com/perldoc/Mojo')->path('/DOM/HTML'); |
|
upgraded Mojolicious to v3.7...
|
365 | |
update Mojolicious 4.07
|
366 |
# "http://example.com/perldoc/DOM/HTML" |
367 |
Mojo::URL->new('http://example.com/perldoc/Mojo')->path('DOM/HTML'); |
|
upgraded Mojolicious to v3.7...
|
368 | |
update Mojolicious 4.07
|
369 |
# "http://example.com/perldoc/Mojo/DOM/HTML" |
370 |
Mojo::URL->new('http://example.com/perldoc/Mojo/')->path('DOM/HTML'); |
|
copy gitweblite soruce code
|
371 | |
update Mojolicious and added...
|
372 |
=head2 protocol |
upgraded Mojolicious to v3.7...
|
373 | |
374 |
my $proto = $url->protocol; |
|
375 | ||
update Mojolicious to 4.57
|
376 |
Normalized version of L</"scheme">. |
upgraded Mojolicious to v3.7...
|
377 | |
378 |
# "http" |
|
update Mojolicious 4.07
|
379 |
Mojo::URL->new('HtTp://example.com')->protocol; |
copy gitweblite soruce code
|
380 | |
update Mojolicious and added...
|
381 |
=head2 query |
copy gitweblite soruce code
|
382 | |
383 |
my $query = $url->query; |
|
384 |
$url = $url->query(replace => 'with'); |
|
385 |
$url = $url->query([merge => 'with']); |
|
386 |
$url = $url->query({append => 'to'}); |
|
387 |
$url = $url->query(Mojo::Parameters->new); |
|
388 | ||
update Mojolicious 4.07
|
389 |
Query part of this URL, pairs in an array will be merged and pairs in a hash |
390 |
appended, defaults to a L<Mojo::Parameters> object. |
|
copy gitweblite soruce code
|
391 | |
392 |
# "2" |
|
update Mojolicious 4.07
|
393 |
Mojo::URL->new('http://example.com?a=1&b=2')->query->param('b'); |
copy gitweblite soruce code
|
394 | |
update Mojolicious 4.07
|
395 |
# "http://example.com?a=2&c=3" |
396 |
Mojo::URL->new('http://example.com?a=1&b=2')->query(a => 2, c => 3); |
|
copy gitweblite soruce code
|
397 | |
update Mojolicious 4.07
|
398 |
# "http://example.com?a=2&a=3" |
399 |
Mojo::URL->new('http://example.com?a=1&b=2')->query(a => [2, 3]); |
|
upgraded Mojolicious to v3.7...
|
400 | |
update Mojolicious 4.07
|
401 |
# "http://example.com?a=2&b=2&c=3" |
402 |
Mojo::URL->new('http://example.com?a=1&b=2')->query([a => 2, c => 3]); |
|
copy gitweblite soruce code
|
403 | |
update Mojolicious 4.07
|
404 |
# "http://example.com?b=2" |
405 |
Mojo::URL->new('http://example.com?a=1&b=2')->query([a => undef]); |
|
copy gitweblite soruce code
|
406 | |
update Mojolicious 4.07
|
407 |
# "http://example.com?a=1&b=2&a=2&c=3" |
408 |
Mojo::URL->new('http://example.com?a=1&b=2')->query({a => 2, c => 3}); |
|
copy gitweblite soruce code
|
409 | |
update Mojolicious and added...
|
410 |
=head2 to_abs |
copy gitweblite soruce code
|
411 | |
412 |
my $abs = $url->to_abs; |
|
update Mojolicious 4.07
|
413 |
my $abs = $url->to_abs(Mojo::URL->new('http://example.com/foo')); |
copy gitweblite soruce code
|
414 | |
update Mojolicious to 4.57
|
415 |
Clone relative URL and turn it into an absolute one using L</"base"> or |
416 |
provided base URL. |
|
417 | ||
418 |
# "http://example.com/foo/baz.xml?test=123" |
|
419 |
Mojo::URL->new('baz.xml?test=123') |
|
420 |
->to_abs(Mojo::URL->new('http://example.com/foo/bar.html')); |
|
421 | ||
422 |
# "http://example.com/baz.xml?test=123" |
|
423 |
Mojo::URL->new('/baz.xml?test=123') |
|
424 |
->to_abs(Mojo::URL->new('http://example.com/foo/bar.html')); |
|
425 | ||
426 |
# "http://example.com/foo/baz.xml?test=123" |
|
427 |
Mojo::URL->new('//example.com/foo/baz.xml?test=123') |
|
428 |
->to_abs(Mojo::URL->new('http://example.com/foo/bar.html')); |
|
copy gitweblite soruce code
|
429 | |
update Mojolicious and added...
|
430 |
=head2 to_rel |
copy gitweblite soruce code
|
431 | |
432 |
my $rel = $url->to_rel; |
|
update Mojolicious 4.07
|
433 |
my $rel = $url->to_rel(Mojo::URL->new('http://example.com/foo')); |
copy gitweblite soruce code
|
434 | |
update Mojolicious to 4.57
|
435 |
Clone absolute URL and turn it into a relative one using L</"base"> or |
436 |
provided base URL. |
|
437 | ||
438 |
# "foo/bar.html?test=123" |
|
439 |
Mojo::URL->new('http://example.com/foo/bar.html?test=123') |
|
440 |
->to_rel(Mojo::URL->new('http://example.com')); |
|
441 | ||
442 |
# "bar.html?test=123" |
|
443 |
Mojo::URL->new('http://example.com/foo/bar.html?test=123') |
|
444 |
->to_rel(Mojo::URL->new('http://example.com/foo/')); |
|
445 | ||
446 |
# "//example.com/foo/bar.html?test=123" |
|
447 |
Mojo::URL->new('http://example.com/foo/bar.html?test=123') |
|
448 |
->to_rel(Mojo::URL->new('http://')); |
|
copy gitweblite soruce code
|
449 | |
update Mojolicious and added...
|
450 |
=head2 to_string |
copy gitweblite soruce code
|
451 | |
update Mojolicious 4.07
|
452 |
my $str = $url->to_string; |
453 |
my $str = "$url"; |
|
copy gitweblite soruce code
|
454 | |
455 |
Turn URL into a string. |
|
456 | ||
457 |
=head1 SEE ALSO |
|
458 | ||
459 |
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>. |
|
460 | ||
461 |
=cut |