Skip to content

Commit

Permalink
the authority method is no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 6, 2016
1 parent 70c09a5 commit 80af325
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 102 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
7.0 2016-07-06
- Code name "Thinking Face", this is a major release.
- Removed Mojolicious::Plugin::Charset.
- Removed authority method from Mojo::URL.
- Removed squish method from Mojo::ByteStream.
- Removed squish function from Mojo::Util.
- Removed support for smart whitespace trimming from all_text and text methods
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -55,7 +55,7 @@ sub extract_start_line {
return !$self->error({message => 'Bad request start-line'})
unless $1 =~ /^(\S+)\s+(\S+)\s+HTTP\/(\d\.\d)$/;
my $url = $self->method($1)->version($3)->url;
return !!($1 eq 'CONNECT' ? $url->authority($2) : $url->parse($2));
return !!($1 eq 'CONNECT' ? $url->host_port($2) : $url->parse($2));
}

sub fix_headers {
Expand Down Expand Up @@ -125,7 +125,7 @@ sub parse {
my $base = $self->url->base;
$base->scheme('http') unless $base->scheme;
my $headers = $self->headers;
if (!$base->host && (my $host = $headers->host)) { $base->authority($host) }
if (!$base->host && (my $host = $headers->host)) { $base->host_port($host) }

# Basic authentication
if (my $basic = _basic($headers->authorization)) { $base->userinfo($basic) }
Expand Down
61 changes: 17 additions & 44 deletions lib/Mojo/URL.pm
Expand Up @@ -10,30 +10,6 @@ use Mojo::Util
has base => sub { Mojo::URL->new };
has [qw(fragment host port scheme userinfo)];

sub authority {
my $self = shift;

# New authority
if (@_) {
return $self unless defined(my $authority = shift);

# Userinfo
$self->userinfo(_decode(url_unescape $1)) if $authority =~ s/^([^\@]+)\@//;

# Port
$self->port($1) if $authority =~ s/:(\d+)$//;

# Host
my $host = url_unescape $authority;
return $host =~ /[^\x00-\x7f]/ ? $self->ihost($host) : $self->host($host);
}

# Build authority
return undef unless defined(my $authority = $self->host_port);
return $authority unless defined(my $info = $self->userinfo);
return _encode($info, '^A-Za-z0-9\-._~!$&\'()*+,;=:') . '@' . $authority;
}

sub clone {
my $self = shift;
my $clone = $self->new;
Expand All @@ -43,7 +19,14 @@ sub clone {
}

sub host_port {
my $self = shift;
my ($self, $host_port) = @_;

if (defined $host_port) {
$self->port($1) if $host_port =~ s/:(\d+)$//;
my $host = url_unescape $host_port;
return $host =~ /[^\x00-\x7f]/ ? $self->ihost($host) : $self->host($host);
}

return undef unless defined(my $host = $self->ihost);
return $host unless my $port = $self->port;
return "$host:$port";
Expand Down Expand Up @@ -77,10 +60,13 @@ sub parse {
# Official regex from RFC 3986
$url =~ m!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!;
$self->scheme($2) if defined $2;
$self->authority($4) if defined $4;
$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);
}

return $self;
}
Expand Down Expand Up @@ -141,8 +127,8 @@ sub to_abs {
$abs->base($base)->scheme($base->scheme);

# Authority
return $abs if $abs->authority;
$abs->authority($base->authority);
return $abs if $abs->host;
$abs->userinfo($base->userinfo)->host($base->host)->port($base->port);

# Absolute path
my $path = $abs->path;
Expand Down Expand Up @@ -179,15 +165,14 @@ sub to_string {

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

1;

=encoding utf8
Expand Down Expand Up @@ -297,19 +282,6 @@ Userinfo part of this URL.
L<Mojo::URL> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 authority
my $authority = $url->authority;
$url = $url->authority('root:%E2%99%A5@localhost:8080');
Authority part of this URL.
# "root:%E2%99%A5@xn--n3h.net:8080"
Mojo::URL->new('http://root:♥@☃.net:8080/test')->authority;
# "root@example.com"
Mojo::URL->new('http://root@example.com/test')->authority;
=head2 clone
my $url2 = $url->clone;
Expand All @@ -319,6 +291,7 @@ Clone this URL.
=head2 host_port
my $host_port = $url->host_port;
$url = $url->host_port('example.com:8080');
Normalized version of L</"host"> and L</"port">.
Expand Down
102 changes: 46 additions & 56 deletions t/mojo/url.t
Expand Up @@ -5,11 +5,10 @@ use Mojo::URL;

# Simple
my $url = Mojo::URL->new('HtTp://Example.Com');
is $url->scheme, 'HtTp', 'right scheme';
is $url->protocol, 'http', 'right protocol';
is $url->host, 'Example.Com', 'right host';
is $url->ihost, 'example.com', 'right internationalized host';
is $url->authority, 'example.com', 'right authority';
is $url->scheme, 'HtTp', 'right scheme';
is $url->protocol, 'http', 'right protocol';
is $url->host, 'Example.Com', 'right host';
is $url->ihost, 'example.com', 'right internationalized host';
is "$url", 'http://example.com', 'right format';

# Advanced
Expand All @@ -23,7 +22,6 @@ is $url->username, 'sri', 'right username';
is $url->password, 'foobar', 'right password';
is $url->host, 'example.com', 'right host';
is $url->port, '8080', 'right port';
is $url->authority, 'sri:foobar@example.com:8080', 'right authority';
is $url->path, '/x/index.html', 'right path';
is $url->query, 'monkey=biz&foo=1', 'right query';
is $url->path_query, '/x/index.html?monkey=biz&foo=1', 'right path and query';
Expand All @@ -37,13 +35,12 @@ is "$url", 'https://example.com:8080/index.xml?monkey=biz&foo=1#/!%?@3',
# Advanced userinfo and fragment roundtrip
$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->authority, 'AZaz09-._~!$&\'()*+,;=:@localhost', 'right authority';
is $url->fragment, 'AZaz09-._~!$&\'()*+,;=%:@/?', 'right fragment';
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://localhost#AZaz09-._~!$&\'()*+,;=%:@/?', 'right format';

# Parameters
Expand Down Expand Up @@ -119,26 +116,24 @@ is "$url", 'https://example.com/0?0#0', 'right format';

# No authority
$url = Mojo::URL->new('DATA:image/png;base64,helloworld123');
is $url->scheme, 'DATA', 'right scheme';
is $url->protocol, 'data', 'right protocol';
is $url->userinfo, undef, 'no userinfo';
is $url->host, undef, 'no host';
is $url->port, undef, 'no port';
is $url->authority, undef, 'no authority';
is $url->path, 'image/png;base64,helloworld123', 'right path';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is $url->scheme, 'DATA', 'right scheme';
is $url->protocol, 'data', 'right protocol';
is $url->userinfo, undef, 'no userinfo';
is $url->host, undef, 'no host';
is $url->port, undef, 'no port';
is $url->path, 'image/png;base64,helloworld123', 'right path';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is "$url", 'data:image/png;base64,helloworld123', 'right format';
$url = $url->clone;
is $url->scheme, 'DATA', 'right scheme';
is $url->protocol, 'data', 'right protocol';
is $url->userinfo, undef, 'no userinfo';
is $url->host, undef, 'no host';
is $url->port, undef, 'no port';
is $url->authority, undef, 'no authority';
is $url->path, 'image/png;base64,helloworld123', 'right path';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is $url->scheme, 'DATA', 'right scheme';
is $url->protocol, 'data', 'right protocol';
is $url->userinfo, undef, 'no userinfo';
is $url->host, undef, 'no host';
is $url->port, undef, 'no port';
is $url->path, 'image/png;base64,helloworld123', 'right path';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is "$url", 'data:image/png;base64,helloworld123', 'right format';
$url = Mojo::URL->new->parse('mailto:sri@example.com');
is $url->scheme, 'mailto', 'right scheme';
Expand All @@ -153,13 +148,12 @@ is $url->query, 'foo=bar', 'right query';
is $url->fragment, 'baz', 'right fragment';
is "$url", 'foo:/test/123?foo=bar#baz', 'right format';
is $url->scheme('Bar')->to_string, 'bar:/test/123?foo=bar#baz', 'right format';
is $url->scheme, 'Bar', 'right scheme';
is $url->protocol, 'bar', 'right protocol';
is $url->host, undef, 'no host';
is $url->authority, undef, 'no authority';
is $url->path, '/test/123', 'right path';
is $url->query, 'foo=bar', 'right query';
is $url->fragment, 'baz', 'right fragment';
is $url->scheme, 'Bar', 'right scheme';
is $url->protocol, 'bar', 'right protocol';
is $url->host, undef, 'no host';
is $url->path, '/test/123', 'right path';
is $url->query, 'foo=bar', 'right query';
is $url->fragment, 'baz', 'right fragment';
is "$url", 'bar:/test/123?foo=bar#baz', 'right format';
$url = Mojo::URL->new->parse('file:///foo/bar');
is $url->scheme, 'file', 'right scheme';
Expand Down Expand Up @@ -365,21 +359,18 @@ 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';
is $url->authority, '%E2%99%A5:%E2%99%A5@xn--krih-moa.com:3000',
'right authority';
is "$url", 'https://xn--krih-moa.com:3000', 'right format';

# IDNA (snowman)
$url = Mojo::URL->new('http://☃:☃@☃.☃.de/☃?☃#☃');
ok $url->is_abs, 'is absolute';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, '☃:☃', 'right userinfo';
is $url->host, '☃.☃.de', 'right host';
is $url->ihost, 'xn--n3h.xn--n3h.de', 'right internationalized host';
is $url->authority, '%E2%98%83:%E2%98%83@xn--n3h.xn--n3h.de', 'right authority';
is $url->path, '/%E2%98%83', 'right path';
is $url->query, '%E2%98%83', 'right query';
is $url->fragment, '', 'right fragment';
ok $url->is_abs, 'is absolute';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, '☃:☃', 'right userinfo';
is $url->host, '☃.☃.de', 'right host';
is $url->ihost, 'xn--n3h.xn--n3h.de', 'right internationalized host';
is $url->path, '/%E2%98%83', 'right path';
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';

Expand Down Expand Up @@ -417,13 +408,12 @@ is $url->to_abs, 'http://foo.com/', 'right absolute version';

# "0"
$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->authority, '0@foo.com', 'right authority';
is $url->fragment, '0', 'right fragment';
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://foo.com#0', 'right format';

# Empty path elements
Expand Down

0 comments on commit 80af325

Please sign in to comment.