Skip to content

Commit

Permalink
handle chunk size more consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 4, 2012
1 parent 8c53b68 commit c5dd0bb
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 18 deletions.
5 changes: 2 additions & 3 deletions lib/Mojo/Content.pm
Expand Up @@ -4,8 +4,6 @@ use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';
use Mojo::Headers;

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;

has [qw/auto_relax relaxed/] => 0;
has headers => sub { Mojo::Headers->new };
has max_leftover_size => sub { $ENV{MOJO_MAX_LEFTOVER_SIZE} || 262144 };
Expand Down Expand Up @@ -69,7 +67,8 @@ sub get_header_chunk {
= $headers ? "$headers\x0d\x0a\x0d\x0a" : "\x0d\x0a";
}

return substr $self->{header_buffer}, $offset, CHUNK_SIZE;
return substr $self->{header_buffer}, $offset,
$ENV{MOJO_CHUNK_SIZE} || 131072;
}

sub has_leftovers { length(shift->{buffer} || '') }
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -5,8 +5,6 @@ use Errno qw/EAGAIN ECONNRESET EINTR EPIPE EWOULDBLOCK/;
use Scalar::Util 'weaken';
use Time::HiRes 'time';

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;

has reactor => sub {
require Mojo::IOLoop;
Mojo::IOLoop->singleton->reactor;
Expand Down Expand Up @@ -105,7 +103,8 @@ sub _read {
my $self = shift;

# Read
my $read = $self->{handle}->sysread(my $buffer, CHUNK_SIZE, 0);
my $read
= $self->{handle}->sysread(my $buffer, $ENV{MOJO_CHUNK_SIZE} || 131072, 0);

# Error
unless (defined $read) {
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojo/Message.pm
Expand Up @@ -12,8 +12,6 @@ use Mojo::Upload;
use Mojo::Util qw/decode url_unescape/;
use Scalar::Util 'weaken';

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;

has content => sub { Mojo::Content::Single->new };
has default_charset => 'UTF-8';
has dom_class => 'Mojo::DOM';
Expand Down Expand Up @@ -210,7 +208,7 @@ sub get_start_line_chunk {
my ($self, $offset) = @_;
$self->emit(progress => 'start_line', $offset);
return substr $self->{start_line_buffer} //= $self->_build_start_line,
$offset, CHUNK_SIZE;
$offset, $ENV{MOJO_CHUNK_SIZE} || 131072;
}

sub has_leftovers { shift->content->has_leftovers }
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Server/CGI.pm
@@ -1,8 +1,6 @@
package Mojo::Server::CGI;
use Mojo::Base 'Mojo::Server';

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;

has 'nph';

# "Lisa, you're a Buddhist, so you believe in reincarnation.
Expand All @@ -21,7 +19,8 @@ sub run {
# Request body
binmode STDIN;
until ($req->is_finished) {
last unless my $read = STDIN->read(my $buffer, CHUNK_SIZE, 0);
my $size = $ENV{MOJO_CHUNK_SIZE} || 131072;
last unless my $read = STDIN->read(my $buffer, $size, 0);
$req->parse($buffer);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/Mojo/Server/PSGI.pm
@@ -1,8 +1,6 @@
package Mojo::Server::PSGI;
use Mojo::Base 'Mojo::Server';

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;

# "Things aren't as happy as they used to be down here at the unemployment
# office.
# Joblessness is no longer just for philosophy majors.
Expand All @@ -20,7 +18,8 @@ sub run {
# Request body
my $len = $env->{CONTENT_LENGTH};
until ($req->is_finished) {
my $chunk = ($len && $len < CHUNK_SIZE) ? $len : CHUNK_SIZE;
my $size = $ENV{MOJO_CHUNK_SIZE} || 131072;
my $chunk = ($len && $len < $size) ? $len : $size;
last unless my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
$req->parse($buffer);
last if ($len -= $read) <= 0;
Expand Down
4 changes: 1 addition & 3 deletions lib/Mojo/Template.pm
Expand Up @@ -7,8 +7,6 @@ use Mojo::ByteStream;
use Mojo::Exception;
use Mojo::Util qw/decode encode/;

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 131072;

# "If for any reason you're not completely satisfied, I hate you."
has [qw/auto_escape compiled/];
has [qw/append code prepend template/] => '';
Expand Down Expand Up @@ -301,7 +299,7 @@ sub render_file {
$self->name($path) unless defined $self->{name};
croak qq/Can't open template "$path": $!/ unless open my $file, '<', $path;
my $tmpl = '';
while ($file->sysread(my $buffer, CHUNK_SIZE, 0)) { $tmpl .= $buffer }
while ($file->sysread(my $buffer, 131072, 0)) { $tmpl .= $buffer }

# Decode and render
if (my $encoding = $self->encoding) {
Expand Down

0 comments on commit c5dd0bb

Please sign in to comment.