Skip to content

Commit

Permalink
improved MOJO_MAX_MEMORY_SIZE handling by moving it from Mojo::Conten…
Browse files Browse the repository at this point in the history
…t::Single to Mojo::Asset::Memory
  • Loading branch information
kraih committed Oct 22, 2011
1 parent 67d2c25 commit 8500233
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,8 @@ This file documents the revision history for Perl extension Mojolicious.
2.05 2011-10-22 00:00:00
- Improved start_tls method in Mojo::IOLoop by allowing it to accept
more options.
- Improved MOJO_MAX_MEMORY_SIZE handling by moving it from
Mojo::Content::Single to Mojo::Asset::Memory.
- Improved documentation.
- Fixed HTTPS proxy bug in Mojo::UserAgent.

Expand Down
38 changes: 19 additions & 19 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -180,12 +180,12 @@ Mojo::Asset::File - File asset
use Mojo::Asset::File;
my $asset = Mojo::Asset::File->new;
$asset->add_chunk('foo bar baz');
say $asset->slurp;
my $file = Mojo::Asset::File->new;
$file->add_chunk('foo bar baz');
say $file->slurp;
my $asset = Mojo::Asset::File->new(path => '/foo/bar/baz.txt');
say $asset->slurp;
my $file = Mojo::Asset::File->new(path => '/foo/bar/baz.txt');
say $file->slurp;
=head1 DESCRIPTION
Expand All @@ -197,29 +197,29 @@ L<Mojo::Asset::File> implements the following attributes.
=head2 C<cleanup>
my $cleanup = $asset->cleanup;
$asset = $asset->cleanup(1);
my $cleanup = $file->cleanup;
$file = $file->cleanup(1);
Delete file automatically once it's not used anymore.
=head2 C<handle>
my $handle = $asset->handle;
$asset = $asset->handle(IO::File->new);
my $handle = $file->handle;
$file = $file->handle(IO::File->new);
Actual file handle.
=head2 C<path>
my $path = $asset->path;
$asset = $asset->path('/foo/bar/baz.txt');
my $path = $file->path;
$file = $file->path('/foo/bar/baz.txt');
Actual file path.
=head2 C<tmpdir>
my $tmpdir = $asset->tmpdir;
$asset = $asset->tmpdir('/tmp');
my $tmpdir = $file->tmpdir;
$file = $file->tmpdir('/tmp');
Temporary directory.
Expand All @@ -230,31 +230,31 @@ the following new ones.
=head2 C<add_chunk>
$asset = $asset->add_chunk('foo bar baz');
$file = $file->add_chunk('foo bar baz');
Add chunk of data to asset.
Add chunk of data.
=head2 C<contains>
my $position = $asset->contains('bar');
my $position = $file->contains('bar');
Check if asset contains a specific string.
=head2 C<get_chunk>
my $chunk = $asset->get_chunk($start);
my $chunk = $file->get_chunk($start);
Get chunk of data starting from a specific position.
=head2 C<move_to>
$asset = $asset->move_to('/foo/bar/baz.txt');
$file = $file->move_to('/foo/bar/baz.txt');
Move asset data into a specific file.
=head2 C<size>
my $size = $asset->size;
my $size = $file->size;
Size of asset data in bytes.
Expand Down
24 changes: 14 additions & 10 deletions lib/Mojo/Asset/Memory.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::Asset';

use Carp 'croak';
use IO::File;
use Mojo::Asset::File;

