Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port to Channel object for UserAgent
this redoes the work prior to backporting relevant changes from an earlier branch still have to port to using the events
- Loading branch information
Showing
2 changed files
with
70 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package Mojo::Channel::HTTP::Client; | ||
|
||
use Mojo::Base 'Mojo::EventEmitter'; | ||
|
||
use Mojo::Util 'term_escape'; | ||
use Scalar::Util 'weaken'; | ||
|
||
use constant DEBUG => $ENV{MOJO_USERAGENT_DEBUG}; | ||
|
||
has [qw/ioloop tx/]; | ||
|
||
sub read { | ||
my ($self, $id, $chunk) = @_; | ||
return unless my $tx = $self->tx; | ||
|
||
# Process incoming data | ||
warn term_escape "-- Client <<< Server (@{[_url($tx)]})\n$chunk\n" if DEBUG; | ||
$tx->client_read($chunk); | ||
if ($tx->is_finished) { $self->emit(finished => $id) } | ||
elsif ($tx->is_writing) { $self->write($id) } | ||
} | ||
|
||
sub write { | ||
my ($self, $id) = @_; | ||
return unless my $tx = $self->tx; | ||
return if !$tx->is_writing || $self->{writing}++; | ||
my $chunk = $tx->client_write; | ||
delete $self->{writing}; | ||
warn term_escape "-- Client >>> Server (@{[_url($tx)]})\n$chunk\n" if DEBUG; | ||
my $stream = $self->ioloop->stream($id)->write($chunk); | ||
$self->emit(finished => $id) if $tx->is_finished; | ||
|
||
# Continue writing | ||
return unless $tx->is_writing; | ||
weaken $self; | ||
$stream->write('' => sub { $self->write($id) if $self }); | ||
} | ||
|
||
sub _url { shift->req->url->to_abs } | ||
|
||
1; | ||
|
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