Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move client_read methods to respective channel classes
  • Loading branch information
jberger committed Dec 20, 2015
1 parent d9f92f1 commit d9b36c6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 20 deletions.
17 changes: 17 additions & 0 deletions lib/Mojo/Channel/HTTP/Client.pm
Expand Up @@ -4,5 +4,22 @@ use Mojo::Base -base;

has [qw/ioloop tx/];

sub read {
my ($self, $chunk) = @_;
my $tx = $self->tx;

# Skip body for HEAD request
my $res = $tx->res;
$res->content->skip_body(1) if uc $tx->req->method eq 'HEAD';
return unless $res->parse($chunk)->is_finished;

# Unexpected 1xx response
return $tx->{state} = 'finished'
if !$res->is_status_class(100) || $res->headers->upgrade;
$tx->res($res->new)->emit(unexpected => $res);
return if (my $leftovers = $res->content->leftovers) eq '';
$self->read($leftovers);
}

1;

21 changes: 21 additions & 0 deletions lib/Mojo/Channel/WebSocket.pm
@@ -0,0 +1,21 @@
package Mojo::Channel::WebSocket;

use Mojo::Base -base;

has [qw/ioloop tx/];

sub read {
my ($self, $chunk) = @_;
my $tx = $self->tx;

$self->{read} .= $chunk // '';
while (my $frame = $tx->parse_frame(\$self->{read})) {
$tx->finish(1009) and last unless ref $frame;
$tx->emit(frame => $frame);
}

$tx->emit('resume');
}

1;

6 changes: 6 additions & 0 deletions lib/Mojo/Channel/WebSocket/Client.pm
@@ -0,0 +1,6 @@
package Mojo::Channel::WebSocket::Client;

use Mojo::Base 'Mojo::Channel::WebSocket';

1;

16 changes: 0 additions & 16 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -5,22 +5,6 @@ use Mojo::Transaction::WebSocket;

has 'previous';

sub client_read {
my ($self, $chunk) = @_;

# Skip body for HEAD request
my $res = $self->res;
$res->content->skip_body(1) if uc $self->req->method eq 'HEAD';
return unless $res->parse($chunk)->is_finished;

# Unexpected 1xx response
return $self->{state} = 'finished'
if !$res->is_status_class(100) || $res->headers->upgrade;
$self->res($res->new)->emit(unexpected => $res);
return if (my $leftovers = $res->content->leftovers) eq '';
$self->client_read($leftovers);
}

sub client_write { shift->_write(0) }

sub is_empty { !!(uc $_[0]->req->method eq 'HEAD' || $_[0]->res->is_empty) }
Expand Down
1 change: 0 additions & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -123,7 +123,6 @@ sub client_handshake {
$headers->sec_websocket_key($challenge) unless $headers->sec_websocket_key;
}

sub client_read { shift->server_read(@_) }
sub client_write { shift->server_write(@_) }

sub connection { shift->handshake->connection }
Expand Down
10 changes: 7 additions & 3 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -4,6 +4,7 @@ use Mojo::Base 'Mojo::EventEmitter';
# "Fry: Since when is the Internet about robbing people of their privacy?
# Bender: August 6, 1991."
use Mojo::Channel::HTTP::Client;
use Mojo::Channel::WebSocket::Client;
use Mojo::IOLoop;
use Mojo::Util qw(monkey_patch term_escape);
use Mojo::UserAgent::CookieJar;
Expand Down Expand Up @@ -240,8 +241,11 @@ sub _finish {
if (my $new = $self->transactor->upgrade($old)) {
weaken $self;
$new->on(resume => sub { $self->_write($id) });
$c->{cb}($self, $c->tx($new)->tx);
return $new->client_read($old->res->content->leftovers);
my $cb = $c->{cb};
$c = $self->{connections}{$id}
= Mojo::Channel::WebSocket::Client->new(ioloop => $c->ioloop, tx => $new);
$cb->($self, $new);
return $c->read($old->res->content->leftovers);
}

# Finish normal connection and handle redirects
Expand All @@ -258,7 +262,7 @@ sub _read {

# Process incoming data
warn term_escape "-- Client <<< Server (@{[_url($tx)]})\n$chunk\n" if DEBUG;
$tx->client_read($chunk);
$c->read($chunk);
if ($tx->is_finished) { $self->_finish($id) }
elsif ($tx->is_writing) { $self->_write($id) }
}
Expand Down

0 comments on commit d9b36c6

Please sign in to comment.