Skip to content

Commit

Permalink
added host_port method to Mojo::URL
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2014
1 parent b3ddfb5 commit 544807f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

4.85 2014-02-26
- Added next_tick method to Mojo::IOLoop and Mojo::Reactor.
- Added host_port method to Mojo::URL.
- Improved Mojo::Reactor::EV responsiveness.

4.84 2014-02-22
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -85,9 +85,7 @@ sub fix_headers {
}

# Host
my $host = $url->ihost;
my $port = $url->port;
$headers->host($port ? "$host:$port" : $host) unless $headers->host;
$headers->host($url->host_port) unless $headers->host;

return $self;
}
Expand Down
29 changes: 20 additions & 9 deletions lib/Mojo/URL.pm
Expand Up @@ -29,14 +29,16 @@ sub authority {
}

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

return $authority;
sub host_port {
my $self = shift;
return undef unless defined(my $host = $self->ihost);
return $host unless my $port = $self->port;
return "$host:$port";
}

sub clone {
Expand Down Expand Up @@ -292,8 +294,8 @@ Scheme part of this URL.
=head2 userinfo
my $userinfo = $url->userinfo;
$url = $url->userinfo('root:pass%3Bw0rd');
my $info = $url->userinfo;
$url = $url->userinfo('root:pass%3Bw0rd');
Userinfo part of this URL.
Expand All @@ -315,6 +317,15 @@ Authority part of this URL.
Clone this URL.
=head2 host_port
my $host_port = $url->host_port;
Normalized version of L</"host"> and L</"port">.
# "xn--da5b0n.net:8080"
Mojo::URL->new('http://☃.net:8080/test')->host_port;
=head2 ihost
my $ihost = $url->ihost;
Expand Down
22 changes: 11 additions & 11 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -752,17 +752,17 @@ Get L<Mojo::Message::Request> object from L<Mojo::Transaction/"req">.
my $req = $c->tx->req;
# Extract request information
my $url = $c->req->url->to_abs;
my $userinfo = $c->req->url->to_abs->userinfo;
my $host = $c->req->url->to_abs->host;
my $agent = $c->req->headers->user_agent;
my $bytes = $c->req->body;
my $str = $c->req->text;
my $hash = $c->req->params->to_hash;
my $hash = $c->req->json;
my $foo = $c->req->json('/23/foo');
my $dom = $c->req->dom;
my $bar = $c->req->dom('div.bar')->first->text;
my $url = $c->req->url->to_abs;
my $info = $c->req->url->to_abs->userinfo;
my $host = $c->req->url->to_abs->host;
my $agent = $c->req->headers->user_agent;
my $bytes = $c->req->body;
my $str = $c->req->text;
my $hash = $c->req->params->to_hash;
my $hash = $c->req->json;
my $foo = $c->req->json('/23/foo');
my $dom = $c->req->dom;
my $bar = $c->req->dom('div.bar')->first->text;
=head2 res
Expand Down
13 changes: 7 additions & 6 deletions t/mojo/url.t
Expand Up @@ -306,12 +306,13 @@ is "$url", 'wss://[::1]:3000/', 'right format';

# IDNA
$url = Mojo::URL->new('http://bücher.ch:3000/foo');
ok $url->is_abs, 'is absolute';
is $url->scheme, 'http', 'right scheme';
is $url->host, 'bücher.ch', 'right host';
is $url->ihost, 'xn--bcher-kva.ch', 'right internationalized host';
is $url->port, 3000, 'right port';
is $url->path, '/foo', 'right path';
ok $url->is_abs, 'is absolute';
is $url->scheme, 'http', 'right scheme';
is $url->host, 'bücher.ch', 'right host';
is $url->ihost, 'xn--bcher-kva.ch', 'right internationalized host';
is $url->port, 3000, 'right port';
is $url->host_port, 'xn--bcher-kva.ch:3000', 'right host and port';
is $url->path, '/foo', 'right path';
is "$url", 'http://xn--bcher-kva.ch:3000/foo', 'right format';
$url = Mojo::URL->new('http://bücher.bücher.ch:3000/foo');
ok $url->is_abs, 'is absolute';
Expand Down

0 comments on commit 544807f

Please sign in to comment.