Skip to content

Commit

Permalink
fixed gzip compression to work with chunked content
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 6, 2012
1 parent 6072f3a commit 1e977dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Mojo/Content.pm
Expand Up @@ -296,7 +296,7 @@ sub _parse_chunked_trailing_headers {
$encoding
? $headers->transfer_encoding($encoding)
: $headers->remove('Transfer-Encoding');
$headers->content_length($self->{real_size});
$headers->content_length($self->{real_size}) unless $headers->content_length;
}

sub _parse_headers {
Expand Down Expand Up @@ -348,7 +348,7 @@ sub _uncompress {
$self->emit(read => $out) if defined $out;
# Replace Content-Encoding with Content-Length
$self->headers->content_length($gz->total_out)->remove('Transfer-Encoding')
$self->headers->content_length($gz->total_out)->remove('Content-Encoding')
if $status == Z_STREAM_END;
# Check buffer size
Expand Down
30 changes: 30 additions & 0 deletions t/mojo/response.t
Expand Up @@ -372,6 +372,7 @@ is $res->message, 'Internal Server Error', 'right message';
is $res->version, '1.1', 'right version';
is $res->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $res->headers->content_length, 13, 'right "Content-Length" value';
is $res->headers->transfer_encoding, undef, 'no "Transfer-Encoding" value';
is $res->body_size, 13, 'right size';

# Parse HTTP 1.1 multipart response
Expand Down Expand Up @@ -451,6 +452,35 @@ is $res->version, '1.1', 'right version';
is $res->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $res->headers->content_length, length($uncompressed),
'right "Content-Length" value';
is $res->headers->content_encoding, undef, 'no "Content-Encoding" value';
is $res->body, $uncompressed, 'right content';

# Parse HTTP 1.1 chunked gzip compressed response
$compressed = undef;
gzip \($uncompressed = 'abc' x 1000), \$compressed;
$res = Mojo::Message::Response->new;
$res->parse("HTTP/1.1 200 OK\x0d\x0a");
$res->parse("Content-Type: text/plain\x0d\x0a");
$res->parse("Content-Encoding: gzip\x0d\x0a");
$res->parse("Transfer-Encoding: chunked\x0d\x0a\x0d\x0a");
$res->parse("1\x0d\x0a");
$res->parse(substr($compressed, 0, 1));
$res->parse("\x0d\x0a");
$res->parse(sprintf('%x', length($compressed) - 1));
$res->parse("\x0d\x0a");
$res->parse(substr($compressed, 1, length($compressed) - 1));
$res->parse("\x0d\x0a");
$res->parse("0\x0d\x0a\x0d\x0a");
ok $res->is_finished, 'response is finished';
ok !$res->error, 'no error';
is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
is $res->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $res->headers->content_length, length($uncompressed),
'right "Content-Length" value';
is $res->headers->transfer_encoding, undef, 'no "Transfer-Encoding" value';
is $res->headers->content_encoding, undef, 'no "Content-Encoding" value';
is $res->body, $uncompressed, 'right content';

# Build HTTP 1.1 response start line with minimal headers
Expand Down

0 comments on commit 1e977dd

Please sign in to comment.