Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 5, 2013
1 parent 599cabf commit 4241161
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/Mojo/Content.pm
Expand Up @@ -155,7 +155,7 @@ sub parse_body {
sub progress {
my $self = shift;
return 0 unless my $state = $self->{state};
return 0 unless grep { $_ eq $state } qw(body finished);
return 0 unless $state eq 'body' || $state eq 'finished';
return $self->{raw_size} - ($self->{header_size} || 0);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -134,7 +134,7 @@ sub parse {
if (!$self->xml && $VOID{$start}) || $attr =~ m!/\s*$!;

# Relaxed "script" or "style"
if (grep { $_ eq $start } qw(script style)) {
if ($start eq 'script' || $start eq 'style') {
if ($html =~ m!\G(.*?)<\s*/\s*$start\s*>!gcsi) {
push @$current, ['raw', $1];
$self->_end($start, \$current);
Expand Down Expand Up @@ -309,17 +309,17 @@ sub _start {
elsif ($start eq 'tr') { $self->_close($current, {tr => 1}) }

# "<th>" and "<td>"
elsif (grep { $_ eq $start } qw(th td)) {
elsif ($start eq 'th' || $start eq 'td') {
$self->_close($current, {$_ => 1}) for qw(th td);
}

# "<dt>" and "<dd>"
elsif (grep { $_ eq $start } qw(dt dd)) {
elsif ($start eq 'dt' || $start eq 'dd') {
$self->_end($_, $current) for qw(dt dd);
}

# "<rt>" and "<rp>"
elsif (grep { $_ eq $start } qw(rt rp)) {
elsif ($start eq 'rt' || $start eq 'rp') {
$self->_end($_, $current) for qw(rt rp);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -105,10 +105,10 @@ sub _read {
unless (defined $read) {

# Retry
return if grep { $_ == $! } EAGAIN, EINTR, EWOULDBLOCK;
return if $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK;

# Closed
return $self->close if grep { $_ == $! } ECONNRESET, EPIPE;
return $self->close if $! == ECONNRESET || $! == EPIPE;

# Read error
return $self->emit_safe(error => $!)->close;
Expand All @@ -129,10 +129,10 @@ sub _write {
unless (defined $written) {

# Retry
return if grep { $_ == $! } EAGAIN, EINTR, EWOULDBLOCK;
return if $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK;

# Closed
return $self->close if grep { $_ == $! } ECONNRESET, EPIPE;
return $self->close if $! == ECONNRESET || $! == EPIPE;

# Write error
return $self->emit_safe(error => $!)->close;
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -164,7 +164,7 @@ sub is_finished { (shift->{state} // '') eq 'finished' }

sub is_limit_exceeded {
return undef unless my $code = (shift->error)[1];
return !!grep { $_ eq $code } 413, 431;
return !!($code eq 413 || $code eq 431);
}

sub is_multipart { shift->content->is_multipart }
Expand Down Expand Up @@ -202,8 +202,9 @@ sub parse {
}

# Content
my $state = $self->{state} // '';
$self->content($self->content->parse(delete $self->{buffer}))
if grep { $_ eq ($self->{state} // '') } qw(content finished);
if $state eq 'content' || $state eq 'finished';

# Check line size
return $self->error('Maximum line size exceeded', 431)
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -129,7 +129,7 @@ sub get_start_line_chunk {
sub is_empty {
my $self = shift;
return undef unless my $code = $self->code;
return $self->is_status_class(100) || grep { $_ eq $code } qw(204 304);
return $self->is_status_class(100) || $code eq 204 || $code eq 304;
}

sub is_status_class {
Expand Down
10 changes: 3 additions & 7 deletions lib/Mojo/Path.pm
Expand Up @@ -20,15 +20,11 @@ sub canonicalize {

# ".."
if ($part eq '..') {
unless (@parts && $parts[-1] ne '..') { push @parts, '..' }
else { pop @parts }
next;
(@parts && $parts[-1] ne '..') ? pop @parts : push @parts, '..';
}

# "."
next if grep { $_ eq $part } '.', '';

push @parts, $part;
# Something else than "."
elsif ($part ne '.' && $part ne '') { push @parts, $part }
}
$self->trailing_slash(undef) unless @parts;

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Template.pm
Expand Up @@ -58,7 +58,7 @@ sub build {
if ($type eq 'code' || $multi) { $lines[-1] .= "$value" }

# Expression
if (grep { $_ eq $type } qw(expr escp)) {
if ($type eq 'expr' || $type eq 'escp') {

# Start
unless ($multi) {
Expand Down Expand Up @@ -275,7 +275,7 @@ sub _trim {
for (my $j = @$line - 4; $j >= 0; $j -= 2) {

# Skip captures
next if grep { $_ eq $line->[$j] } qw(cpst cpen);
next if $line->[$j] eq 'cpst' || $line->[$j] eq 'cpen';

# Only trim text
return unless $line->[$j] eq 'text';
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -89,7 +89,7 @@ sub redirect {
my $new = Mojo::Transaction::HTTP->new;
my $req = $old->req;
my $method = uc $req->method;
if (grep { $_ eq $code } 301, 307, 308) {
if ($code eq 301 || $code eq 307 || $code eq 308) {
return undef unless my $req = $req->clone;
$new->req($req);
$req->headers->remove('Host')->remove('Cookie')->remove('Referer');
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -54,7 +54,7 @@ sub render {
}

# Placeholder, relaxed or wildcard
elsif (grep { $_ eq $op } qw(placeholder relaxed wildcard)) {
elsif ($op eq 'placeholder' || $op eq 'relaxed' || $op eq 'wildcard') {
my $name = $token->[1];
$rendered = $values->{$name} // '';
my $default = $self->defaults->{$name};
Expand Down Expand Up @@ -127,7 +127,7 @@ sub _compile {
}

# Placeholder
elsif (grep { $_ eq $op } qw(placeholder relaxed wildcard)) {
elsif ($op eq 'placeholder' || $op eq 'relaxed' || $op eq 'wildcard') {
my $name = $token->[1];
unshift @{$self->placeholders}, $name;

Expand Down Expand Up @@ -212,7 +212,7 @@ sub _tokenize {
}

# Relaxed or wildcard start (upgrade when quoted)
elsif (grep { $_ eq $char } $relaxed, $wildcard) {
elsif ($char eq $relaxed || $char eq $wildcard) {
push @tree, ['placeholder', ''] unless $quoted;
$tree[-1][0] = $state = $char eq $relaxed ? 'relaxed' : 'wildcard';
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Static.pm
Expand Up @@ -6,7 +6,6 @@ use Mojo::Asset::File;
use Mojo::Asset::Memory;
use Mojo::Home;
use Mojo::Loader;
use Mojo::Path;

has classes => sub { ['main'] };
has paths => sub { [] };
Expand All @@ -23,8 +22,9 @@ sub dispatch {

# Canonical path
my $stash = $c->stash;
my $path = $stash->{path} || $c->req->url->path->clone->canonicalize;
return undef unless my @parts = @{Mojo::Path->new("$path")->parts};
my $path = $c->req->url->path;
$path = $stash->{path} ? $path->new($stash->{path}) : $path->clone;
return undef unless my @parts = @{$path->canonicalize->parts};

# Serve static file and prevent directory traversal
return undef if $parts[0] eq '..' || !$self->serve($c, join('/', @parts));
Expand Down

0 comments on commit 4241161

Please sign in to comment.