Skip to content

Commit

Permalink
move _close and resume methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jberger committed Nov 29, 2015
1 parent 04cd573 commit c4b742b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
12 changes: 12 additions & 0 deletions lib/Mojo/Channel/HTTP.pm
Expand Up @@ -4,6 +4,12 @@ use Mojo::Base 'Mojo::Channel';

has tx => sub { Mojo::Transaction->new };

sub close {
my $self = shift;
$self->{state} = 'finished';
$self->tx->_announce_finished;
}

sub incoming { die 'meant to be overloaded by subclass' }

sub is_finished { (shift->{state} // '') eq 'finished' }
Expand All @@ -14,6 +20,12 @@ sub is_writing { (shift->{state} // 'write') eq 'write' }

sub outgoing { die 'meant to be overloaded by subclass' }

sub resume {
my $self = shift;
$self->{state} = 'write';
$self->tx->_announce_resume;
}

sub write {
my $self = shift;
return '' unless $self->{state} eq 'write';
Expand Down
17 changes: 17 additions & 0 deletions lib/Mojo/Channel/HTTP/Client.pm
Expand Up @@ -2,6 +2,23 @@ package Mojo::Channel::HTTP::Client;

use Mojo::Base 'Mojo::Channel::HTTP';

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

# Premature connection close
my $res = $self->tx->res->finish;
if ($close && !$res->code && !$res->error) {
$res->error({message => 'Premature connection close'});
}

# 4xx/5xx
elsif ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error({message => $res->message, code => $res->code});
}

return $self->SUPER::close;
}

sub incoming { shift->tx->res }

sub outgoing { shift->tx->req }
Expand Down
34 changes: 6 additions & 28 deletions lib/Mojo/Transaction.pm
Expand Up @@ -14,23 +14,7 @@ has [
has req => sub { Mojo::Message::Request->new };
has res => sub { Mojo::Message::Response->new };

sub client_close {
my ($self, $close) = @_;

# Premature connection close
my $res = $self->res->finish;
if ($close && !$res->code && !$res->error) {
$res->error({message => 'Premature connection close'});
}

# 4xx/5xx
elsif ($res->is_status_class(400) || $res->is_status_class(500)) {
$res->error({message => $res->message, code => $res->code});
}

return $self->server_close;
}

sub client_close { shift->_channel->close(@_) }
sub client_read { shift->_channel->read(@_) }
sub client_write { shift->_channel->write(@_) }

Expand Down Expand Up @@ -60,9 +44,9 @@ sub remote_address {
: $self->original_remote_address;
}

sub resume { shift->_state(qw(write resume)) }
sub server_close { shift->_state(qw(finished finish)) }
sub resume { shift->channel->resume(@_) }

sub server_close { shift->_channel(1)->close(@_) }
sub server_read { shift->_channel(1)->read(@_) }
sub server_write { shift->_channel(1)->write(@_) }

Expand All @@ -78,17 +62,11 @@ sub _channel {
my $class = $server ? 'Mojo::Channel::HTTP::Server' : 'Mojo::Channel::HTTP::Client';
my $channel = $class->new(tx => $self);
Scalar::Util::weaken($channel->{tx});
return $channel;
return $self->channel($channel)->channel;
}

sub _state {
my ($self, $state, $event) = @_;
#TODO encapsulation!!!!!
if (my $channel = $self->channel) {
$channel->{state} = $state;
}
return $self->emit($event);
}
sub _announce_finish { shift->emit('finish') }
sub _announce_resume { shift->emit('resume') }

1;

Expand Down

0 comments on commit c4b742b

Please sign in to comment.