Skip to content

Commit

Permalink
fix bug in Mojo::Content where the drain event would not always be em…
Browse files Browse the repository at this point in the history
…itted for dynamically generated content with a Content-Length header (closes #949)
  • Loading branch information
kraih committed Apr 30, 2016
1 parent ffe7ea7 commit 6ab2acc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -2,6 +2,8 @@
6.61 2016-04-30
- Improved Mojo::Server::Daemon to no longer log when a connection has been
closed prematurely.
- Fixed bug in Mojo::Content where the drain event would not always be emitted
for dynamically generated content with a Content-Length header.

6.60 2016-04-25
- Fixed bug in Mojo::IOLoop where stopping gracefully would sometimes result
Expand Down
8 changes: 5 additions & 3 deletions lib/Mojo/Content.pm
Expand Up @@ -38,8 +38,10 @@ sub clone {
sub generate_body_chunk {
my ($self, $offset) = @_;

$self->emit(drain => $offset) unless length($self->{body_buffer} // '');
my $chunk = delete $self->{body_buffer} // '';
$self->emit(drain => $offset) unless length($self->{body_buffer} //= '');
my $len = $self->headers->content_length;
return '' if looks_like_number $len && $len == $offset;
my $chunk = delete $self->{body_buffer};
return $self->{eof} ? '' : undef unless length $chunk;

return $chunk;
Expand All @@ -59,7 +61,7 @@ sub is_chunked { !!shift->headers->transfer_encoding }

sub is_compressed { lc(shift->headers->content_encoding // '') eq 'gzip' }

sub is_dynamic { $_[0]{dynamic} && !defined $_[0]->headers->content_length }
sub is_dynamic { !!$_[0]{dynamic} }

sub is_finished { (shift->{state} // '') eq 'finished' }

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/response.t
Expand Up @@ -966,7 +966,7 @@ while (1) {
}
$res->fix_headers;
is $res->headers->connection, undef, 'no "Connection" value';
ok !$res->content->is_dynamic, 'no dynamic content';
ok $res->content->is_dynamic, 'dynamic content';
is $count, length($body), 'right length';
is $full, $body, 'right content';

Expand Down
9 changes: 8 additions & 1 deletion t/mojolicious/longpolling_lite_app.t
Expand Up @@ -72,7 +72,11 @@ get '/longpoll/length' => sub {
Mojo::IOLoop->timer(
0.25 => sub {
$c->on(finish => sub { shift->stash->{finished}++ });
$c->write('there plain,' => sub { shift->write(' whats up?') });
$c->write(
'there plain,' => sub {
shift->write(' whats up?' => sub { shift->stash->{drain}++ });
}
);
}
);
};
Expand Down Expand Up @@ -246,9 +250,12 @@ is $tx->res->error->{message}, 'Interrupted', 'right error';
is $buffer, 'hi ', 'right content';

# Stream with delay and content length
$stash = undef;
$t->app->plugins->once(before_dispatch => sub { $stash = shift->stash });
$t->get_ok('/longpoll/length')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')->content_type_is('text/plain')
->content_is('hi there plain, whats up?');
is $stash->{drain}, 1, 'drain event has been emitted once';

# Stream with delay and finish
$t->get_ok('/longpoll/nolength')->status_is(200)
Expand Down

0 comments on commit 6ab2acc

Please sign in to comment.