# "There's your giraffe, little girl.
# I'm a boy.
Expand All @@ -16,6 +17,8 @@ sub new {
sub add_chunk {
my ($self, $chunk) = @_;
$self->{content} .= $chunk if defined $chunk;
return Mojo::Asset::File->new->add_chunk($self->slurp)
if $self->size > ($ENV{MOJO_MAX_MEMORY_SIZE} || 262144);
return $self;
}

Expand Down Expand Up @@ -66,9 +69,9 @@ Mojo::Asset::Memory - In-memory asset
use Mojo::Asset::Memory;
my $asset = Mojo::Asset::Memory->new;
$asset->add_chunk('foo bar baz');
say $asset->slurp;
my $mem = Mojo::Asset::Memory->new;
$mem->add_chunk('foo bar baz');
say $mem->slurp;
=head1 DESCRIPTION
Expand All @@ -81,37 +84,38 @@ implements the following new ones.
=head2 C<new>
my $asset = Mojo::Asset::Memory->new;
my $mem = Mojo::Asset::Memory->new;
Construct a new L<Mojo::Asset::Memory> object.
=head2 C<add_chunk>
$asset = $asset->add_chunk('foo bar baz');
$mem = $mem->add_chunk('foo bar baz');
my $file = $mem->add_chunk('abc' x 262144);
Add chunk of data to asset.
Add chunk of data and upgrade to L<Mojo::Asset::File> object if necessary.
=head2 C<contains>
my $position = $asset->contains('bar');
my $position = $mem->contains('bar');
Check if asset contains a specific string.
=head2 C<get_chunk>
my $chunk = $asset->get_chunk($offset);
my $chunk = $mem->get_chunk($offset);
Get chunk of data starting from a specific position.
=head2 C<move_to>
$asset = $asset->move_to('/foo/bar/baz.txt');
$mem = $mem->move_to('/foo/bar/baz.txt');
Move asset data into a specific file.
=head2 C<size>
my $size = $asset->size;
my $size = $mem->size;
Size of asset data in bytes.
Expand Down
19 changes: 8 additions & 11 deletions lib/Mojo/Content/Single.pm
@@ -1,7 +1,6 @@
package Mojo::Content::Single;
use Mojo::Base 'Mojo::Content';

use Mojo::Asset::File;
use Mojo::Asset::Memory;
use Mojo::Content::MultiPart;

Expand Down Expand Up @@ -52,23 +51,19 @@ sub parse {
return $multi->parse;
}

# Don't waste memory and upgrade to file based storage on demand
my $asset = $self->asset;
$self->asset($asset = Mojo::Asset::File->new->add_chunk($asset->slurp))
if $asset->isa('Mojo::Asset::Memory')
&& $asset->size > ($ENV{MOJO_MAX_MEMORY_SIZE} || 262144);

# Chunked body or relaxed content
my $asset = $self->asset;
if ($self->is_chunked || $self->relaxed) {
$asset->add_chunk($self->{buffer});
$self->asset($asset->add_chunk($self->{buffer}));
$self->{buffer} = '';
}

# Normal body
else {
my $len = $self->headers->content_length || 0;
my $need = $len - $asset->size;
$asset->add_chunk(substr $self->{buffer}, 0, $need, '') if $need > 0;
$self->asset($asset->add_chunk(substr $self->{buffer}, 0, $need, ''))
if $need > 0;

# Finished
$self->{state} = 'finished' if $len <= $self->progress;
Expand Down Expand Up @@ -169,9 +164,11 @@ Get a chunk of content starting from a specfic position.
=head2 C<parse>
$single = $single->parse("Content-Length: 12\r\n\r\nHello World!");
$single = $single->parse("Content-Length: 12\r\n\r\nHello World!");
my $multi = $single->parse("Content-Type: multipart/form-data\r\n\r\n");
Parse content chunk.
Parse content chunk and upgrade to L<Mojo::Content::MultiPart> object if
possible.
=head1 SEE ALSO
Expand Down
5 changes: 3 additions & 2 deletions t/mojo/message.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 1340;
use Test::More tests => 1341;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -261,9 +261,10 @@ $req->parse('-Type: text/');
is $req->content->progress, 0, 'right progress';
$req->parse("plain\x0d\x0aContent-Length: 27\x0d\x0a\x0d\x0aHell");
is $req->content->progress, 4, 'right progress';
is $req->content->asset->isa('Mojo::Asset::Memory'), 1, 'stored in memory';
$req->parse("o World!\n");
is $req->content->progress, 13, 'right progress';
is $req->content->asset->isa('Mojo::Asset::Memory'), 1, 'stored in memory';
is $req->content->asset->isa('Mojo::Asset::File'), 1, 'stored in file';
$req->parse("1234\nlalalala\n");
is $req->content->progress, 27, 'right progress';
is $req->content->asset->isa('Mojo::Asset::File'), 1, 'stored in file';
Expand Down

0 comments on commit 8500233

Please sign in to comment.