Navigation Menu

Skip to content

Commit

Permalink
move client_close to channels
Browse files Browse the repository at this point in the history
note: Mojo::Transaction::WebSocket::client_close should not have been
calling Mojo::Transaction::client_close or rather that method should
have been in Mojo::Transaction::HTTP::client_close and a stub in its
place in the base clase pointing to client_close. This logic is now what
is employed by this commit
  • Loading branch information
jberger committed Jan 9, 2016
1 parent 71516d1 commit 9d2db69
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 26 deletions.
2 changes: 2 additions & 0 deletions lib/Mojo/Channel/HTTP.pm
@@ -1,6 +1,8 @@
package Mojo::Channel::HTTP;
use Mojo::Base 'Mojo::Channel';

sub close { shift->{tx}->server_close }

sub is_server { 0 }

sub write {
Expand Down
18 changes: 18 additions & 0 deletions lib/Mojo/Channel/HTTP/Client.pm
@@ -1,6 +1,24 @@
package Mojo::Channel::HTTP::Client;
use Mojo::Base 'Mojo::Channel::HTTP';

sub close {
my ($self, $close) = @_;
my $tx = $self->{tx};

# Premature connection close
my $res = $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});
}

$self->SUPER::close;
}

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

Expand Down
2 changes: 2 additions & 0 deletions lib/Mojo/Channel/WebSocket.pm
Expand Up @@ -3,6 +3,8 @@ use Mojo::Base 'Mojo::Channel';

use Mojo::WebSocket 'parse_frame';

sub close { shift->{tx}->server_close }

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

Expand Down
25 changes: 0 additions & 25 deletions lib/Mojo/Transaction.pm
Expand Up @@ -10,23 +10,6 @@ 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});
}

$self->server_close;
}

sub connection {
my $self = shift;
return $self->emit(connection => $self->{connection} = shift) if @_;
Expand Down Expand Up @@ -172,14 +155,6 @@ HTTP response, defaults to a L<Mojo::Message::Response> object.
L<Mojo::Transaction> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 client_close
$tx->client_close;
$tx->client_close(1);
Transaction closed client-side, no actual connection close is assumed by
default, used to implement user agents such as L<Mojo::UserAgent>.
=head2 connection
my $id = $tx->connection;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -230,7 +230,7 @@ sub _finish {
$c->{ioloop}->remove($c->{timeout}) if $c->{timeout};

return $self->_reuse($id, $close) unless my $old = $c->{tx};
$old->client_close($close);
$c->close($close);

# Finish WebSocket
return $self->_remove($id) if $old->is_websocket;
Expand Down

0 comments on commit 9d2db69

Please sign in to comment.