Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 22, 2012
1 parent 609ac9e commit f97c311
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -789,7 +789,7 @@ be easily changed.

1;

All templates from the DATA section are bound to the encoding of the Perl
All templates from the C<DATA> section are bound to the encoding of the Perl
script, so don't forget to use the L<utf8> pragma if necessary.

use Mojolicious::Lite;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Renderer.pm
Expand Up @@ -341,7 +341,7 @@ Register a new helper.
handler => 'epl'
}, 'foo.html.ep');
Get a DATA template by name, usually used by handlers.
Get a C<DATA> section template by name, usually used by handlers.
=head2 C<render>
Expand Down
21 changes: 11 additions & 10 deletions lib/Mojolicious/Static.pm
Expand Up @@ -42,14 +42,13 @@ sub serve {

# Search all paths
my $asset;
my $size = 0;
my $modified = $self->{modified} ||= time;
my $res = $c->res;
my $mtime = $self->{mtime} ||= time;
my $res = $c->res;
for my $path (@{$self->paths}) {
next unless my $data = $self->_get_file(catfile $path, split('/', $rel));

# Exists
last if ($asset, $size, $modified) = @$data;
last if ($asset, $mtime) = @$data;

# Forbidded
$c->app->log->debug(qq{File "$rel" is forbidden.});
Expand All @@ -58,7 +57,6 @@ sub serve {

# Search DATA
if (!$asset && defined(my $data = $self->_get_data_file($c, $rel))) {
$size = length $data;
$asset = Mojo::Asset::Memory->new->add_chunk($data);
}

Expand All @@ -67,7 +65,7 @@ sub serve {
my $b = $self->{bundled} ||= Mojo::Home->new(Mojo::Home->mojo_lib_dir)
->rel_dir('Mojolicious/public');
my $data = $self->_get_file(catfile($b, split('/', $rel)));
($asset, $size, $modified) = @$data if $data && @$data;
($asset, $mtime) = @$data if $data && @$data;
}

# Not a static file
Expand All @@ -80,17 +78,20 @@ sub serve {

# Not modified
my $since = Mojo::Date->new($date)->epoch;
if (defined $since && $since == $modified) {
if (defined $since && $since == $mtime) {
$res_headers->remove('Content-Type')->remove('Content-Length')
->remove('Content-Disposition');
return $res->code(304);
}
}

# Range
my $size = $asset->size;
my $start = 0;
my $end = $size - 1 >= 0 ? $size - 1 : 0;
my $end = $size - 1 >= 0 ? $size - 1 : 0;
if (my $range = $req_headers->range) {

# Satisfiable
if ($range =~ m/^bytes=(\d+)-(\d+)?/ && $1 <= $end) {
$start = $1;
$end = $2 if defined $2 && $2 <= $end;
Expand All @@ -109,7 +110,7 @@ sub serve {
$res->content->asset($asset);
$rel =~ /\.(\w+)$/;
return $res_headers->content_type($c->app->types->type($1) || 'text/plain')
->accept_ranges('bytes')->last_modified(Mojo::Date->new($modified));
->accept_ranges('bytes')->last_modified(Mojo::Date->new($mtime));
}

# "I like being a women.
Expand Down Expand Up @@ -137,7 +138,7 @@ sub _get_file {
no warnings 'newline';
return unless -f $path;
return [] unless -r $path;
return [Mojo::Asset::File->new(path => $path), (stat $path)[7, 9]];
return [Mojo::Asset::File->new(path => $path), (stat $path)[9]];
}

1;
Expand Down

0 comments on commit f97c311

Please sign in to comment.