Skip to content

Commit

Permalink
added is_range method to Mojo::Asset and a content post-processing re…
Browse files Browse the repository at this point in the history
…cipe to the rendering guide
  • Loading branch information
kraih committed Dec 5, 2012
1 parent 4e0b549 commit 0af2b2a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

3.65 2012-12-04
3.65 2012-12-05
- Added is_range method to Mojo::Asset.
- Improved documentation.
- Improved tests.
- Fixed parameter replacement bug in Mojo::Parameters. (alexbyk, sri)
Expand Down
11 changes: 11 additions & 0 deletions lib/Mojo/Asset.pm
Expand Up @@ -12,6 +12,11 @@ sub get_chunk { croak 'Method "get_chunk" not implemented by subclass' }

sub is_file {undef}

sub is_range {
my $self = shift;
return !!($self->end_range || $self->start_range);
}

sub move_to { croak 'Method "move_to" not implemented by subclass' }
sub size { croak 'Method "size" not implemented by subclass' }
sub slurp { croak 'Method "slurp" not implemented by subclass' }
Expand Down Expand Up @@ -87,6 +92,12 @@ in a subclass.
False.
=head2 C<is_range>
my $success = $asset->is_range;
Check if asset has a C<start_range> or C<end_range>.
=head2 C<move_to>
$asset = $asset->move_to('/home/sri/foo.txt');
Expand Down
38 changes: 36 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -934,8 +934,42 @@ do is add a new C<handler>.
@@ index.html.mine
...

Since most template systems don't support templates in the C<DATA> section the
renderer provides methods to help you with that.
Since most template systems don't support templates in the C<DATA> section,
the renderer provides methods to help you with that.

=head2 Post-processing content

While post-processing tasks are generally very easy with the C<after_dispatch>
hook, there are a few things you need to watch out for, such as dynamically
generated content and content that is streamed directly from files.

use Mojolicious::Lite;
use IO::Compress::Gzip 'gzip';

hook after_dispatch => sub {
my $self = shift;

# Check if user has set "gzip => 1" in the stash
return unless $self->stash->{gzip};

# Check if user agent accepts GZip compression
return unless ($self->req->headers->accept_encoding // '') =~ /gzip/i;

# Check if content is streamed from a file or dynamically generated
my $asset = $self->res->content->asset;
return if $asset->is_file || !$asset->size || $asset->is_range;

# Compress content
gzip \(my $dummy = $asset->slurp), \my $compressed;
$self->res->body($compressed)->headers->content_encoding('gzip');
};

get '/' => sub {
my $self = shift;
$self->render(text => 'Hello World!', gzip => 1);
};

app->start;

=head1 MORE

Expand Down
6 changes: 6 additions & 0 deletions t/mojo/asset.t
Expand Up @@ -30,24 +30,28 @@ is $file->contains('a'), -1, 'does not contain "a"';

# Empty memory asset
$mem = Mojo::Asset::Memory->new;
ok !$mem->is_range, 'no range';
is $mem->contains('a'), -1, 'does not contain "a"';

# File asset range support (a[bcdef])
$file = Mojo::Asset::File->new(start_range => 1);
ok $file->is_range, 'has range';
$file->add_chunk('abcdef');
is $file->contains('bcdef'), 0, '"bcdef" at position 0';
is $file->contains('cdef'), 1, '"cdef" at position 1';
is $file->contains('db'), -1, 'does not contain "db"';

# Memory asset range support (a[bcdef])
$mem = Mojo::Asset::Memory->new(start_range => 1);
ok $mem->is_range, 'has range';
$mem->add_chunk('abcdef');
is $mem->contains('bcdef'), 0, '"bcdef" at position 0';
is $mem->contains('cdef'), 1, '"cdef" at position 1';
is $mem->contains('db'), -1, 'does not contain "db"';

# File asset range support (ab[cdefghi]jk)
$file = Mojo::Asset::File->new(start_range => 2, end_range => 8);
ok $file->is_range, 'has range';
$file->add_chunk('abcdefghijk');
is $file->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $file->contains('fghi'), 3, '"fghi" at position 3';
Expand All @@ -64,6 +68,7 @@ is $chunk, 'hi', 'chunk from position 5';

# Memory asset range support (ab[cdefghi]jk)
$mem = Mojo::Asset::Memory->new(start_range => 2, end_range => 8);
ok $mem->is_range, 'has range';
$mem->add_chunk('abcdefghijk');
is $mem->contains('cdefghi'), 0, '"cdefghi" at position 0';
is $mem->contains('fghi'), 3, '"fghi" at position 3';
Expand All @@ -80,6 +85,7 @@ is $chunk, 'hi', 'chunk from position 5';

# Huge file asset
$file = Mojo::Asset::File->new;
ok !$file->is_range, 'no range';
$file->add_chunk('a' x 131072);
$file->add_chunk('b');
$file->add_chunk('c' x 131072);
Expand Down

0 comments on commit 0af2b2a

Please sign in to comment.