Skip to content

Commit

Permalink
improved Mojo::Message to allow max_message_size check to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 17, 2013
1 parent ca78a52 commit cfe02df
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@

4.50 2013-10-18
- Improved Mojo::Message to allow max_message_size check to be disabled.

4.49 2013-10-17
- Added tls_ciphers option to Mojo::IOLoop::Server::listen.
- Added ciphers parameter to Mojo::Server::Daemon::listen.
Expand Down
14 changes: 8 additions & 6 deletions lib/Mojo/Message.pm
Expand Up @@ -14,7 +14,7 @@ use Mojo::Util 'decode';
has content => sub { Mojo::Content::Single->new };
has default_charset => 'UTF-8';
has max_line_size => sub { $ENV{MOJO_MAX_LINE_SIZE} || 10240 };
has max_message_size => sub { $ENV{MOJO_MAX_MESSAGE_SIZE} || 10485760 };
has max_message_size => sub { $ENV{MOJO_MAX_MESSAGE_SIZE} // 10485760 };
has version => '1.1';

sub body {
Expand Down Expand Up @@ -153,8 +153,9 @@ sub parse {
my ($self, $chunk) = @_;

# Check message size
my $max = $self->max_message_size;
return $self->_limit('Maximum message size exceeded', 413)
if ($self->{raw_size} += length($chunk //= '')) > $self->max_message_size;
if $max && ($self->{raw_size} += length($chunk //= '')) > $max;

$self->{buffer} .= $chunk;

Expand Down Expand Up @@ -392,10 +393,11 @@ MOJO_MAX_LINE_SIZE environment variable or C<10240>.
$msg = $msg->max_message_size(1024);
Maximum message size in bytes, defaults to the value of the
MOJO_MAX_MESSAGE_SIZE environment variable or C<10485760>. Note that
increasing this value can also drastically increase memory usage, should you
for example attempt to parse an excessively large message body with the
C<body_params>, C<dom> or C<json> methods.
MOJO_MAX_MESSAGE_SIZE environment variable or C<10485760>. Setting the value
to C<0> will allow messages of indefinite size. Note that increasing this
value can also drastically increase memory usage, should you for example
attempt to parse an excessively large message body with the C<body_params>,
C<dom> or C<json> methods.
=head2 version
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -43,7 +43,7 @@ has types => sub { Mojolicious::Types->new };
has validator => sub { Mojolicious::Validator->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.49';
our $VERSION = '4.50';

sub AUTOLOAD {
my $self = shift;
Expand Down
18 changes: 18 additions & 0 deletions t/mojo/response.t
Expand Up @@ -195,6 +195,24 @@ is $res->version, '1.0', 'right version';
is $res->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $res->headers->content_length, 27, 'right "Content-Length" value';

# Parse full HTTP 1.0 response (no limit)
{
local $ENV{MOJO_MAX_MESSAGE_SIZE} = 0;
$res = Mojo::Message::Response->new;
is $res->max_message_size, 0, 'right size';
$res->parse("HTTP/1.0 500 Internal Server Error\x0d\x0a");
$res->parse("Content-Type: text/plain\x0d\x0a");
$res->parse("Content-Length: 27\x0d\x0a\x0d\x0a");
$res->parse("Hello World!\n1234\nlalalala\n");
ok $res->is_finished, 'response is finished';
ok !$res->error, 'no error';
is $res->code, 500, 'right status';
is $res->message, 'Internal Server Error', 'right message';
is $res->version, '1.0', 'right version';
is $res->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $res->headers->content_length, 27, 'right "Content-Length" value';
}

# Parse full HTTP 1.0 response (missing Content-Length)
$res = Mojo::Message::Response->new;
$res->parse("HTTP/1.0 500 Internal Server Error\x0d\x0a");
Expand Down

0 comments on commit cfe02df

Please sign in to comment.