Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved memory usage of chunked transfer encoding parser in Mojo::Co…
…ntent
  • Loading branch information
kraih committed Nov 28, 2012
1 parent 2ab4689 commit cfe45b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,8 +1,10 @@

3.63 2012-11-27
3.63 2012-11-28
- Added support for smooth restarting to Morbo.
- Added acceptor method to Mojo::IOLoop.
- Added stop method to Mojo::Server::Daemon.
- Improved memory usage of chunked transfer encoding parser in
Mojo::Content.
- Improved documentation.
- Improved tests.

Expand Down
29 changes: 13 additions & 16 deletions lib/Mojo/Content.pm
Expand Up @@ -248,29 +248,26 @@ sub _parse_chunked {
return $self->_parse_chunked_trailing_headers
if ($self->{chunk_state} // '') eq 'trailing_headers';

# New chunk (ignore the chunk extension)
while ($self->{pre_buffer} =~ /^((?:\x0d?\x0a)?([[:xdigit:]]+).*\x0a)/) {
my $header = $1;
my $len = hex $2;
# Parse chunks
while (length $self->{pre_buffer}) {

# Check if we have a whole chunk yet
last unless length($self->{pre_buffer}) >= (length($header) + $len);
# Start new chunk (ignore the chunk extension)
unless ($self->{chunk_len}) {
last
unless $self->{pre_buffer} =~ s/^(?:\x0d?\x0a)?([[:xdigit:]]+).*\x0a//;
next if $self->{chunk_len} = hex $1;

# Remove header
substr $self->{pre_buffer}, 0, length $header, '';

# Last chunk
if ($len == 0) {
# Last chunk_state
$self->{chunk_state} = 'trailing_headers';
last;
}

# Remove payload
$self->{real_size} += $len;
# Remove as much as possible from payload
last unless my $len = length $self->{pre_buffer};
$len = $self->{chunk_len} if $self->{chunk_len} < $len;
$self->{buffer} .= substr $self->{pre_buffer}, 0, $len, '';

# Remove newline at end of chunk
$self->{pre_buffer} =~ s/^(\x0d?\x0a)//;
$self->{real_size} += $len;
$self->{chunk_len} -= $len;
}

# Trailing headers
Expand Down
4 changes: 3 additions & 1 deletion t/mojo/response.t
Expand Up @@ -469,7 +469,9 @@ $res->parse("\x0d\x0a1a\x0d\x0a");
$res->parse("Content-Type: image/jpeg\x0d\x0a");
$res->parse("\x0d\x0a16\x0d\x0a");
$res->parse("Content-ID: 600050\x0d\x0a\x0d\x0a");
$res->parse("\x0d\x0a6\x0d\x0a");
$res->parse("\x0d\x0a");
$res->parse('6');
$res->parse("\x0d\x0a");
$res->parse("abcd\x0d\x0a");
$res->parse("\x0d\x0a7\x0d\x0a");
$res->parse("--AAA\x0d\x0a");
Expand Down

0 comments on commit cfe45b3

Please sign in to comment.