Skip to content

Commit

Permalink
improved message parser performance slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 2, 2012
1 parent 8fdbbfa commit 899af07
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

2.99 2012-06-03
- Improved message parser performance slightly.
- Improved documentation.
- Improved tests.
- Fixed small JSON Pointer bug in get command. (avkhozov)
Expand Down
19 changes: 7 additions & 12 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -129,22 +129,18 @@ sub parse {
my $base = $self->url->base;
$base->scheme('http') unless $base->scheme;
my $headers = $self->headers;
if (!$base->authority && (my $host = $headers->host)) {
if (!$base->host && (my $host = $headers->host)) {
$base->authority($host);
}

# Basic authentication
if (my $auth = $headers->authorization) {
if (my $userinfo = $self->_parse_basic_auth($auth)) {
$base->userinfo($userinfo);
}
if (my $userinfo = _parse_basic_auth($headers->authorization)) {
$base->userinfo($userinfo);
}

# Basic proxy authentication
if (my $auth = $headers->proxy_authorization) {
if (my $userinfo = $self->_parse_basic_auth($auth)) {
$self->proxy(Mojo::URL->new->userinfo($userinfo));
}
if (my $userinfo = _parse_basic_auth($headers->proxy_authorization)) {
$self->proxy(Mojo::URL->new->userinfo($userinfo));
}

# "X-Forwarded-HTTPS"
Expand Down Expand Up @@ -200,9 +196,8 @@ sub _build_start_line {
}

sub _parse_basic_auth {
my ($self, $header) = @_;
return unless $header =~ /Basic (.+)$/;
return b64_decode $1;
return unless my $header = shift;
return $header =~ /Basic (.+)$/ ? b64_decode($1) : undef;
}

sub _parse_env {
Expand Down
23 changes: 7 additions & 16 deletions lib/Mojo/URL.pm
Expand Up @@ -25,33 +25,24 @@ sub authority {

# New authority
if (defined $authority) {
my $host = $authority;

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

# Port
my $port = undef;
if ($host =~ /^(.+)\:(\d+)$/) {
$host = $1;
$self->port($2);
}
if ($authority =~ s/\:(\d+)$//) { $self->port($1) }

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

# Format
my $userinfo = $self->userinfo;
$authority .= url_escape($userinfo, "^$UNRESERVED$SUBDELIM\:") . '@'
if $userinfo;
if (my $userinfo = $self->userinfo) {
$authority .= url_escape($userinfo, "^$UNRESERVED$SUBDELIM\:") . '@';
}
$authority .= lc($self->ihost || '');
my $port = $self->port;
$authority .= ":$port" if $port;
if (my $port = $self->port) { $authority .= ":$port" }

return $authority;
}
Expand Down

0 comments on commit 899af07

Please sign in to comment.