Skip to content

Commit

Permalink
add to_unsafe_string method to Mojo::URL
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 27, 2017
1 parent 822f658 commit 132d772
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

7.23 2017-01-27
- Added to_unsafe_string method to Mojo::URL.
- Fixed a data corruption problem in Mojo::IOLoop::Stream, caused by a
dependency of IO::Socket::SSL on the internal representation of strings,
which differs from IO::Socket::IP. (coolo, sri)
Expand Down
46 changes: 31 additions & 15 deletions lib/Mojo/URL.pm
Expand Up @@ -63,9 +63,9 @@ sub parse {
$self->path($5) if defined $5;
$self->query($7) if defined $7;
$self->fragment(_decode(url_unescape $9)) if defined $9;
if (defined(my $authority = $4)) {
$self->userinfo(_decode(url_unescape $1)) if $authority =~ s/^([^\@]+)\@//;
$self->host_port($authority);
if (defined(my $auth = $4)) {
$self->userinfo(_decode(url_unescape $1)) if $auth =~ s/^([^\@]+)\@//;
$self->host_port($auth);
}

return $self;
Expand Down Expand Up @@ -148,31 +148,38 @@ sub to_abs {
return $abs;
}

sub to_string {
my $self = shift;
sub to_string { shift->_string(0) }
sub to_unsafe_string { shift->_string(1) }

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

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

sub _encode { url_escape encode('UTF-8', $_[0]), $_[1] }

sub _string {
my ($self, $unsafe) = @_;

# Scheme
my $url = '';
if (my $proto = $self->protocol) { $url .= "$proto:" }

# Authority (without userinfo)
my $authority = $self->host_port;
$url .= "//$authority" if defined $authority;
# Authority
my $auth = $self->host_port;
if ($unsafe && defined(my $info = $self->userinfo)) {
$auth = _encode($info, '^A-Za-z0-9\-._~!$&\'()*+,;=:') . '@' . $auth;
}
$url .= "//$auth" if defined $auth;

# Path and query
my $path = $self->path_query;
$url .= !$authority || !length $path || $path =~ m!^[/?]! ? $path : "/$path";
$url .= !$auth || !length $path || $path =~ m!^[/?]! ? $path : "/$path";

# Fragment
return $url unless defined(my $fragment = $self->fragment);
return $url . '#' . url_escape encode('UTF-8', $fragment),
'^A-Za-z0-9\-._~!$&\'()*+,;=:@/?';
return $url . '#' . _encode($fragment, '^A-Za-z0-9\-._~!$&\'()*+,;=:@/?');
}

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

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

1;

=encoding utf8
Expand Down Expand Up @@ -477,6 +484,15 @@ security reasons.
# "http://mojolicious.org"
Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_string;
=head2 to_unsafe_string
my $str = $url->to_unsafe_string;
Same as L</"to_string">, but includes L</"userinfo">.
# "ttp://daniel:s3cret@mojolicious.org"

This comment has been minimized.

Copy link
@jberger

jberger Jan 27, 2017

Member

typo, missing h

Mojo::URL->new('http://daniel:s3cret@mojolicious.org')->to_unsafe_string;
=head2 username
my $username = $url->username;
Expand Down
7 changes: 7 additions & 0 deletions t/mojo/url.t
Expand Up @@ -42,6 +42,9 @@ is $url->password, '', 'right password';
is $url->host, 'localhost', 'right host';
is $url->fragment, 'AZaz09-._~!$&\'()*+,;=:@/?', 'right fragment';
is "$url", 'ws://localhost#AZaz09-._~!$&\'()*+,;=:@/?', 'right format';
is $url->to_unsafe_string,
'ws://AZaz09-._~!$&\'()*+,;=:@localhost#AZaz09-._~!$&\'()*+,;=:@/?',
'right format';

# Parameters
$url = Mojo::URL->new(
Expand Down Expand Up @@ -373,6 +376,9 @@ is $url->query, '%E2%98%83', 'right query';
is $url->fragment, '', 'right fragment';
is "$url", 'http://xn--n3h.xn--n3h.de/%E2%98%83?%E2%98%83#%E2%98%83',
'right format';
is $url->to_unsafe_string,
'http://%E2%98%83:%E2%98%83@xn--n3h.xn--n3h.de/%E2%98%83?%E2%98%83#%E2%98%83',
'right format';

# IRI/IDNA
$url = Mojo::URL->new('http://☃.net/♥/?q=♥☃');
Expand Down Expand Up @@ -415,6 +421,7 @@ is $url->password, undef, 'no password';
is $url->host, 'foo.com', 'right host';
is $url->fragment, '0', 'right fragment';
is "$url", 'http://foo.com#0', 'right format';
is $url->to_unsafe_string, 'http://0@foo.com#0', 'right format';

# Empty path elements
$url = Mojo::URL->new('http://example.com/foo//bar/23/');
Expand Down

0 comments on commit 132d772

Please sign in to comment.