Skip to content

Commit

Permalink
add password and username methods to Mojo::URL
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 14, 2016
1 parent fb58d90 commit 9a7c5be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.65 2016-06-14
- Added password and username methods to Mojo::URL.
- Updated jQuery to version 3.0.0.
- Fixed "0" value bug in Mojo::UserAgent::CookieJar. (jamadam)

Expand Down
25 changes: 25 additions & 0 deletions lib/Mojo/URL.pm
Expand Up @@ -85,6 +85,8 @@ sub parse {
return $self;
}

sub password { (shift->userinfo // '') =~ /:(.*)$/ ? $1 : undef }

sub path {
my $self = shift;

Expand Down Expand Up @@ -183,6 +185,8 @@ sub to_string {
return $url . '#' . _encode($fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=%:@/?');
}

sub username { (shift->userinfo // '') =~ /^([^:]+)/ ? $1 : undef }

sub _decode { decode('UTF-8', $_[0]) // $_[0] }

sub _encode { url_escape encode('UTF-8', $_[0]), $_[1] }
Expand Down Expand Up @@ -378,6 +382,18 @@ Parse relative or absolute URL.
# "sri@example.com"
$url->parse('mailto:sri@example.com')->path;
=head2 password
my $password = $url->password;
Password part of L</"userinfo">.
# "s3cret"
Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->password;
# "s:3:c:r:e:t"
Mojo::URL->new('http://isabel:s:3:c:r:e:t@mojolicious.org')->password;
=head2 path
my $path = $url->path;
Expand Down Expand Up @@ -488,6 +504,15 @@ Turn URL into a string.
# "http://mojolicious.org"
Mojo::URL->new->scheme('http')->host('mojolicious.org')->to_string;
=head2 username
my $username = $url->username;
Username part of L</"userinfo">.
# "isabel"
Mojo::URL->new('http://isabel:s3cret@mojolicious.org')->username;
=head1 OPERATORS
L<Mojo::URL> overloads the following operators.
Expand Down
18 changes: 15 additions & 3 deletions t/mojo/url.t
Expand Up @@ -19,6 +19,8 @@ ok $url->is_abs, 'is absolute';
is $url->scheme, 'https', 'right scheme';
is $url->protocol, 'https', 'right protocol';
is $url->userinfo, 'sri:foobar', 'right userinfo';
is $url->username, 'sri', 'right username';
is $url->password, 'foobar', 'right username';
is $url->host, 'example.com', 'right host';
is $url->port, '8080', 'right port';
is $url->authority, 'sri:foobar@example.com:8080', 'right authority';
Expand All @@ -39,6 +41,8 @@ $url = Mojo::URL->new(
'ws://AZaz09-._~!$&\'()*+,;=:@localhost#AZaz09-._~!$&\'()*+,;=%:@/?');
is $url->scheme, 'ws', 'right scheme';
is $url->userinfo, 'AZaz09-._~!$&\'()*+,;=:', 'right userinfo';
is $url->username, 'AZaz09-._~!$&\'()*+,;=', 'right username';
is $url->password, '', 'right password';
is $url->host, 'localhost', 'right host';
is $url->fragment, 'AZaz09-._~!$&\'()*+,;=%:@/?', 'right fragment';
is "$url", 'ws://AZaz09-._~!$&\'()*+,;=:@localhost#AZaz09-._~!$&\'()*+,;=%:@/?',
Expand Down Expand Up @@ -87,10 +91,12 @@ is "$url", 'http://sri:foobar@example.com:8080?%E5=%E4#23', 'right format';

# Query string
$url = Mojo::URL->new(
'wss://sri:foobar@example.com:8080?_monkeybiz%3B&_monkey;23#23');
'wss://sri:foo:bar@example.com:8080?_monkeybiz%3B&_monkey;23#23');
ok $url->is_abs, 'is absolute';
is $url->scheme, 'wss', 'right scheme';
is $url->userinfo, 'sri:foobar', 'right userinfo';
is $url->userinfo, 'sri:foo:bar', 'right userinfo';
is $url->username, 'sri', 'right username';
is $url->password, 'foo:bar', 'right password';
is $url->host, 'example.com', 'right host';
is $url->port, '8080', 'right port';
is $url->path, '', 'no path';
Expand All @@ -99,12 +105,14 @@ is_deeply $url->query->pairs, ['_monkeybiz;', '', '_monkey;23', ''],
'right structure';
is $url->query, '_monkeybiz%3B=&_monkey%3B23=', 'right query';
is $url->fragment, '23', 'right fragment';
is "$url", 'wss://sri:foobar@example.com:8080?_monkeybiz%3B=&_monkey%3B23=#23',
is "$url", 'wss://sri:foo:bar@example.com:8080?_monkeybiz%3B=&_monkey%3B23=#23',
'right format';
$url = Mojo::URL->new('https://example.com/0?0#0');
ok $url->is_abs, 'is absolute';
is $url->scheme, 'https', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
is $url->username, undef, 'no username';
is $url->password, undef, 'no password';
is $url->host, 'example.com', 'right host';
is $url->port, undef, 'no port';
is $url->host_port, 'example.com', 'right host and port';
Expand Down Expand Up @@ -351,6 +359,8 @@ is "$url", 'http://xn--n3h.xn--n3h.net', 'right format';
# IDNA (escaped userinfo and host)
$url = Mojo::URL->new('https://%E2%99%A5:%E2%99%A5@kr%E4ih.com:3000');
is $url->userinfo, '♥:♥', 'right userinfo';
is $url->username, '', 'right username';
is $url->password, '', 'right password';
is $url->host, "kr\xe4ih.com", 'right host';
is $url->ihost, 'xn--krih-moa.com', 'right internationalized host';
is $url->port, 3000, 'right port';
Expand Down Expand Up @@ -406,6 +416,8 @@ is $url->to_abs, 'http://foo.com/', 'right absolute version';
$url = Mojo::URL->new('http://0@foo.com#0');
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, '0', 'right userinfo';
is $url->username, '0', 'right username';
is $url->password, undef, 'no password';
is $url->host, 'foo.com', 'right host';
is $url->fragment, '0', 'right fragment';
is "$url", 'http://0@foo.com#0', 'right format';
Expand Down

0 comments on commit 9a7c5be

Please sign in to comment.