Skip to content

Commit

Permalink
move the client_read methods to respective Channel classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jberger committed Jan 9, 2016
1 parent 9fa69c4 commit ed1bcf9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
17 changes: 17 additions & 0 deletions lib/Mojo/Channel/HTTP/Client.pm
@@ -1,4 +1,21 @@
package Mojo::Channel::HTTP::Client;
use Mojo::Base 'Mojo::Channel::HTTP';

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;
13 changes: 13 additions & 0 deletions lib/Mojo/Channel/WebSocket.pm
@@ -1,4 +1,17 @@
package Mojo::Channel::WebSocket;
use Mojo::Base 'Mojo::Channel';

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;
16 changes: 0 additions & 16 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -3,22 +3,6 @@ use Mojo::Base 'Mojo::Transaction';

has [qw(next 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
9 changes: 5 additions & 4 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -241,11 +241,11 @@ sub _finish {
if (my $new = $self->transactor->upgrade($old)) {
weaken $self;
$new->on(resume => sub { $self->_write($id) });
$self->{connections}{$id}
my $ws = $self->{connections}{$id}
= Mojo::Channel::WebSocket::Client->new(ioloop => $c->{ioloop},
tx => $new);
$c->{cb}($self, $c->{tx} = $new);
return $new->client_read($old->res->content->leftovers);
return $ws->read($old->res->content->leftovers);
}

# Finish normal connection and handle redirects
Expand All @@ -257,11 +257,12 @@ sub _read {
my ($self, $id, $chunk) = @_;

# Corrupted connection
return $self->_remove($id) unless my $tx = $self->{connections}{$id}{tx};
my $c = $self->{connections}{$id};
return $self->_remove($id) unless my $tx = $c->{tx};

# 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 ed1bcf9

Please sign in to comment.