-
-
Save anonymous/8519a14930e32972fa24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Mojo/Content.pm b/lib/Mojo/Content.pm | |
index 9f0c93f..77d1b41 100644 | |
--- a/lib/Mojo/Content.pm | |
+++ b/lib/Mojo/Content.pm | |
@@ -38,8 +38,7 @@ sub clone { | |
sub generate_body_chunk { | |
my ($self, $offset) = @_; | |
- $self->emit(drain => $offset) | |
- if !delete $self->{delay} && ($self->{body_buffer} // '') eq ''; | |
+ $self->emit(drain => $offset) if ($self->{body_buffer} // '') eq ''; | |
my $chunk = delete $self->{body_buffer} // ''; | |
return $self->{eof} ? '' : undef if $chunk eq ''; | |
@@ -148,8 +147,7 @@ sub write { | |
my ($self, $chunk, $cb) = @_; | |
$self->{dynamic} = 1; | |
- if (defined $chunk) { $self->{body_buffer} .= $chunk } | |
- else { $self->{delay} = 1 } | |
+ $self->{body_buffer} .= $chunk if defined $chunk; | |
$self->once(drain => $cb) if $cb; | |
$self->{eof} = 1 if defined $chunk && $chunk eq ''; | |
diff --git a/lib/Mojo/Server/Daemon.pm b/lib/Mojo/Server/Daemon.pm | |
index f976fd4..cf2c1f3 100644 | |
--- a/lib/Mojo/Server/Daemon.pm | |
+++ b/lib/Mojo/Server/Daemon.pm | |
@@ -93,7 +93,12 @@ sub _build_tx { | |
# HTTP | |
else { $self->emit(request => $tx) } | |
+ # Last keep-alive request or corrupted connection | |
+ $tx->res->headers->connection('close') | |
+ if $c->{requests} >= $self->max_requests || $tx->req->error; | |
+ | |
$tx->on(resume => sub { $self->_write($id) }); | |
+ $self->_write($id); | |
} | |
); | |
@@ -206,14 +211,7 @@ sub _read { | |
my $tx = $c->{tx} ||= $self->_build_tx($id, $c); | |
warn term_escape "-- Server <<< Client (@{[_url($tx)]})\n$chunk\n" if DEBUG; | |
$tx->server_read($chunk); | |
- | |
- # Last keep-alive request or corrupted connection | |
- $tx->res->headers->connection('close') | |
- if $c->{requests} >= $self->max_requests || $tx->req->error; | |
- | |
- # Finish or start writing | |
- if ($tx->is_finished) { $self->_finish($id) } | |
- elsif ($tx->is_writing) { $self->_write($id) } | |
+ $self->_finish($id) if ($c->{tx} || $tx)->is_finished; | |
} | |
sub _remove { | |
@@ -229,17 +227,17 @@ sub _write { | |
# Get chunk and write | |
my $c = $self->{connections}{$id}; | |
- return unless my $tx = $c->{tx}; | |
- return if !$tx->is_writing || $c->{writing}++; | |
+ return if !(my $tx = $c->{tx}) || $c->{writing}++; | |
my $chunk = $tx->server_write; | |
delete $c->{writing}; | |
warn term_escape "-- Server >>> Client (@{[_url($tx)]})\n$chunk\n" if DEBUG; | |
my $stream = $self->ioloop->stream($id)->write($chunk); | |
# Finish or continue writing | |
- my $next = '_write'; | |
+ my $next = $chunk eq '' ? undef : '_write'; | |
$tx->has_subscribers('finish') ? ($next = '_finish') : $self->_finish($id) | |
if $tx->is_finished; | |
+ return unless $next; | |
weaken $self; | |
$stream->write('' => sub { $self->$next($id) }); | |
} | |
diff --git a/lib/Mojo/Transaction/HTTP.pm b/lib/Mojo/Transaction/HTTP.pm | |
index be1375b..97793b0 100644 | |
--- a/lib/Mojo/Transaction/HTTP.pm | |
+++ b/lib/Mojo/Transaction/HTTP.pm | |
@@ -87,17 +87,15 @@ sub _body { | |
my $written = defined $buffer ? length $buffer : 0; | |
$self->{write} = $msg->content->is_dynamic ? 1 : ($self->{write} - $written); | |
$self->{offset} += $written; | |
- if (defined $buffer) { delete $self->{delay} } | |
# Delayed | |
- elsif (delete $self->{delay}) { $self->{state} = 'read' } | |
- else { $self->{delay} = 1 } | |
+ $self->{state} = 'read' unless defined $buffer; | |
# Finished | |
$self->{state} = $finish ? 'finished' : 'read' | |
if $self->{write} <= 0 || defined $buffer && $buffer eq ''; | |
- return defined $buffer ? $buffer : ''; | |
+ return $buffer // ''; | |
} | |
sub _headers { | |
diff --git a/lib/Mojo/UserAgent.pm b/lib/Mojo/UserAgent.pm | |
index d1714b3..248b728 100644 | |
--- a/lib/Mojo/UserAgent.pm | |
+++ b/lib/Mojo/UserAgent.pm | |
@@ -316,7 +316,7 @@ sub _write { | |
# Get and write chunk | |
my $c = $self->{connections}{$id}; | |
- return if $c->{writing}++ || !(my $tx = $c->{tx}); | |
+ return if !(my $tx = $c->{tx}) || $c->{writing}++; | |
my $chunk = $tx->client_write; | |
delete $c->{writing}; | |
warn term_escape "-- Client >>> Server (@{[_url($tx)]})\n$chunk\n" if DEBUG; | |
diff --git a/lib/Mojolicious.pm b/lib/Mojolicious.pm | |
index 70edf8f..6425e6f 100644 | |
--- a/lib/Mojolicious.pm | |
+++ b/lib/Mojolicious.pm | |
@@ -127,7 +127,7 @@ sub handler { | |
# Delayed response | |
$self->log->debug('Nothing has been rendered, expecting delayed response') | |
- unless $c->tx->is_writing; | |
+ unless $c->stash->{'mojo.rendered'}; | |
} | |
sub helper { shift->renderer->add_helper(@_) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment